Professional Documents
Culture Documents
Create A Telegram Bot Using Python - GeeksforGeeks
Create A Telegram Bot Using Python - GeeksforGeeks
org/create-a-telegram-bot-using-python/
Write & Earn DSA Data Structures Algorithms Interview Preparation Data Science Topic-wise P
In this article, we are going to see how to create a telegram bot using
Python.
Requirements
• A Telegram Account: If you don’t have the Telegram app installed
just download it from the play store. After downloading create an
account using your mobile number just like WhatsApp.
• .python-telegram-bot module: Here we will need a module called
python-telegram-bot, This library provides a pure Python
interface for the Telegram Bot API. It’s compatible with Python
versions 3.6.8+. In addition to the pure API implementation, this
library features a number of high-level classes to make the
development of bots easy and straightforward. These classes are
contained in the “telegram.ext” submodule. For more information,
you can check their official GitHub repo.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
1 of 13 12/7/2022, 4:43 PM
Create a Telegram Bot using Python - GeeksforGeeks https://www.geeksforgeeks.org/create-a-telegram-bot-using-python/
We can install this module via pip and conda with the below
command.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Got It !
2 of 13 12/7/2022, 4:43 PM
Create a Telegram Bot using Python - GeeksforGeeks https://www.geeksforgeeks.org/create-a-telegram-bot-using-python/
Step 3: Give a unique name to your bot. After naming it, Botfather will
ask for its username. Then also give a unique name BUT remember
the username of your bot must end with the bot, like my_bot,
hellobot etc.
Step 4: After giving a unique name and if it gets accepted you will get
a message something like this –
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Got It !
3 of 13 12/7/2022, 4:43 PM
Create a Telegram Bot using Python - GeeksforGeeks https://www.geeksforgeeks.org/create-a-telegram-bot-using-python/
Here the token value will be different for you, we will use this token in
our python code to make changes in our bot and make it just like we
want, and add some commands in it.
Stepwise implement
Python3
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
from telegram.ext.updater import Updater
Got It !
from telegram.update import Update
4 of 13 12/7/2022, 4:43 PM
Create a Telegram Bot using Python - GeeksforGeeks https://www.geeksforgeeks.org/create-a-telegram-bot-using-python/
• Updater: This will contain the API key we got from BotFather to
specify in which bot we are adding functionalities to using our
python code.
• Update: This will invoke every time a bot receives an update i.e.
message or command and will send the user a message.
• CallbackContext: We will not use its functionality directly in our
code but when we will be adding the dispatcher it is required (and
it will work internally)
• CommandHandler: This Handler class is used to handle any
command sent by the user to the bot, a command always starts
with “/” i.e “/start”,”/help” etc.
• MessageHandler: This Handler class is used to handle any normal
message sent by the user to the bot,
• FIlters: This will filter normal text, commands, images, etc from a
sent message.
Start function: It will display the first conversation, you may name it
something else but the message inside it will be sent to the user
whenever they press ‘start’ at the very beginning.
Python3
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
def start(update: Update, context: CallbackContext):
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
update.message.reply_text(
Gotto
"Enter the text you want It !show to the user whenever they start the bot"
5 of 13 12/7/2022, 4:43 PM
Create a Telegram Bot using Python - GeeksforGeeks https://www.geeksforgeeks.org/create-a-telegram-bot-using-python/
Basically, in the start message, you should add something like “Hello
Welcome to the Bot” etc.
Help function: It is basically in this function you should add any kind
of help the user might need, i.e. All the commands your bot
understands, The information related to the bot, etc)
Python3
Python3
def
We useunknown(update: Update,
cookies to ensure you have the best context: CallbackContext):
browsing experience on our website. By using our site, you
update.message.reply_text(
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
"Sorry '%s' is not a valid command" % update.message.text)
Got It !
6 of 13 12/7/2022, 4:43 PM
Create a Telegram Bot using Python - GeeksforGeeks https://www.geeksforgeeks.org/create-a-telegram-bot-using-python/
Here we have added 4 functions one to open Gmail, one for youtube,
one for LinkedIn, and the last one for GeeksforGeeks. These are not
MANDATORY functions, you can add any kind of functions and their
reply_text as you want, these are just for demonstration. Here the
unknown_text function will send the message written inside it
whenever it gets some unknown messages and the unknown
function will Filter out all the unknown commands sent by the user
and reply to the message written inside it.
Python3
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('youtube', youtube_url))
updater.dispatcher.add_handler(CommandHandler('help', help))
updater.dispatcher.add_handler(CommandHandler('linkedin', linkedIn_url))
updater.dispatcher.add_handler(CommandHandler('gmail', gmail_url))
updater.dispatcher.add_handler(CommandHandler('geeks', geeks_url))
updater.dispatcher.add_handler(MessageHandler(Filters.text, unknown))
updater.dispatcher.add_handler(MessageHandler(
# Filters out unknown commands
Filters.command, unknown))
Here each line suggests that whenever a user writes a command i.e.
the first parameter of the CommandHandler in reply the user gets the
message written inside the function mentioned in the next
parameter.
Python3 Got It !
7 of 13 12/7/2022, 4:43 PM
Create a Telegram Bot using Python - GeeksforGeeks https://www.geeksforgeeks.org/create-a-telegram-bot-using-python/
updater.start_polling()
Here whenever we start polling the bot will be active and it will look
for any new message sent by any of the users and if it matches the
command specified there it will reply accordingly.
Python3
We useyoutube_url(update:
def cookies to ensure you have theUpdate,
best browsing experience
context: on our website. By using our site, you
CallbackContext):
acknowledge that you have read and understood our
update.message.reply_text("Youtube Cookie
Link Policy & Privacy Policy
=>\
https://www.youtube.com/")Got It !
8 of 13 12/7/2022, 4:43 PM
Create a Telegram Bot using Python - GeeksforGeeks https://www.geeksforgeeks.org/create-a-telegram-bot-using-python/
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('youtube', youtube_url))
updater.dispatcher.add_handler(CommandHandler('help', help))
updater.dispatcher.add_handler(CommandHandler('linkedin', linkedIn_url))
updater.dispatcher.add_handler(CommandHandler('gmail', gmail_url))
updater.dispatcher.add_handler(CommandHandler('geeks', geeks_url))
updater.dispatcher.add_handler(MessageHandler(Filters.text, unknown))
updater.dispatcher.add_handler(MessageHandler(
Filters.command, unknown)) # Filters out unknown commands
updater.start_polling()
Output:
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Got It !
00:00 01:33
9 of 13 12/7/2022, 4:43 PM
Create a Telegram Bot using Python - GeeksforGeeks https://www.geeksforgeeks.org/create-a-telegram-bot-using-python/
Like 37
Previous Next
Related Articles
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
3.acknowledge
Instagram Bot
that you haveusing Python
read and and
understood InstaPy
our Cookie Policy & Privacy Policy
Got It !
10 of 13 12/7/2022, 4:43 PM
Create a Telegram Bot using Python - GeeksforGeeks https://www.geeksforgeeks.org/create-a-telegram-bot-using-python/
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Got It !
11 of 13 12/7/2022, 4:43 PM
Create a Telegram Bot using Python - GeeksforGeeks https://www.geeksforgeeks.org/create-a-telegram-bot-using-python/
dwaipayan_bandyopadhyay
feedback@geeksforgeeks.org
@dwaipayan_bandyopadhyay
PracticeCopyright
Tags : Policy
python CS Subjects
Courses
Improve Article Report Issue
News Languages
Top News
Python
Technology
Java
Work & Career
CPP
Business
Golang
Finance
C#
Lifestyle
SQL
Knowledge
Kotlin
NodeJS
12 of 13 12/7/2022, 4:43 PM
Create a Telegram Bot using Python - GeeksforGeeks https://www.geeksforgeeks.org/create-a-telegram-bot-using-python/
NodeJS
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Got It !
13 of 13 12/7/2022, 4:43 PM