尝试制作我的第一个电报机器人,在所有示例和说明中,它看起来非常简单且易于重复。但是,我的机器人根本不起作用。首先,我来自俄罗斯,telegram api被屏蔽,所以我需要使用代理。从 https://www.socks-proxy.net/ 拿了一份。从 BotFather 获得 token 。现在,当我运行脚本 telegraf.js
时:
const Telegraf = require('telegraf');
const SocksAgent = require('socks5-https-client/lib/Agent');
const socksAgent = new SocksAgent({
socksHost: '103.206.97.70',
socksPort: 4145,
});
const bot = new Telegraf(MY_TOKEN, {
telegram: {
agent: socksAgent,
}
});
bot.hears('hi', ctx => {
return ctx.reply('Hey!');
});
bot.startPolling();
我知道问题出在我的代理配置中,但无法理解到底出了什么问题。
最佳答案
问题出在代理上。我使用 https-proxy-agent
而不是 socks5-https-client
import Telegraf from 'telegraf';
import config from 'config';
import HttpsProxyAgent from 'https-proxy-agent';
const TOKEN = config.get('token');
const proxy = config.get('proxy');
const bot = new Telegraf(TOKEN, {
telegram: {
agent: new HttpsProxyAgent({
host: proxy.host,
port: proxy.port
})
},
});
bot.hears('hi', ctx => {
return ctx.reply('Hey!');
});
bot.startPolling();
关于javascript - Telegram 机器人意外结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54060545/