首页 > 解决方案 > Telegram 机器人意外结束

问题描述

尝试制作我的第一个电报机器人,在所有示例和说明中,它看起来非常简单且易于重复。但是,我的机器人根本不起作用。首先,我来自俄罗斯,电报 api 被阻止,所以我需要使用代理。从https://www.socks-proxy.net/拿了一个。从 BotFather 获得令牌。现在当我运行我的脚本时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();

没有任何反应,程序完成在此处输入图像描述

在此处输入图像描述

我知道问题出在我的代理配置中,但不明白到底出了什么问题。

标签: javascriptnode.jstelegram-bothttp-proxy

解决方案


问题出在代理上。我用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();

推荐阅读