Part 2: Deploying Telegram Bot for FREE on Heroku
Following my previous article on how to create a telegram bot, in this article we will learn how to deploy the bot for free on Heroku.
First of all, what is Heroku? 🤔
Quoting from the website.
Heroku is a cloud platform that lets companies build, deliver, monitor and scale apps — we’re the fastest way to go from idea to URL, bypassing all those infrastructure headaches.
In simpler terms, Heroku provides you the platform to host your applications, so you don’t have to run your machine 24 X 7. 😌
Want to read this story later? Save it in Journal.
Since now you have the basic idea now of what Heroku is, let’s start.
Setting up Heroku
- First things first, you will need to create an account on Heroku.
- Install heroku-cli for your specific operating system.
- Login in to your account by running the following command in terminal
heroku login
More about this here - Logging In
Code Changes
As mentioned in the previous article, we were using polling for running the bot. For deploying the bot online, we will be using webhooks so we need to make some code changes to our bot.
Polling vs. Webhook
The general difference between polling and a webhook is:
Polling (via
get_updates
) periodically connects to Telegram's servers to check for new updatesA Webhook is a URL you transmit to Telegram once. Whenever a new update for your bot arrives, Telegram sends that update to the specified URL.
You can learn more about the difference between polling and webhooks here.
We will be adding the set_webhook method in our code. Learn more about it here.
Replace TOKEN with the value you got from BotFather as in the previous article. Replace APP_NAME with the application name you will be creating in the next step. (For some reason I am not able to link within the page, please see “Create the Heroku application” part).
Deploying the application
If you have followed the steps till now, you would be able to create a new application on Heroku. Let’s go back to our directory which contains the bot code if you’re following from the previous article, we had made a directory named echo-bot. Run the following command to change to your bot’s directory.
cd echo-bot/
Now, we will first need to make this directory as a git repository before pushing the code to Heroku. Use the following command to instantiate your current directory as a git repository.
Note: If your repository is already a git repo, you don’t need to run the next command.
git init
Create the Heroku application using the following command, you can use whatever “app_name” you like.
heroku create "app_name"
Now you can use this Heroku application link in the APP_NAME in the code above. (In my case APP_NAME is “https://echotelegrambot.herokuapp.com/”)
Once the app is created, you can check the Remote URL of the application. Run the following command to check the emote information.
git remote -v
Now we need to tell our application what command to run on startup, for that Heroku uses a file called Procfile. You can read more about Procfile here.
Create a file named “Procfile” using the following command.
vim Procfile
Add the following contents to Procfile. Make sure that your file name is bot.py
web: python3 bot.py
Note: This Procfile will only work if your bot is written in python if you’re using any other language check Heroku’s official documentation on how to write the Procfile for that corresponding language.
Now, you need to tell the dependencies to install on the Heroku server. Enter the pipenv shell first.
pipenv shell
pip freeze > requirements.txt
Running this command will create a file named “requirements.txt” which will contain all the dependencies required for running this application.
Now we’re almost done with the Heroku part, you just need to add the files, commit it and push those changes to Heroku. For our changes, we can add all the files in the git repo. So run the following command.
git add .
You can check the status using the following command.
git status
If you were following the tutorial till now the output of git status should like exactly like this.
Once added to git, we need to commit it and push it. Run these commands.
git commit -m <add_your_message>
git push heroku master
Now your application will start its build, you can check the logs by running the following command.
heroku logs -t
And we’ve finally hosted our telegram bot on Heroku. 🥳
Interested in learning about Heroku commands. Check this awesome cheatsheet - Heroku CheatSheet
I hope you learned something new from reading this article!
Checkout my other bots in action:
- BookQuoteBot 📚: Read the best quotes from famous books.
- SplitwizeBot: Uses Splitwise API to list, create and settle the expenses all within Telegram.
If you liked the article do consider buying me a coffee at — https://www.buymeacoffee.com/3Bm2Jaz
📝 Save this story in Journal.
👩💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. Read the Noteworthy in Tech newsletter.