Create a Telegram Bot using Python (original) (raw)

Last Updated : 11 Oct, 2021

In this article, we are going to see how to create a telegram bot using Python.

In recent times Telegram has become one of the most used messaging and content sharing platforms, it has no file sharing limit like Whatsapp and it comes with some preinstalled bots one can use in any channels (groups in case of whatsapp) to control the behavior or filter the spam messages sent by users.

Requirements

Installation of the module

We can install this module via pip and conda with the below command.

installing via pip

pip install python-telegram-bot

installing via conda

conda install -c conda-forge python-telegram-bot

Steps to create your first bot

Step 1: After opening an account on Telegram, in the search bar at the top search for “BotFather”

Step 2: Click on the ‘BotFather’ (first result) and type /newbot

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 –

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

Step 1: Importing required libraries

Python3

from telegram.ext.updater import Updater

from telegram.update import Update

from telegram.ext.callbackcontext import CallbackContext

from telegram.ext.commandhandler import CommandHandler

from telegram.ext.messagehandler import MessageHandler

from telegram.ext.filters import Filters

Brief usage of the functions we are importing:

Step 2: Define functions for operation

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

updater = Updater( "your_own_API_Token got from BotFather" ,

`` use_context = True )

def start(update: Update, context: CallbackContext):

`` update.message.reply_text(

`` "Enter the text you want to show to the user whenever they start the bot" )

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

def help (update: Update, context: CallbackContext):

`` update.message.reply_text( "Your Message" )

Adding some more functionalities to the Bot.

Python3

def gmail_url(update: Update, context: CallbackContext):

`` update.message.reply_text( "gmail link here" )

def youtube_url(update: Update, context: CallbackContext):

`` update.message.reply_text( "youtube link" )

def linkedIn_url(update: Update, context: CallbackContext):

`` update.message.reply_text( "Your linkedin profile url" )

def geeks_url(update: Update, context: CallbackContext):

`` update.message.reply_text( "GeeksforGeeks url here" )

def unknown_text(update: Update, context: CallbackContext):

`` update.message.reply_text(

`` "Sorry I can't recognize you , you said '%s'" % update.message.text)

def unknown(update: Update, context: CallbackContext):

`` update.message.reply_text(

`` "Sorry '%s' is not a valid command" % update.message.text)

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.

Step 3: Adding the Handlers to handle our messages and commands

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.command, unknown))

updater.dispatcher.add_handler(MessageHandler(Filters.text, unknown_text))

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.

Step 4: Running the bot

Python3

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.

Below is the full implementation:

Python3

from telegram.ext.updater import Updater

from telegram.update import Update

from telegram.ext.callbackcontext import CallbackContext

from telegram.ext.commandhandler import CommandHandler

from telegram.ext.messagehandler import MessageHandler

from telegram.ext.filters import Filters

updater = Updater( "your_own_API_Token got from BotFather" ,

`` use_context = True )

def start(update: Update, context: CallbackContext):

`` update.message.reply_text(

`` "Hello sir, Welcome to the Bot.Please write\

`` / help to see the commands available.")

def help (update: Update, context: CallbackContext):

`` update.message.reply_text(

`` )

def gmail_url(update: Update, context: CallbackContext):

`` update.message.reply_text(

`` "Your gmail link here (I am not \

`` giving mine one for security reasons)")

def youtube_url(update: Update, context: CallbackContext):

`` update.message.reply_text("Youtube Link = >\

`` https: / / www.youtube.com / ")

def linkedIn_url(update: Update, context: CallbackContext):

`` update.message.reply_text(

`` "LinkedIn URL = > \

`` https: / / www.linkedin.com / in / dwaipayan - bandyopadhyay - 007a / ")

def geeks_url(update: Update, context: CallbackContext):

`` update.message.reply_text(

def unknown(update: Update, context: CallbackContext):

`` update.message.reply_text(

`` "Sorry '%s' is not a valid command" % update.message.text)

def unknown_text(update: Update, context: CallbackContext):

`` update.message.reply_text(

`` "Sorry I can't recognize you , you said '%s'" % update.message.text)

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))

updater.dispatcher.add_handler(MessageHandler(Filters.text, unknown_text))

updater.start_polling()

Output:

https://media.geeksforgeeks.org/wp-content/uploads/20211004190138/2021-10-04-18-56-30.mp4