首页 > 解决方案 > 错误:[polling_error] {"code":"ETELEGRAM","message":"ETELEGRAM: 401 Unauthorized"}

问题描述

请帮我解决这个问题,我得到了 polling_error

我要在电报中创建一个机器人并为我自己定制它

但是当我运行程序时,我遇到了一些这样的错误:

node-telegram-bot-api deprecated 不推荐使用自动启用取消承诺。

和另一个这样的错误:

错误:[polling_error] {"code":"ETELEGRAM","message":"ETELEGRAM: 401 Unauthorized"}

我该如何解决这个问题?

完整的代码在这里:

const TelegramBot = require('node-telegram-bot-api');
const token = '***';
const bot = new TelegramBot(token, {polling: true});

bot.on('message', (msg) => {
    let Hi = "hi";
    if (msg.text.toString().toLowerCase().indexOf(Hi) === 0) {
        bot.sendMessage(msg.chat.id,"Hello dear user");
    }
});

标签: node.js

解决方案


const TelegramBot = require('node-telegram-bot-api')
const Agent = require('socks5-https-client/lib/Agent')

const bot = new TelegramBot(process.env.TELEGRAM_API_TOKEN, {
    polling: true,
    request: {
        agentClass: Agent,
        agentOptions: {
            socksHost: process.env.PROXY_SOCKS5_HOST,
            socksPort: parseInt(process.env.PROXY_SOCKS5_PORT),
            // If authorization is needed:
            // socksUsername: process.env.PROXY_SOCKS5_USERNAME,
            // socksPassword: process.env.PROXY_SOCKS5_PASSWORD
        }
    }
})

推荐阅读