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

Develop #646

Merged
merged 2 commits into from
Oct 26, 2024
Merged

Develop #646

merged 2 commits into from
Oct 26, 2024

Conversation

alimaktabi
Copy link
Collaborator

@alimaktabi alimaktabi commented Oct 26, 2024

Summary by Sourcery

Enhance the Telegram bot by improving webhook registration and refactoring handler registration to use more specific attributes.

Enhancements:

  • Improve webhook registration by storing the response from the requests.post call.
  • Refactor handler registration to use message, callback, and command attributes instead of callback.

Copy link
Contributor

sourcery-ai bot commented Oct 26, 2024

Reviewer's Guide by Sourcery

This PR implements several improvements to the Telegram bot implementation, focusing on webhook security, handler registration, and code cleanup. The changes include proper handler attribute references, webhook response handling, and removal of debug logging.

Sequence diagram for webhook registration process

sequenceDiagram
    participant Bot
    participant TelegramAPI
    Bot->>TelegramAPI: POST /setWebhook with secret_token
    TelegramAPI-->>Bot: Response stored in 'res'
Loading

Updated class diagram for handler registration

classDiagram
    class BaseTelegramMessageHandler {
        +message
    }
    class BaseTelegramCommandHandler {
        +command
    }
    class Bot {
        +callback_handlers
        +message_handlers
        +command_handlers
    }
    Bot --> BaseTelegramMessageHandler : registers
    Bot --> BaseTelegramCommandHandler : registers
Loading

File-Level Changes

Change Details Files
Improved webhook setup and security implementation
  • Added response capture for webhook registration request
  • Removed outdated comment about webhook and token replacement
telegram/bot.py
Fixed handler registration and initialization
  • Added explicit handler registration in ready() method
  • Corrected attribute references from 'callback' to proper handler types ('message', 'command')
  • Initialized handler collections during bot preparation
telegram/bot.py
Cleaned up request handling code
  • Removed debug logging of request headers
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

@alimaktabi alimaktabi merged commit 92ce809 into main Oct 26, 2024
2 of 3 checks 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:

  • The webhook registration response (res) is captured but never checked. Consider adding error handling to verify the webhook was set up successfully.
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