How to make a telegram bot

What is a bot and How to make our own Telegram bot.
profile
Rajan PuriFirst published: 2020-10-20Last updated: 2025-06-25
how-to-make-telegram-bot

What is Bot?

According to Wikipedia, It is a software application that runs automated tasks (scripts) over the Internet. In other terms, It is a program written to mimic human behaviour in order to perform some tasks. Bots take input and do simple and repetitive tasks, which is much faster than humans.

There are two types of bots:

  • Good Bots: Bots that are beneficial to organizations as well as individuals such as ChatBots, Social Media bots, etc
  • Bad Bots: These are the bots that are used to perform malicious activities such as Scraping and Spamming.

In this, We will make Telegram Chatbot, which would send a copy of the input that the user has sent.

Making our Telegram Chatbot

We will be using Python language to make the bot and will be using Telegram package for our bot. You can know more about the telegram package

Firstly, we will generate our telegram token. Below are the steps to follow to generate your own token.

  1. Search BotFather on Telegram.
  2. Type /start to get started.
  3. Type /newbot to get a bot.
  4. Enter your Bot name and unique Username, which should end with the bot.
  5. Then, you would get your Bot token.

After generating our token, we will make a python program to create a Telegram bot that will send the Text, Emojis, and Stickers similar to the user's input.

  • Importing libraries required.
1import logging
2from telegram.ext import Updater, Filters, CommandHandler, MessageHandler
1logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',     #take time,level,name
2                    level=logging.INFO)
3logger = logging.getLogger(__name__)

This script does a basic configuration for the logging system. It takes time, level, and name.

  • Now, we will make a variable Token, which will take your Telegram token.
1TOKEN = "ENTER YOUR TOKEN"
  • After writing the token, we will make some functions that our bot will perform.
1def start(bot,update):
2    name  = update.message.from_user.first_name  #first name of the user messaging
3    reply = "Hi!! {}".format(name)
4    bot.send_message(chat_id = update.message.chat_id, text = reply)      #sending message
5def help(bot,update):
6reply = "How can I help You"
7bot.send_message(chat_id = update.message.chat_id, text = reply)  #sending message
8def echo_text(bot,update):
9reply = update.message.text
10bot.send_message(chat_id = update.message.chat_id, text = reply)
11def sticker(bot,update):
12reply = update.message.sticker.file_id
13bot.send_sticker(chat_id = update.message.chat_id, sticker = reply)
14def error(bot,update):
15logger.error("Shit!! Update {} caused error {}".format(update,update.error))

start function would be taking the user's input and would be sending, Hi, with the user's name.

help function will prompt the message of how can I help you.

echo_text and sticker function will send the same message or the stickers which the user has sent.

error function would be printing the error message on the command prompt.

  • Finally, we will make the main function, which would be executed first on running the program. It will take the updates and handle the updates.
1def main():
2    updater = Updater(TOKEN)  #take the updates
3    dp = updater.dispatcher   #handle the updates
4dp.add_handler(CommandHandler("start", start))
5dp.add_handler(CommandHandler("help", help))
6dp.add_handler(MessageHandler(Filters.text, echo_text))   #if the user sends text
7dp.add_handler(MessageHandler(Filters.sticker, sticker))  #if the user sends sticker
8dp.add_error_handler(error)
9updater.start_polling()
10logger.info("Started...")
11updater.idle()
12
13if name=="main":
14main()

Save the program and execute it to check the working of the telegram bot on telegram.

You can get the source code to make the Telegram bot from this Github Repository Bot :robot:. You can also make some changes to the code to make the bot do cool stuff such as Getting News, Articles, or Movies recommendations.