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

fixed telegram respond issue #645

Merged
merged 1 commit into from
Oct 26, 2024
Merged

Conversation

Bob-six
Copy link
Collaborator

@Bob-six Bob-six commented Oct 26, 2024

Summary by Sourcery

Fix the Telegram bot response issue by correctly registering message, callback, and command handlers, and enhance webhook registration by capturing the response from the requests.post call.

Bug Fixes:

  • Fix the issue with Telegram bot response by ensuring correct registration of message, callback, and command handlers.

Enhancements:

  • Improve webhook registration by capturing the response from the requests.post call.

Copy link
Contributor

sourcery-ai bot commented Oct 26, 2024

Reviewer's Guide by Sourcery

This PR fixes issues with Telegram bot response handling by correcting handler registration attributes and improving webhook setup. The changes focus on properly initializing message, callback, and command handlers while also enhancing the webhook security implementation.

Sequence diagram for Telegram bot webhook setup

sequenceDiagram
    participant Bot as Telegram Bot
    participant TelegramAPI as Telegram API
    participant Settings as Settings

    Bot->>TelegramAPI: POST /setWebhook
    Note right of Bot: Using secret token for security
    TelegramAPI-->>Bot: Webhook registered

    Note over Bot,TelegramAPI: Webhook setup with security token
Loading

Updated class diagram for Telegram bot handlers

classDiagram
    class BaseTelegramMessageHandler {
        +message
    }
    class BaseTelegramCommandHandler {
        +command
    }

    class Bot {
        +callback_handlers
        +message_handlers
        +command_handlers
        +ready()
    }

    Bot --> BaseTelegramMessageHandler : uses
    Bot --> BaseTelegramCommandHandler : uses

    note for Bot "Registers handlers for messages, callbacks, and commands"
Loading

File-Level Changes

Change Details Files
Fixed handler registration by correcting attribute references
  • Changed 'callback' attribute to 'message' in message handler registration
  • Changed 'callback' attribute to 'command' in command handler registration
  • Added initialization of handlers in the ready() method
telegram/bot.py
Enhanced webhook handling and security
  • Added response capture for webhook registration request
  • Removed redundant comment about webhook and token replacement
  • Removed debug logging of request headers
telegram/bot.py
telegram/views.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@Bob-six Bob-six merged commit b48c120 into develop Oct 26, 2024
1 check passed
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @alimaktabi - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider validating the response from the webhook setup request to ensure it succeeded. A failed webhook registration could cause issues that are hard to debug.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟡 Security: 1 issue found
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): The webhook registration response should be checked for errors to ensure proper setup

Consider validating the response status code and handling potential errors to prevent silent failures in webhook setup

@@ -46,8 +46,6 @@ def telebot_respond(request):
# if client_ip not in telegram_ips:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 issue (security): IP validation should not be commented out as it reduces security defense in depth

While secret token validation provides security, IP validation adds an additional important security layer. Consider re-enabling it or documenting why it's disabled.

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

Successfully merging this pull request may close these issues.

1 participant