skip to Main Content

I’m trying to create a Telegram bot but when I run the code I get these errors:

"node-telegram-bot-api deprecated Automatic enabling of cancellation of promises is deprecated
In the future, you will have to enable it yourself.
See https://github.com/yagop/node-telegram-bot-api/issues/319. internalmodulescjsloader.js:1063:30"

"error: [polling_error] {"code":"ETELEGRAM","message":"ETELEGRAM: 409 Conflict: terminated by
other getUpdates request; make sure that only one bot instance is running"}"

Any help would be appreciated.

Here is my bot.js code if that helps:

const TelegramBot = require('node-telegram-bot-api');
const axios = require('axios');

const parser = require('./parser.js');
 
require('dotenv').config();
 
const token = process.env.TELEGRAM_TOKEN;
let bot;
 
if (process.env.NODE_ENV === 'production') {
   bot = new TelegramBot(token);
   bot.setWebHook(process.env.HEROKU_URL + bot.token);
} else {
   bot = new TelegramBot(token, { polling: true });
}
 // Matches "/word whatever"
bot.onText(//word (.+)/, (msg, match) => {
   const chatId = msg.chat.id;
   const word = match[1];
   axios
     .get(`${process.env.OXFORD_API_URL}/entries/en-gb/${word}`, {
       params: {
         fields: 'definitions',
         strictMatch: 'false'
       },
       headers: {
         app_id: process.env.OXFORD_APP_ID,
         app_key: process.env.OXFORD_APP_KEY
       }
     })
     .then(response => {
       const parsedHtml = parser(response.data);
       bot.sendMessage(chatId, parsedHtml, { parse_mode: 'HTML' });
     })
     .catch(error => {
       const errorText = error.response.status === 404 ? `No definition found for the word: <b>${word}</b>` : `<b>An error occured, please try again later</b>`;
       bot.sendMessage(chatId, errorText, { parse_mode:'HTML'})
     });
 });

2

Answers


    • Install this package npm I dotenv
    • Create a file called .env at the root of your project.
    • Add to that file this NTBA_FIX_319=1
    • Add to the top of your index.js file (or whatever file that contains your bot instance): require('dotenv').config()
    • Restart your bot.
    • So your code will look like

    require('dotenv').config();
    
    const TelegramBot = require('node-telegram-bot-api');
    
    // replace the value below with the Telegram token you receive from @BotFather
    const token = 'YOUR_TELEGRAM_BOT_TOKEN';
    
    // Create a bot that uses 'polling' to fetch new updates
    const bot = new TelegramBot(token, {polling: true});
    
    bot.on('message', (msg) => {
      const chatId = msg.chat.id;
    
      // send a message to the chat acknowledging receipt of their message
      bot.sendMessage(chatId, 'Received your message');
    });
    
    Login or Signup to reply.
  1. 1) Get rid of the promise cancellation warning

    The simplest, is to add that line at the top of your bot file:

    process.env.NTBA_FIX_319 = 1 // this line SHOULD be above all imports
    
    const TelegramBot = require('node-telegram-bot-api')
    
    // ...rest of your telegram bot code
    

    2) The 409 conflict error

    In short, there are two version of your telegram bot running at the same time, so:

    • check if there is not multiple instance of your server running your bot, which can be the case when using workers, pm2…
    • check that your bot script is not started twice

    Other causes and explanations can be found here and here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search