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

Fix bug #1103 in setting TELEGRAM_WEBHOOK_HOST causing Flood control exceeded error #1934

Closed
wants to merge 1 commit into from

Conversation

alexintech
Copy link
Contributor

@alexintech alexintech commented May 15, 2023

What this PR does

When TELEGRAM_WEBHOOK_HOST sets into database it calls Telegram's setWebhook twice. The first call on field validation goes correctly. But after saving the field LiveSettings.validate_settings() runs, which validates all settings. It calls Telegram's setWebhook again and in that case receives Flood control exceeded error from Telegram.

I have found that LiveSettings.validate_settings has been invented in commit ef37e6a like a replacement for twilio dependent settings.

This PR revalidates only updated dependent settings. So it does not flood Telegram API.

Which issue(s) this PR fixes

#1103

Checklist

  • Unit, integration, and e2e (if applicable) tests updated
  • Documentation added (or pr:no public docs PR label added if not required)
  • CHANGELOG.md updated (or pr:no changelog PR label added if not required)

@alexintech alexintech requested a review from a team May 15, 2023 13:04
Copy link
Member

@vstpme vstpme left a comment

Choose a reason for hiding this comment

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

Hi @alexintech 👋 Thank you for opening a PR!
Looks like this change disables live setting validation for everything except Twilio, and I'm not sure this would be a good idea. Please see LiveSettingsValidator for more info on live settings validation.

Have you considered removing the second call to setWebhook here? The first call to setWebhook is performed on serializer save, so the second call indeed seems to be redundant. Let me know if this makes sense 🙂

@vstpme vstpme requested a review from mderynck May 16, 2023 14:03
@alexintech
Copy link
Contributor Author

Hi @alexintech 👋 Thank you for opening a PR! Looks like this change disables live setting validation for everything except Twilio, and I'm not sure this would be a good idea. Please see LiveSettingsValidator for more info on live settings validation.

Have you considered removing the second call to setWebhook here? The first call to setWebhook is performed on serializer save, so the second call indeed seems to be redundant. Let me know if this makes sense 🙂

Hi, @vadimkerr! Thank you for your attention.

Actually, there are three possible calls to the setWebhook API when updating a live setting.

First case (valid webhook URL entered):

  1. The first call to setWebhook is performed on serializer save as you mentioned. It sets a valid URL to Telegram and clears error field if one exists in the database.
  2. The second call here won't actually call the setWebhook endpoint because it checks here if the webhook URL has already been set.
  3. The second actual call to setWebhook is performed in LiveSettings.validate_settings(). This call results in a Flood control exceeded error, and this error is saved to the database.

Second case (invalid webhook URL entered, for example, https://123.ngrok):

  1. The first call to setWebhook is performed on serializer save. Telegram responds with the error Bad Request: bad webhook: Failed to resolve host: Name or service not known and unsets the webhook URL if one was set before. This error is saved to the database.
  2. The second call to setWebhook happens here after the webhook URL has been unset, and it sends a real request to Telegram. However, the result of this call does not change the error field in the database.
  3. The third call to the setWebhook endpoint happens in LiveSettings.validate_settings(). This call results in a Flood control exceeded error, which overrides the more informative error message from the first step. This means that the actual cause of the error, the invalid webhook URL, is not saved in the database. Instead, the database will contain the Flood control exceeded error, which is not helpful in identifying and resolving the underlying issue.

I have considered removing these lines:

  1. In the first case it doesn't change anything;
  2. In the second case it also doesn't change anything.

Another option I have tried is modifing _check_telegram_webhook_host() adding validation for existing url:

            url = create_engine_url("/telegram/", override_base=telegram_webhook_host)
            bot = Bot(token=live_settings.TELEGRAM_TOKEN)

            webhook_info = bot.get_webhook_info()
            if webhook_info.url == url:
                return

            bot.set_webhook(url)
  1. It actually resolves the issue in the first case;
  2. However, in the second case, it shows the same Flood control exceeded error instead of the more informative Bad Request: bad webhook: Failed to resolve host: Name or service not known error. This happens even with the removal of these lines

In LiveSettingsValidator I see only Twilio settings use multiple settings to execute validation. Currently when I change a setting value, it validates one setting, and then runs validations for all existing settings.

When I update one setting value, basically I'm excepting that only that field and other dependent on it would be validated.
But if you really need to revalidate all settings, currently you need to change some value back and forth. Maybe for that case it would be better to have an extra button in UI Revalidate all settings? I'm not sure if revalidation of all live settings is required after every change.

@alexintech alexintech closed this May 17, 2023
@alexintech alexintech reopened this May 17, 2023
@alexintech alexintech requested a review from vstpme May 19, 2023 04:18
@alexintech
Copy link
Contributor Author

@vadimkerr @mderynck any ideas? 😄

@vstpme
Copy link
Member

vstpme commented Jun 5, 2023

@alexintech please see #2100, multiple calls to live setting validation should be fixed there

vstpme added a commit that referenced this pull request Jun 6, 2023
# What this PR does

Fixes #1103, inspired by
#1934.

Makes sure that:
1. `LiveSettings.validate_settings` is only called once per update
request and not called for any individual LiveSetting instance save.
2. `telegram.Bot.set_webhook` is only called once per request when
changing `TELEGRAM_WEBHOOK_HOST`.


## Which issue(s) this PR fixes

#1103

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
@vstpme
Copy link
Member

vstpme commented Jun 6, 2023

The issue should be fixed in #2100, so I'm closing this PR. @alexintech thank you for the contribution!

@vstpme vstpme closed this Jun 6, 2023
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.

2 participants