hi,
I wrote a bot for connecting 2 people together.
it works fine but when a user block it, the bot cannot do anything. It stick in a loop and send a message continuously to users.
$data=Telegram::commandsHandler(false);
when I want above method it gaves me:
TelegramResponseException in TelegramResponseException.php line 58: Forbidden: bot was blocked by the user
and it dose not work for any user. not just that user who blocked my bot!!
how can I get rid of this problem?
thanks.
I used Telegram::setAsyncRequest(true)-> before each sendMessage method and get rid of this error.
but still I want to know when I have this 403 error to handle error and do something.
Don't be using the setAsyncRequest method unless you REALLY understand what it is and why you need it.
Why don't you just use a simple Try and Catch block and deal with the exception?
This is VERY rough and basic example below.
try {
$telegram->sendMessage([
'chat_id' => '<PERSONS_ID>',
'text' => 'Here is some text',
]);
} catch (TelegramResponseException $e) {
$errorData = $e->getResponseData();
if ($errorData['ok'] === false) {
$telegram->sendMessage([
'chat_id' => '<ADMINISTRATOR ID>',
'text' => 'There was an error for a user. ' . $errorData['error_code'] . ' ' . $errorData['description'],
]);
}
}
Please let us know if this resolved your problem.
Dear @jonnywilliamson
thank you for the response. it works for me.
Hi.Sorry to bring this up again but using try catch doesn't seem to work for me. Although it catches exceptions but my bot encounters a loop all the same. Any suggestions?
@mohsennrs you got any another solution if yes then please let me know i have also been fallen pray to telegram infinite loop
@neel004 I got it to work at the time but I'm really not sure how I fixed it, but I think using general Exception class did the job. My code is like this:
`try{
$response = $this->telegram->deleteMessage([
"chat_id" => "chat id",
"message_id" => "message id"
]);
return $response
} catch (\Exception $e) {
return $e;
}`
Most helpful comment
Don't be using the
setAsyncRequestmethod unless you REALLY understand what it is and why you need it.Why don't you just use a simple Try and Catch block and deal with the exception?
This is VERY rough and basic example below.
Please let us know if this resolved your problem.