Part 1: How to create a Telegram Bot in Python in under 10 minutes

Karan Deep Batra
3 min readJun 7, 2020

In this short tutorial, I will walk you through the steps for creating your own Telegram bot in python right from scratch.

Building a Telegram Bot

Let us build a Telegram Bot that echoes the messages that we send to it. In the next part, we will learn how to deploy the bot on websites like Heroku.

Step 1: Set up your Bot’s profile

To set up a new bot, start the conversation with BotFather (@BotFather).

BotFather will help us in creating the new bot.

  • Search for @botfather in Telegram.
Search @botfather
  • Start your conversation by pressing the Start button.

Want to read this story later? Save it in Journal.

Start conversation with BotFather
  • Create the bot by running /newbot command
Run newbot command
  • Enter the Display Name and User Name for the bot.
Enter display name and user name of the bot
  • BotFather will send you a message with the token
Save the token sent by BotFather

DISCLAIMER — Keep access token of the bot securely. Anyone with your token can manipulate this bot.

Step 2: Coding the bot

Open up the terminal and start by creating a new directory first.

mkdir echo-bot/
cd echo-bot/

We will be using pipenv virtual environment. Make sure that you have pipenv installed in your system.

Pipenv is a dependency manager for Python projects.

We will be using python-telegram-bot package for interacting with Telegram API. Install the package using the following command.

pipenv install python-telegram-bot

Create a new file bot.py and paste the following code in it.

echobot.py

Replace “TOKEN” on line 56 with the token that you got from the BotFather earlier.

This code uses polling approach to check for messages and will reply to every message it receives with the same message. You can read more about how python-telegram-bot works here - Coding your first bot

Run the bot using

pipenv run python bot.py

Voilà! We’re done 😄

I bet this would have taken you less than 10 minutes to get started with your first bot.

See the 🤖 in action

Play with the bot here - EchoBot

I hope you enjoyed reading the article. Have any bot ideas to share? Comment below!

Checkout my other bots in action:

  1. BookQuoteBot 📚: Read the best quotes from famous books.
  2. SplitwizeBot: Uses Splitwise API to list, create and settle the expenses all within Telegram.

Read part 2 about how to deploy the bot on Heroku.

Part 2 - Deploying Telegram Bot for FREE on Heroku

Want to create an MVP but struggling to get the data? Want help in generating leads? IndieData provides scraped databases to solve all your data extraction problems. Save hours and efforts of scraping data using IndieData!

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.

--

--

Karan Deep Batra

Engineering @google. Interested in tech, startups and fitness.