Skip to content

Commit

Permalink
fixed telegram respond issue
Browse files Browse the repository at this point in the history
  • Loading branch information
alimaktabi committed Oct 26, 2024
1 parent 5e3dabf commit 5831034
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
15 changes: 9 additions & 6 deletions telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
)
Expand All @@ -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)
)
Expand Down Expand Up @@ -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
}


Expand All @@ -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
}
2 changes: 0 additions & 2 deletions telegram/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5831034

Please sign in to comment.