In php without library Is there a code to drop webhook

requests if the bot is not working?
Also another one for avoiding flood by someone

14 ответов

21 просмотр

Drop pending requests: <?php file_get_contents('https://api.telegram.org/bot<token>/setWebhook?url=<you_url>&drop_pending_updates=true');

Can you explain more? When a bot contains a bug, the bot receives the request but answers when I fix the bug Where should I put this at the end of the script?

ッ Marshmallow 🎀 ❦ ᵐⁱˢˢ Σ ᶜᵒˡˡᵉᵍⁱᵃⁿ ᵃⁿᵈ ᵍᵃᵐᵉʳ 👱🏻‍♀ 🦋²⁰²²ᵗʰ 🎀 🧞‍♀️×͜×
Can you explain more? When a bot contains a bug, t...

no, I assumed you needed a way to reset webhooks updates after a long shutdown of the bot. Could you describe your problem in more detail?

If there is a bug in the script, the bot does not respond to users but receives requests when they send something means if someone send 10 times command /start The webhook will receive a 10 pending requests When I fix the error in script the bot will responds to the user 10 times I want to automatically drop these requests when the bot does not respond, what should I do?

ッ Marshmallow 🎀 ❦ ᵐⁱˢˢ Σ ᶜᵒˡˡᵉᵍⁱᵃⁿ ᵃⁿᵈ ᵍᵃᵐᵉʳ 👱🏻‍♀ 🦋²⁰²²ᵗʰ 🎀 🧞‍♀️×͜×
If there is a bug in the script, the bot does not ...

Updates (requests) are pending when your script responds to telegram not with 200 http code. So you can try to do something like this in the beginning of the php script: <?php register_shutdown_function(function() { if(http_response_code() != 200) { http_response_code(200); file_get_contents('https://api.telegram.org/bot<token>/sendMessage?' . http_build_query([ 'chat_id' => '<chat id from update request>', 'text' => 'An internal server error has occurred. Please try again later.', ])); } });

ッ Marshmallow 🎀 ❦ ᵐⁱˢˢ Σ ᶜᵒˡˡᵉᵍⁱᵃⁿ ᵃⁿᵈ ᵍᵃᵐᵉʳ 👱🏻‍♀ 🦋²⁰²²ᵗʰ 🎀 🧞‍♀️×͜×
Not working

If you don't want to send the "An internal server error has occurred. Please try again later." message to user you can use: <?php register_shutdown_function(function() { http_response_code(200); });

ッ Marshmallow 🎀 ❦ ᵐⁱˢˢ Σ ᶜᵒˡˡᵉᵍⁱᵃⁿ ᵃⁿᵈ ᵍᵃᵐᵉʳ 👱🏻‍♀ 🦋²⁰²²ᵗʰ 🎀 🧞‍♀️×͜×
Not working

Also ensure the ini directive display_errors is 0 (or use ini_set('display_errors', 0); in the beginning of the php script) and the script has no output from print, echo, etc. (because http_response_code doesn't work after some output).

Похожие вопросы

Карта сайта