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

Created on 1 Feb 2017  路  11Comments  路  Source: yagop/node-telegram-bot-api

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.

docs question

Most helpful comment

@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
}

All 11 comments

have the user added the bot?

@hems could you provide us with a code snippet of how you are trying to catch the error?

@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

If the exception is expected flow, you can just check it and not printing the stacktrace on the log

@chiu0602 sorry i don't really understand what you mean here?
@GochoMugo any idea on how to catch that one?

For example,

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

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

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

Hello, and sorry for bringing this up again, but the soultion above can catch the exception, thus I can remove the user from the DB, but how to know if the user UNBLOCK the bot and start sending messages again?

@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
}

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

antonrifco picture antonrifco  路  3Comments

abbddo picture abbddo  路  3Comments

mbrammer picture mbrammer  路  3Comments

alikhil picture alikhil  路  3Comments

Hostile picture Hostile  路  3Comments