GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Webhooks Webhooks
Poolitzer edited this page
Our examples usually start the bot using First things first: You should have a good reason to switch from polling to a webhook. Don't do it simply because it sounds cool. The general difference between polling and a webhook is:
Let's explain this with a metaphor. Imagine you want to check if you have mail. With (long) polling, you have a post office box and need to check yourself if you have new mail. So you rush to the post office and wait in front of your box all day to see if the postman puts something in there - you only go home for small pee breaks. With a webhook, you have a mailbox right at your home and the postman delivers the mail right to that mailbox. However, that only works if the postman knows your address (the URL). Usually this means you have to run your bot on a server, either a dedicated server or a VPS. Read Where to host Telegram Bots to find a list of options. Make sure you can connect to your server from the public internet, either by IP or domain name. If All communication with the Telegram servers must be encrypted with HTTPS using SSL. With polling, this is taken care of by the Telegram Servers, but if you want to receive updates via a Webhook, you have to take care of it. Telegram will not send you any updates if you don't. There are two ways to do this:
If you don't already have a verified certificate, use a self-signed one. It's easier and there is no disadvantage to it. To create a self-signed SSL certificate using The There actually is a third requirement: a HTTP server to listen for webhook connections. At this point, there are several things to consider, depending on your needs. The However, there is a limitation to this solution. Telegram currently only supports four ports for webhooks: 443, 80, 88 and 8443. As a result, you can only run a maximum of four bots on one domain/IP address with the integrated server. If that's not a problem for you (yet), you can use the code below (or similar) to start your bot with a webhook. The application.run_webhook( listen='0.0.0.0', port=8443, secret_token='ASecretTokenIHaveChangedByNow', key='private.key', cert='cert.pem', webhook_url='https://example.com:8443' )
To overcome the port limitation, you can use a reverse proxy like nginx or haproxy. In this model, a single server application listening on the public IP, the reverse proxy, accepts all webhook requests and forwards them to the correct instance of locally running integrated webhook servers. It also performs the SSL termination, meaning it decrypts the HTTPS connection, so the webhook servers receive the already decrypted traffic. These servers can run on any port, not just the four ports allowed by Telegram, because Telegram only connects to the reverse proxy directly. Depending on the reverse proxy application you (or your hosting provider) are using, the implementation will look a bit different. In the following, there are a few possible setups listed. On Heroku using webhook can be beneficial because it will automatically manage the downtime required. The reverse proxy is set up for you and an environment is created. From this environment you will have to extract the port the bot is supposed to listen on. Heroku manages the SSL on the proxy side, so you don't have to provide the certificate yourself. import os TOKEN = "TOKEN" PORT = int(os.environ.get('PORT', '8443')) # add handlers application.run_webhook( listen="0.0.0.0", port=PORT, secret_token='ASecretTokenIHaveChangedByNow', webhook_url="https://<appname>.herokuapp.com/" ) This is similar to the Heroku approach, just that you set up the reverse proxy yourself. All bots set their Note: Example code to start the bot: application.run_webhook( listen='127.0.0.1', port=5000, url_path='1', secret_token='ASecretTokenIHaveChangedByNow', webhook_url='https://example.com/1', cert='cert.pem' ) When setting up nginx, it is usually a good idea to secure it with an SSL certificate right away (see for example this tutorial). If you've done that, you must not pass your certificate in Example configuration for
In this approach, each bot is assigned their own subdomain. If your server has the domain example.com, you could have the subdomains bot1.example.com, bot2.example.com etc. You will need one certificate for each bot, with the FQDN set for their respective subdomain (or a wildcard certificate). The reverse proxy in this example is Note: For this to work, you need a domain for your server. Example code to start the bot: application.run_webhook( listen='127.0.0.1', port=5000, secret_token='ASecretTokenIHaveChangedByNow', webhook_url='https://bot1.example.com', cert='cert_bot1.pem') ) Example configuration for You don't necessarily have to use the integrated webserver at all. If you choose to go this way, you should not use the The general idea is outlined below and also explained in more detail in this wiki section. from telegram import Bot from telegram.ext import Application application = Application.builder().token('TOKEN').build() # Register handlers here # Get the update_queue from which the application fetches the updates to handle update_queue = application.update_queue start_fetching_updates(update_queue) # Start and run the application async with application: application.start() # when some shutdown mechanism is triggered: application.stop() Here, We have an example using this approach available here. If you don't want to use the long running tasks started by To check if the webhook was accepted by Telegram, you can simply open this link in your browser:
Wiki of
Clone this wiki locally |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |