node-telegram-bot-api: how to know if bot is blocked by user (catch 403 forbbidden)?

Sometimes users deletes/block the bots and this error shows up:

W20170201-07:16:56.291(0)? (STDERR) Unhandled rejection Error: 403 {"ok":false,"error_code":403,"description":"Forbidden: Bot was blocked by the user"}
W20170201-07:16:56.293(0)? (STDERR)     at /home/x/telegram/app/node_modules/node-telegram-bot-api/lib/telegram.js:219:17
W20170201-07:16:56.293(0)? (STDERR)     at tryCatcher (/home/x/telegram/app/node_modules/request-promise/node_modules/bluebird/js/main/util.js:26:23)
W20170201-07:16:56.293(0)? (STDERR)     at Promise._settlePromiseFromHandler (/home/x/telegram/app/node_modules/request-promise/node_modules/bluebird/js/main/promise.js:510:31)
W20170201-07:16:56.293(0)? (STDERR)     at Promise._settlePromiseAt (/home/x/telegram/app/node_modules/request-promise/node_modules/bluebird/js/main/promise.js:584:18)
W20170201-07:16:56.294(0)? (STDERR)     at Promise._settlePromises (/home/x/telegram/app/node_modules/request-promise/node_modules/bluebird/js/main/promise.js:700:14)
W20170201-07:16:56.294(0)? (STDERR)     at Async._drainQueue (/home/x/telegram/app/node_modules/request-promise/node_modules/bluebird/js/main/async.js:123:16)

i tried doing .catch on sendMessage but it doesn’t seem to work, even “old school” try/catch around bot.sendMessage curiously didn’t seem to work.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 4
  • Comments: 15

Commits related to this issue

Most upvoted comments

@GochoMugo > What semantically-correct way do you recommend, in place of exceptions?

Well, in golang the syntax is:

result, err := instance.DoSomething();
if err != nil {
   // do something with result
}

The problem with this approach IMO is that exceptions are expensive. It would be better to avoid throwing exception

For example,

bot.sendMessage(chatId, text).then(function(resp) {
  // ...snip...
}).catch(function(error) {
  if (error.response && error.response.statusCode === 403) {
    // ...snip...
  }
});

exceptions aren’t expensive on js, anyway, if you want to avoid try/catch you use “reject” from Promises, which seem like it’s already the case?

That’s a non-issue, even if it was a performance issue nobody is running sendMessage on an infinite loop that’s supposed to perform.

For example,

bot.sendMessage(chatId, text).then(function(resp) {
  // ...snip...
}).catch(function(error) {
  if (error.response && error.response.statusCode === 403) {
    // ...snip...
  }
});

I’m surprised Telegram doesn’t have any API to listen on when user exits or delete bot.

@GochoMugo Just a regular promise.error ( or promise.catch ) ?

  promise = bot.sendMessage to_id, text, options
  promise
  .then ->
    console.log "all good"
  .error ( e ) ->
    console.log 'error sending message'
    console.log e