TG机器人搭建教程Node.js

创建机器人

这个步骤网上有很多方案,直接采用即可(通过botfather创建)

这里只讲开发 不讲创建 浪费各位的时间了

开发机器人

github 上有很多开源的 如 grammYtelegramsjsnode-telegram-bot-api

使用 node-telegram-bot-api 做演示

使用 node-telegram-bot-api 实现自动回复(Node.js

github搜索node-telegram-bot-api,查看README文档使用快速入门方案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
npm i node-telegram-bot-api
# 使用TypeScript开发时需要用到的类型声明
npm install --save-dev @types/node-telegram-bot-api
# 快速入门(创建index.ts编写以下代码)
const TelegramBot = require('node-telegram-bot-api');

// replace the value below with the Telegram token you receive from @BotFather
const token = 'YOUR_TELEGRAM_BOT_TOKEN';

// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, {polling: true});

// Matches "/echo [whatever]"
bot.onText(/\/echo (.+)/, (msg, match) => {
// 'msg' is the received Message from Telegram
// 'match' is the result of executing the regexp above on the text content
// of the message

const chatId = msg.chat.id;
const resp = match[1]; // the captured "whatever"

// send back the matched "whatever" to the chat
bot.sendMessage(chatId, resp);
});

// Listen for any kind of message. There are different kinds of
// messages.
bot.on('message', (msg) => {
const chatId = msg.chat.id;

// send a message to the chat acknowledging receipt of their message
bot.sendMessage(chatId, 'Received your message');
});

注意需要把token换成自己机器人的token

原本以为这部分可以顺利完成,结果运行node .\index.ts后卡住,控制台与tg聊天框均没有反应,通过查找与询问发现tg被国内墙了,

虽然已经使用🪜可以使用tg,但是程序并没法发送请求到api.telegram.org服务器,所以需要在程序中使用代理服务,这里以使用V2rayN为例,

首先在V2rayN的参数设置中设定好本地socks监听端口(一般默认为10808),然后修改程序添加代理服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const { SocksProxyAgent } = require('socks-proxy-agent');

// 使用 socks 代理
const agent = new SocksProxyAgent('socks://127.0.0.1:10808');
const token = 'YOUR_TELEGRAM_BOT_TOKEN'; // 检查你的 token 是否正确

const bot = new TelegramBot(token, {
polling: true, // 启用 polling
request: {
agent, // 使用代理进行请求
},
});

bot.onText(/\/ping/, (msg) => {
const chatId = msg.chat.id;
bot.sendMessage(chatId, 'pong');
});

// 启动 bot
bot.on('polling_error', (error) => console.log('Polling error:', error)); // 打印错误

注意,在使用代理服务时我原先在bot配置里加入了testEnvironment: true参数,

导致运行时程序以为我在模拟环境中测试,并没有与实际的telegarm API服务器通信,

从而报错 error: [polling_error] {"code":"ETELEGRAM","message":"ETELEGRAM: 401 Unauthorized"}
踩过各种坑之后运行node .\index.ts终于成功了