Telegraf: How do I handle errors from Telegram API?

Created on 26 Apr 2017  路  3Comments  路  Source: telegraf/telegraf

Two errors specifically keep appearing in the console of my bot:
(node:4198) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: 403: Forbidden: bot was kicked from the group chat
(node:4198) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: 429: Too Many Requests: retry after 45

How do I handle these errors?

The docs and examples don't seem to mention this

question

All 3 comments

Hey.

Seems like you just swallow Telegram errors.

bot.command('help', (ctx) => {
  // Result of call just swallowed
  ctx.reply('yo')
})

// You can just `pipe` call to upper level
// and hadle errors with telegraf: http://telegraf.js.org/introduction.html#error-handling
bot.command('help', (ctx) => { 
  return ctx.reply('yo')
})

// with `catch`
bot.command('help', (ctx) => { 
  ctx.reply('yo').catch((err) => console.log(err))
})

// with async/await style
bot.command('help', async (ctx) => { 
  try {
    await ctx.reply('yo')
  } catch (err) {
    console.log(err)
  }
})

Great article about errors&promises

Hrm, so I'd have to handle every single ctx.reply then..

Correct, you need to explicitly handle all Telegram errors.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kuhel picture kuhel  路  3Comments

wilcorrea picture wilcorrea  路  3Comments

aminjoharinia picture aminjoharinia  路  3Comments

bostrot picture bostrot  路  3Comments

sijie123 picture sijie123  路  3Comments