From 58310346a113e4b0b70a1e0e3dff444e67db346f Mon Sep 17 00:00:00 2001 From: Ali Maktabi Date: Sat, 26 Oct 2024 19:17:57 +0330 Subject: [PATCH] fixed telegram respond issue --- telegram/bot.py | 15 +++++++++------ telegram/views.py | 2 -- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/telegram/bot.py b/telegram/bot.py index db397a6..d8b2b2b 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -233,14 +233,13 @@ def ensure_webhook(self): """ import requests - # Replace with your webhook and Telegram Bot token webhook_url = "https://api.unitap.app/api/telegram/wh/" telegram_api_url = ( f"https://api.telegram.org/bot{telebot_instance.token}/setWebhook" ) # Register webhook with secret token for added security - requests.post( + res = requests.post( telegram_api_url, data={"url": webhook_url, "secret_token": settings.TELEGRAM_BOT_API_SECRET}, ) @@ -250,6 +249,10 @@ def ready(self): Prepare the bot by registering the message and callback handlers for processing updates. This is the setup function that connects Telegram message updates with handler functions. """ + self.callback_handlers = register_callback_handlers() + self.message_handlers = register_message_handlers() + self.command_handlers = register_command_handlers() + telebot_instance.message_handler(func=lambda _: True)( lambda message: self.on_telegram_message(message) ) @@ -387,9 +390,9 @@ def handler(self, callback: types.CallbackQuery): def register_message_handlers(): return { - subclass.callback: subclass(telebot_instance) + subclass.message: subclass(telebot_instance) for subclass in BaseTelegramMessageHandler.__subclasses__() - if subclass.callback + if subclass.message } @@ -403,7 +406,7 @@ def register_callback_handlers(): def register_command_handlers(): return { - subclass.callback: subclass(telebot_instance) + subclass.command: subclass(telebot_instance) for subclass in BaseTelegramCommandHandler.__subclasses__() - if subclass.callback + if subclass.command } diff --git a/telegram/views.py b/telegram/views.py index 048cd74..d0721c0 100644 --- a/telegram/views.py +++ b/telegram/views.py @@ -46,8 +46,6 @@ def telebot_respond(request): # if client_ip not in telegram_ips: # raise PermissionDenied("Invalid IP address") - logger.info(request.headers) - if ( request.headers.get("X-Telegram-Bot-Api-Secret-Token") != settings.TELEGRAM_BOT_API_SECRET