Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to skip the "Forbidden: bot was blocked by the user" error? #268

Open
Amirhan-Taipovjan-Greatest-I opened this issue Apr 2, 2023 · 2 comments

Comments

@Amirhan-Taipovjan-Greatest-I

The case:
User sent a command and then blocked the bot when bot was switched off.

I can't make any solution for this problem. try-catch, if-else... they didn't help Me.
The program sends the error and stops ownself...

@miannoodle01
Copy link

you may write a recursive script, in which on specific error like blocked by user, script starts again like:
int main() {try {} catch(exception &e) {if (e == "something") {main()}}}

@rd181002
Copy link

rd181002 commented May 14, 2024

if(bot.getApi().blockedByUser(message->chat->id)) return;

Adding this condition check to the beginning of each 'listener' solved the problem for me.

For instance:

bot.getEvents().onAnyMessage([&bot](Message::Ptr message) {
       if(bot.getApi().blockedByUser(message->chat->id)) return;
       printf("User wrote %s\n", message->text.c_str());
       if (StringTools::startsWith(message->text, "/start")) {
           return;
       }
       bot.getApi().sendMessage(message->chat->id, "Your message is: " + message->text);
   });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants