javascript - Telegram 机器人意外结束

标签 javascript node.js telegram-bot http-proxy

尝试制作我的第一个电报机器人,在所有示例和说明中,它看起来非常简单且易于重复。但是,我的机器人根本不起作用。首先,我来自俄罗斯,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();

没有任何反应,程序完成 enter image description here .

enter image description here

我知道问题出在我的代理配置中,但无法理解到底出了什么问题。

最佳答案

问题出在代理上。我使用 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/

相关文章:

javascript - 打开 Windows 从网页运行命令

javascript - 使用 jquery toggle 停止点击次数的增加

telegram - Telegram bot api 很奇怪,无法更改音频标题

javascript - 如何在json数组对象中添加<a> HTML标签

javascript - 如何获取客户端用户的最后一条消息?

JAVA Telegram bots api 获取更新时出错 : Conflict: terminated by other long poll or webhook

laravel - 如何修复 Telegram Webhook 响应中的 "Wrong response from the webhook: 301 Moved Permanently"?

javascript - 如何从js客户端发送json格式的ArrayBuffer到nodejs服务器?

javascript - 分组并显示相同类型的元素

node.js - 如何按条件匹配子文档中的数组元素