Python-telegram-bot: [QUESTION] Is it possible to run a webhook on top of reverse proxy with plain IP and nonstandard port?

Created on 9 May 2020  路  4Comments  路  Source: python-telegram-bot/python-telegram-bot

Hello guys, I've found that if I do like this

...
updater.start_webhook(listen='0.0.0.0', port=7865, url_path=TOKEN)
updater.bot.set_webhook(url=f'https://<server_ip>:7865/{TOKEN}', 
                                       certificate=open('cert.pem', 'rb'))
...

then PTB tells me that only ports 8443, 433, 88, 80 are allowed.

Is that a limitation of the library, or I just don't get the meaning of start_\set_webhook functions?

The goal was to set up nginx server on a port, say 7865, on plain IP (so that FQDN is like just 192.192.192.192) and then redirect the traffic to tokens-paths.

question

All 4 comments

Hi. I'm not an expert on that, but have you seen our wiki page?

Yes, I've read it. There's no examples with plain IP, only domains, but while with that setup (modified from the wiki with ip+port instead of domain):

updater.start_webhook(listen='127.0.0.1', port=5000, url_path='TOKEN1')
updater.bot.set_webhook(webhook_url='https://<server_ip>:1234/TOKEN1',
                        certificate=open('cert.pem', 'rb')

I got error about ports.

This one works fine:

 updater.start_webhook(listen='0.0.0.0',
                            port=443,
                            url_path=TOKEN,
                            key='private.key',
                            cert='cert.pem',
                            webhook_url=f'https://<server_ip>:443/{TOKEN}')

Available ports are a telegram limitation, see https://core.telegram.org/bots/api#setwebhook

You can of course use start_webhook with a different port listening on 127.0.0.1 if you reverse proxy through nginx, as long as nginx listens on one of those available ports. Then you set the webhook to target nginx and you should be good to go.

Also, in that case you might not need to pass url_path on start_webhook, if you set up nginx to just redirect that path to 127.0.0.1:5000, but i'm not 100% sure on the details of how nginx handles this.

Let me know if you have further questions.

Thanks, now all working! (also it turns out that url_path is required for at least nginx case)

updater.start_webhook(listen='127.0.0.1', port=WEBHOOK_PORT, url_path=TOKEN)
updater.bot.set_webhook(url=f'https://<server IP>:<port 80, 88, 443 or 8443>/{TOKEN}',
                certificate=open('cert.pem', 'rb'))

So with that and nginx config similar to wiki's one (but without server_name specified) a bot works supposed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rspadim picture rspadim  路  5Comments

marekyggdrasil picture marekyggdrasil  路  5Comments

jsmnbom picture jsmnbom  路  4Comments

psytron picture psytron  路  4Comments

kmahsi picture kmahsi  路  4Comments