Part 3: Deploy Telegram bot application on Heroku

Andrew Voronov
4 min readApr 16, 2019

This is the 3-rd part of stories series about developing Telegram bot application from scratch and deploying it in the web. In the 1-st part, I described how to create a Telegram bot. In the 2-d part, I described how to set a webhook and add some basic logic to our bot. In this article, I will loop throw the step by step instruction on how to deploy an application on a remote server. For this purpose, I will use a free of cost Heroku service.

Heroku application setup

  • Register on Heroku
  • Create a new application from the dashboard
  • a) Enter application name and region. I set the app name to ‘tb-tutorial’ and chose region USA
  • b) Add app to a pipeline. For a pipeline, I used the same name as for the app. I chose the default stage — staging

Install Heroku CLI and look at configuration steps:

All next commands I will enter using the terminal. Go to your project directory and login on Heroku:

heroku login

Create a git repo from your project directory(if you haven’t created yet). Link your repo to the Heroku. In my case it is the next command:

heroku git:remote -a tb-tutorial

Add to git all project files and commit them. Uncommitted changes will not be pushed

git add .
git commit -am "some commit message"

Push your code to a Heroku server

git push heroku master

Check logs if the app was successfully built and deployed. In my case, I got an error that STATIC_ROOT variable was not properly configured. I added the next line to the end of the base settings (base.py file):

Listed above command says to collect all static files in the specified directory during the deployment.

Congratulations you have built and deployed the app. The bad news is that it still not working and we need to properly configure it.

Database configuration

In our application we use MongoDB and so we need to create the database and make it available from the internet. For this purpose, I used free of cost MongoDB hosting called mLab.

  • Create new MongoDB deployment from scratch
  • a) Choose Sandbox type. It is free of cost up to 500 MB which is more than enough for our app. Press “Continue”
  • b) Choose the region. Press “Continue”
  • c) Enter the database name. In my case, it is a “tb_tutorial”. Submit order

Now you see your new deployment(deployed database). Click on it and you will see a connection string for the database. In my case, it is a

mongodb://<dbuser>:<dbpassword>@ds038855.mlab.com:27744/tb_tutorial

Create a user and password for your new database.

Now we are ready to set production(production.py) settings. We may put sensitive info like the database user, password and host into the environment variables. Let’s do it

In our production settings file we turn off DEBUG, retrieve db user and db password from the environment. Next, we create a database connection string using our environment variables. I have created a “tb_tutorial” database and so I need to specify its name in the MONGO_CLIENT. We need to add our application address to the ALLOWED_HOSTS variable as in other cases we are not able to receive requests from the clients outside the localhost. Add new files to git, commit changes, push to Heroku server.

Environment variables

Go to Heroku dashboard -> Settings -> Config Vars -> Reveal Config Vars. Set all necessary environment variables and their values: DJANGO_SETTINGS_MODULE (=tb_tutorial.settings.production), MONGO_USER, MONGO_PASSWORD, MONGO_HOST, TUTORIAL_BOT_TOKEN

We need to create a Procfile and set a web process. As it says in Heroku documentation:

The name web is important here. It declares that this process type will be attached to the HTTP routing stack of Heroku, and receive web traffic when deployed.

Heroku settings files

Let's create a Procfile(file with the name Procfile, without extension, in the project root) and insert the next row in it:

We set our web server to be a gunicorn and so we need to install a python package so that our web server can work with a gunicorn. If you want to dig deeper into this question please read about WSGI. We don’t need a gunicorn during the local development, but we will use it during the deployment. Heroku checks the requirements.txt file and installs all necessary packages. That is why I put a gunicorn in the requirements.txt file. But if you download my repo and enter the command:

pip install -r requirements.txt 

than gunicorn package will also be installed. To avoid it developers usually divide requirements.txt file on requirements.txt and requirements_dev.txt files. For the local development, they install only packages for the local usage:

pip install -r requirements_dev.txt

I haven’t divided my requirements.txt file to simplify the development process.

We may also specify which python version we want to use on Heroku using the runtime.txt file. I will use Python 3.6.7. Create a runtime.txt file in the project root directory and enter the next row:

python-3.6.7

Commit changes and push to Heroku server. Now all errors should be solved and our server should work properly.

There is one more thing we need to do: set telegram router to send messages on our new server. In my case it is the next URL:

https://api.telegram.org/bot<token>/setWebhook?url=https://tb-tutorial.herokuapp.com/webhooks/tutorial/

Now your bot should be deployed and work properly. My congratulations!

In case you have errors: set DEBUG variable to True, deploy and review the logs on the dashboard.

Thanks for the reading and good luck!

--

--

Andrew Voronov

I am a Python Developer with more than 5 years of experience. Except programming, I prefer to live an active way of life: football, ping pong, night clubs, etc.