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

Bad webhook error on start up #3

Closed
matt17r opened this issue Mar 17, 2021 · 11 comments
Closed

Bad webhook error on start up #3

matt17r opened this issue Mar 17, 2021 · 11 comments

Comments

@matt17r
Copy link

matt17r commented Mar 17, 2021

I just deployed this to Heroku and I think I got everything right but I'm getting an error when the app tries to start... telegram.error.BadRequest: Bad webhook

I've checked the URL as per the first line of output but there's not many other clues... it's running on a free dyno if that makes a difference?

app[web.1]: Running bot in webhook mode. Make sure that this url is correct: https://<app-name>.herokuapp.com/
app[web.1]: Error while bootstrap set webhook: Bad webhook: webhook can be set up only on ports 80, 88, 443 or 8443
app[web.1]: Failed bootstrap phase after 0 retries (Bad webhook: webhook can be set up only on ports 80, 88, 443 or 8443)
app[web.1]: unhandled exception in Bot:1782207609:updater
app[web.1]: Traceback (most recent call last):
app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telegram/ext/updater.py", line 236, in _thread_wrapper
app[web.1]:     target(*args, **kwargs)
app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telegram/ext/updater.py", line 602, in _start_webhook
app[web.1]:     ip_address=ip_address,
app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telegram/ext/updater.py", line 674, in _bootstrap
app[web.1]:     bootstrap_interval,
app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telegram/ext/updater.py", line 530, in _network_loop_retry
app[web.1]:     onerr_cb(telegram_exc)
app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telegram/ext/updater.py", line 652, in bootstrap_onerr_cb
app[web.1]:     raise exc
app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telegram/ext/updater.py", line 516, in _network_loop_retry
app[web.1]:     if not action_cb():
app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telegram/ext/updater.py", line 640, in bootstrap_set_webhook
app[web.1]:     drop_pending_updates=drop_pending_updates,
app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telegram/bot.py", line 127, in decorator
app[web.1]:     result = func(*args, **kwargs)
app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telegram/bot.py", line 2863, in set_webhook
app[web.1]:     result = self._post('setWebhook', data, timeout=timeout, api_kwargs=api_kwargs)
app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telegram/bot.py", line 260, in _post
app[web.1]:     f'{self.base_url}/{endpoint}', data=data, timeout=effective_timeout
app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telegram/utils/request.py", line 354, in post
app[web.1]:     **urlopen_kwargs,
app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/telegram/utils/request.py", line 272, in _request_wrapper
app[web.1]:     raise BadRequest(message)
app[web.1]: telegram.error.BadRequest: Bad webhook: webhook can be set up only on ports 80, 88, 443 or 8443
@matt17r
Copy link
Author

matt17r commented Mar 17, 2021

So, digging a bit deeper, it tries to register a webhook with Telegram and for the port it uses PORT = int(os.environ.get('PORT', '8443')) i.e. the value of the PORT environment variable or 8443 if it's not set.

I ran bash on the console of my dyno and echo $PORT gave me 3160 so obviously Telegram doesn't like that. I tried forcing it to 8443 in Config Vars...

image

...but after doing that echo $PORT gave me 32752.

I found a question on StackOverflow that suggests this is not an uncommon issue and that Heroku maps external ports 80 and 443 to an internal port we can't control but I can't quite figure out how to translate that knowledge into a fix for this problem...?

Edit: It doesn't look like it in the screenshot but the other Config Vars are set correctly... I redacted them but I probably should have put a black block over the top rather than deleting those sections and leaving a transparent gap 🙂

@ohld
Copy link
Owner

ohld commented Mar 17, 2021

Hey!

You don’t need to specify PORT env - Heroku should be able to specify any port it needs. The problem is another

Take a look at the first line of the logs you’ve sent: you just have not setup all env variables. Especially HEROKU_APP_NAME. You also need to specify other variables too and not leave them empty.

@matt17r
Copy link
Author

matt17r commented Mar 17, 2021

Thanks for your response but I have double checked those other settings (see explanation below). I'm fairly sure the port is the problem in my case.

Sorry for the confusion with the screenshot BTW. When I redacted it in Preview it looked like this:

image

...but then the deleted sections just turned white in GutHub. I tried to pre-emptively address it once the post saved - see the "Edit" at the bottom of my comment - but obviously I should have changed the image :)

@matt17r
Copy link
Author

matt17r commented Mar 17, 2021

I just pushed a new build with PORT hardcoded to 443 (changed settings.py:11 to PORT = 443) and now it fails with a slightly different error:

Error while bootstrap set webhook: Bad webhook: ip address 0.0.0.0 is reserved

@ohld
Copy link
Owner

ohld commented Mar 17, 2021

The point Is this I just successfully deployed the bot using that button without any change in the source code of the project. So I have not reproduced your issue yet.

I'd suggest to roll back to the original code without any port change and to try to deploy again with all .env vars provided.

@ohld
Copy link
Owner

ohld commented Mar 17, 2021

Take a look at the first line of your logs:

app[web.1]: Running bot in webhook mode. Make sure that this url is correct: https://<app-name>.herokuapp.com/

You need to have a valid url printed here. See this line: https://github.com/ohld/telegram-support-bot/blob/main/main.py#L21

@matt17r
Copy link
Author

matt17r commented Mar 18, 2021

Thanks for your willingness to help, I realise you have no obligation to help random whiners on the web troubleshoot :)

So, I had already checked the app URL, as I mentioned right at the very beginning:

I've checked the URL as per the first line of output

I've just tried to deploy it again, twice, completely from scratch and it keeps failing in the same way. Steps for me to reproduce:

  1. Click "Deploy to Heroku" on main GitHub page
  2. Fill in all the required settings and deploy
    image
  3. View application logs:
    image

The fact that I can reliably reproduce and it and you can't means there's obviously something else going on... But what?

  • Heroku account type???
    • I'm using free dynos... if you have a paid account perhaps I have extra restrictions?
  • Something to do with the bot I created???
    • Do I need to do anything after /newbot in the BotFather channel?
    • Perhaps I need to do /setjoingroups or something?
  • Something to do with the app name or URL???
    • Perhaps it doesn't like the hyphens?
  • Other???

@ohld
Copy link
Owner

ohld commented Mar 18, 2021

I redeployed the bot using the latest heroku stack (20, not 18 like I did last time). And I received the same error:
image

🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔

@ohld
Copy link
Owner

ohld commented Mar 18, 2021

Probably it is the problem with python-telegram-bot library - they changed the logic behind the webhook support. Let's try to use the older version of the library.

@stikhonchuk
Copy link

Вот такая штука сработала (по мотивам https://github.com/python-telegram-bot/python-telegram-bot/wiki/Webhooks#heroku)

import os

from telegram.ext import Updater

from handlers import setup_dispatcher
from settings import TELEGRAM_TOKEN, HEROKU_APP_NAME

PORT = int(os.environ.get('PORT', '8443'))

# Setup bot handlers
updater = Updater(TELEGRAM_TOKEN)

dp = updater.dispatcher
dp = setup_dispatcher(dp)

# Run bot
if not HEROKU_APP_NAME:  # pooling mode
    print("Can't detect 'HEROKU_APP_NAME' env. Running bot in pooling mode.")
    print("Note: this is not a great way to deploy the bot in Heroku.")

    updater.start_polling()
    updater.idle()

else:  # webhook mode
    print(f"Running bot in webhook mode. Make sure that this url is correct: https://{HEROKU_APP_NAME}.herokuapp.com/")
    updater.start_webhook(
        listen="0.0.0.0",
        port=PORT,
        url_path=TELEGRAM_TOKEN,
        webhook_url=f"https://{HEROKU_APP_NAME}.herokuapp.com/{TELEGRAM_TOKEN}"
    )

#    updater.bot.set_webhook(f"https://{HEROKU_APP_NAME}.herokuapp.com/{TELEGRAM_TOKEN}")
    updater.idle()

@ohld
Copy link
Owner

ohld commented Mar 29, 2021

YES! It worked. Thanks, @stikhonchuk. Final commit: ab9638e

@ohld ohld closed this as completed Mar 29, 2021
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

No branches or pull requests

3 participants