Create Telegram Bot using Python in few minutes

Siddhpura Amitkumar
3 min readJul 17, 2022

Telegram is also one of the more popular IM platforms today like WhatsApp, it allows you to store messages on the cloud instead of just your device and it boasts good multi-platform support. Also Telegram have more features and building a chatbot on Telegram is fairly simple and requires few steps that take very little time to complete. The chatbot can be integrated in Telegram groups and channels, and it also works on its own.

We are going with very simple example of Telegram Bot, now lets start with one by one steps.

First Step: To create a chatbot on Telegram, you need to contact the BotFather, which is essentially a bot used to create other bots.

To set up a new bot, start the conversation with BotFather (@BotFather).
BotFather will help us in creating the new bot.

  • Start your conversation by pressing the Start button.
  • Create the bot by running /newbot command

Your bot should have two attributes: a name and a username. The name will show up for your bot, while the username will be used for mentions and sharing.

Note: username must be end with _bot like I have selected username simple_tlg_bot

After choosing your bot name and username, you will get a message containing your access token, and you’ll obviously need to save your access token and username for later, as you will be needing them.

Second Step: Python Coding

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

mkdir simple-tlg-bot/
cd simple-tlg-bot/

Firt of all you need to install below telegram module

pip3 install python-telegram-bot

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

from telegram.ext import Updater, CommandHandler, MessageHandler, Filters


def start(update, context):
update.message.reply_text("Welcome to Simple Chat Bot")
update.message.reply_text("You can type any message and you will get same message in response")

def echo_reply(update, context):
update.message.reply_text(update.message.text)

if __name__ == "__main__":
token = "REPLACE_WITH_YOUR_TOEKN"
updater = Updater(token, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start))
dp.add_handler(MessageHandler(Filters.text, echo_reply))
# Start the Bot
updater.start_polling()

Note: Here replace “REPLACE_WITH_YOUR_TOEKN” with your access token which you got with BotFather.

Now just run above code by

python3 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

You can play with this bot here → SimpleTelegramBot

I hope you enjoyed reading the article.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Siddhpura Amitkumar
Siddhpura Amitkumar

Written by Siddhpura Amitkumar

📱 Android Engineer, 📝 Writer, 💻 Open Source Contributor, Techie, IoT, Interactive Projects, ☁ AWS, Google Cloud, Firebase, Python, React.

No responses yet

Write a response