Code your first telegram bot with pyTelegramBotAPI

Alages
Analytics Vidhya
Published in
4 min readApr 17, 2020

Fastest way to deploy your telegram bots online!

When I first got curious on how to create my own telegram bot, I scoured the Internet to find the easiest way to do it. Every Youtuber I chanced upon seemed to do it in their own unique and tedious way, which will seem daunting to newbies.

Fret not, after doing a bit of research, I now introduce a way to get your telegram bot up and running in about an hour.(faster if you are already familiar with programming concepts ie : Python/Git/Heroku)

For this tutorial, we will need(assuming you already have git and python installed locally) :

  • Heroku
  • Python
  • Telegram account

First, you will need to set up a Heroku account, if you do not already have one.
After signing up, proceed to create a new app.

Do not add a pipeline at this point, just fill in the first two and click create app.

After creating the app, we will transition over to telegram. You need to create a bot under BotFather, as follows.

/newbot

Fill in all the information accordingly, and if the bot is successfully created, you should see something like this :

You will need the API Token obtained from BotFather soon

Next, open up a terminal/cmd and run the following commands

virtualenv botenv
source botenv/bin/activate

What this does is to create a virtual environment that separates the dependencies that you install with pip3 later from your other python projects.

pip3 install pyTelegramBotAPI Flask

The above piece of code installs all the dependencies required for our telegram bot to run.

After which, create a bot.py file in the same directory, and paste the following code in the file:

import telebot
import os
from flask import Flask,request
TOKEN = "Insert API Token from BotFather here"
bot = telebot.TeleBot(token=TOKEN)
server = Flask(__name__)

This imports all the dependencies that we installed previously with pip3. We also need to replace the TOKEN variable with the token provided by BotFather. Note that the token should be encapsulated in the open and close inverted commas.

Next, we can paste the following code into the file, in order to define the behaviour of our bot(ie: if we send ‘/help’, the bot replies with ‘Help message’). Take a look at the API for more information!

Notice that the function from lines 9–11 basically makes the bot imitate whatever the user sends to it, play around with this function to achieve your desired output!

The last part of our bot.py file consists of the following lines of code, in order to set up the webhook.

Replace the first part of the url with the name of the Heroku app you created earlier. In my case, the URL will be ‘https://bot-template-alages1.herokuapp.com’

Now, in order for us to mount our code onto Heroku’s servers, it requires this file called a Procfile, which specifies the commands that are executed by the app on startup.

Create ‘Procfile’ in the same directory as bot.py(Note Procfile has no file extension)

Since our python code is located in bot.py, we would copy the following code into Procfile :

web: python3 bot.py

Now, we need a requirements.txt file to let Heroku know what dependencies we have installed in our project. Create a requirements.txt file in the same directory as bot.py and Procfile, and run the following command on terminal/cmd

pip3 freeze > requirements.txt

If you had done this step successfully, requirements.txt should now contain all the dependencies required for our bot to function properly.

Going back to your browser, you should now be on https://dashboard.heroku.com/apps/<app_name>. Navigate to the deploy section of the page, and you should see the following :

For this project, we will be using Heroku CLI to deploy our bot to the server, so go ahead and download and install the Heroku CLI. After which, on your cmd/terminal, and type the command above (heroku login). After you are authenticated, do the following :

git init
heroku git:remote -a <app_name>
git add .
git commit -am "make it better"
git push heroku master

If you had followed all the steps above correctly thus far, you should see the following :

https://<app_name>.herokuapp.com/ deployed to Heroku

And that’s it! You have deployed your bot successfully to Heroku. Now you can go and message your bot on Telegram, and see how it responds!

If you enjoyed the content I shared with you, show some love and leave some comments/claps. Also taking suggestions on what to share next!

--

--

Alages
Analytics Vidhya

I am a Penultimate Year Computer Science student studying at the National University of Singapore, looking to specialise in full-stack development and AI