Search code examples
javascriptnode.jstelegram-bothttp-proxy

Telegram bot ends unexpectedly


Trying to make my first telegram bot, in all examples and instructions it looks very simple and easy to repeat. However, my bot doesn't work at all. Firstly, I am from Russia and telegram api is blocked, so I need to use proxy. Took one from https://www.socks-proxy.net/. Got token from BotFather. Now when I run my script 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();

nothing happens and program finishedenter image description here.

enter image description here

I understand that problem is in my proxy configuration, but can't understand what exactly wrong.


Solution

  • The problem was in proxy. I used https-proxy-agent instead of 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();