How To Reopen Your Heroku App To Add Changes
How to Deploy a Telegram Bot using Heroku for Gratuitous
A Complete Guide Using the python-telegram-bot Library
Running your telegram bot locally suffices for simple applications. However, if you wanted to calibration up your bot and enable others to apply your bot even when you are not running the bot locally, y'all volition demand to go ane footstep further to deploy the bot.
In this tutorial, I will exist going through how to deplo y a telegram bot you take created using the python-telegram-bot library with Heroku. What'due south even improve is that we tin do this completely for costless! Forget about all the fees you incur from other hosting options, Heroku will generally likely suffice for your needs, without you having to pay a single cent nor enter your credit carte du jour details.
In guild to do then, I volition use a elementary starter script taken from the examples in the python-telegram-bot Github repository where the bot merely echos whatever the user sends. Yous may accept a unlike script, but I will show you lot which sections to modify in club to deploy the bot using Heroku.
If you lot desire to go direct to the files needed, head over to this Github repository to download them!
This is our starting point:
Modifying the python-telegram-bot script
Changing from Polling to Webhooks
Firstly, we volition modify how the bot fetches new data. The python-telegram-bot script uses polling instead of webhooks to fetch new data. For uncomplicated testing purposes, polling is sufficient because information technology'southward elementary to implement. However, the disadvantage of polling is that it is inefficient and the data it fetches is always old and never real-time. This is considering polling sends a asking at a predetermined frequency to observe whatever changes in the data, meaning it constantly checks if the data is modified, instead of being "notified" when a change is made to the data.
On the other hand, webhooks act similar push button notifications. Rather than fetching data, webhooks are triggered when the data is modified. This allows for realtime data and also makes it much more efficient because at that place is no need to continuously send requests to check for data.
We offset import os, so set the port number to listen in for the webhook.
import os
PORT = int(bone.environ.get('PORT', 5000))
Adjacent, we modify the following line from
updater.start_polling()
to
updater.start_webhook(heed="0.0.0.0",
port=int(PORT),
url_path=TOKEN)
updater.bot.setWebhook('https://yourherokuappname.herokuapp.com/' + TOKEN)
What this is doing is that it changes the polling method to webhook, listening in to 0.0.0.0 with the port you specified above with the PORT variable. The token refers to the API token of your telegram bot, which should be divers at the top of the code. The next line is to gear up the Webhook with the link to your heroku app, which we will get to next.
With all the changes to the python file, it should look similar to this (with your own Telegram bot token):
Creating your Heroku Webapp — Setting upwards the Directory
We are done with editing the python-telegram-bot script, except for changing the proper name of your heroku app which nosotros volition get into soon. In social club for heroku to recognise the following as a python app, you demand the following files in the same directory:
bot.py
Procfile
requirements.txt
The python code should be within the bot.py file, and I will go through the process to create the Procfile and requirements.txt.
First, modify the directory of your last / command prompt to the aforementioned directory containing your python script. Then, create a requirements.txt file containing the following line:
python-telegram-bot==12.7
This is needed to tell heroku which libraries it needs to install in order to execute the code. Assuming you are using the sample code above, the merely library yous demand is python-telegram-bot, and the version nosotros are using is 12.7. If you lot are importing other libraries to run your python code, exist certain to include the other libraries equally well in the requirements.txt file.
Next, you need a Procfile. The procfile declares the process blazon followed by the command, in the format <process type>: <command>. Here, we are using the spider web procedure type, which receives external HTTP traffic from Heroku'southward routers. The command is python3 bot.py, which tells Heroku to execute the python file every bit you normally would with a python file locally. Then your Procfile should contain the line:
web: python3 bot.py
Make sure that the Procfile does not have any file extension like .txt behind information technology, because it won't piece of work.
Creating your Heroku Webapp
With the three files in the same directory, we will now create the Heroku Webapp.
- Login / create a Heroku account.
- Install the Heroku CLI. If you do non have Git installed, beginning install Git earlier proceeding with the Heroku CLI.
- Once installed, you can apply the heroku command in your terminal / command prompt. Go to the same directory every bit your python files, and type:
heroku login
A new window will be opened in your browser prompting you lot to login, and so but click on the button.
4. One time you are logged in, get back to the control line. Type in
heroku create
to create your new webapp. Hither, heroku volition assign your webapp a name also as the link to your webapp, which should be of the format https://yourherokuappname.herokuapp.com/. Paste the URL into the bot.py code, for the line
updater.bot.setWebhook('https://yourherokuappname.herokuapp.com/' + TOKEN)
With all the changes to your bot.py file, it should look similar to this (with your own Telegram bot token and heroku app name, of course):
five. Next, in your command line, type in
git init
git add together .
git commit -m "first commit"
heroku git:remote -a YourAppName
git button heroku main
The first line creates a new Git repository. The second line and so tells Git that you want to include updates to a particular file in the side by side commit. The third line and so commits the changes. In the fourth line, change "YourAppName" to the name of your heroku app. Lastly, the fifth line pushes everything to the server.
You should then encounter the post-obit letters:
In detail, it will say that a Python app is detected and information technology will install the required libraries in the requirements.txt file using pip. And so, it will read the Procfile which specifies that the bot.py file is to be executed.
And then that's it! Once it finishes executing, you can simply caput over to Telegram and bulletin /offset to your bot. It should now deport as you expect it to.
Since you are using the gratis programme on heroku, the bot will slumber after 30 minutes of inactivity. And so do expect the bot to accept a few seconds to respond to your /kickoff if you lot are using it more than than xxx minutes after it was previously used. Other than that, the bot will respond most instantaneously~
What to do if your Bot stops responding
I've noticed that the bot stops responding afterward about 24 hours of inactivity (because nosotros are using the free version of Heroku), so if you desire to "jolt" the bot awake, i way is to brand a change to one of the files (eg. changing the python3 in the procfile to python and vice versa) and then committing the changes with the lines below:
git add .
git commit -m "changing python3 to python in Procfile"
git push heroku master
You should see once more see the letters about a Python app being detected and one time it finishes executing, your bot should revive now!
Source: https://towardsdatascience.com/how-to-deploy-a-telegram-bot-using-heroku-for-free-9436f89575d2
Posted by: majorreeld1968.blogspot.com
0 Response to "How To Reopen Your Heroku App To Add Changes"
Post a Comment