Join us on the demo, while our product experts provide a detailed walkthrough of our enterprise platform.

How to make a telegram bot

What is a bot and How to make our own Telegram bot.

Rajan Puri
Rajan Puri
October 20, 2020
3 min read

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 from here

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.
import logging
from telegram.ext import Updater, Filters, CommandHandler, MessageHandler
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',     #take time,level,name
                    level=logging.INFO)
logger = 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.
TOKEN = "ENTER YOUR TOKEN"  
  • After writing the token, we will make some functions that our bot will perform.
def start(bot,update):
    name  = update.message.from_user.first_name  #first name of the user messaging
    reply = "Hi!! {}".format(name)
    bot.send_message(chat_id = update.message.chat_id, text = reply)      #sending message

def help(bot,update):
    reply = "How can I help You"
    bot.send_message(chat_id = update.message.chat_id, text = reply)  #sending message

def echo_text(bot,update):
    reply = update.message.text
    bot.send_message(chat_id = update.message.chat_id, text = reply)

def sticker(bot,update):
    reply = update.message.sticker.file_id
    bot.send_sticker(chat_id = update.message.chat_id, sticker = reply)

def error(bot,update):
    logger.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.
def main():
    updater = Updater(TOKEN)  #take the updates
    dp = updater.dispatcher   #handle the updates

    dp.add_handler(CommandHandler("start", start))
    dp.add_handler(CommandHandler("help", help))
    dp.add_handler(MessageHandler(Filters.text, echo_text))   #if the user sends text
    dp.add_handler(MessageHandler(Filters.sticker, sticker))  #if the user sends sticker
    dp.add_error_handler(error)
    updater.start_polling()
    logger.info("Started...")
    updater.idle()

if __name__=="__main__":
    main()

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.



LoginRadius Docs

Implement Authentication in Minutes

click here

Most Popular Tags

EngineeringJavaScriptNodeJsReactCSSSecurityOAuthAuthenticationGoSocialLogin

Are your customers safe on your application?

According to Forbes, data breaches exposed 4.1 billion records in the first six months of 2019. If this gets you worried, we’ve got your back!

LoginRadius protects your customers’ identities. We provide world-class security for your customers during login, registration, password setup, and any other data touchpoints, and make sure that their data is safe. We do so through by offering open source SDKs, integrations with over 150 third party applications, pre-designed and customizable login interfaces, and best-in-class data security products such as MFA, RBA, and Advanced Password Policies. The platform is already loved by over 3,000 businesses with a monthly reach of 1.17 billion users worldwide.Secure Your Application Now

Rajan Puri

Rajan Puri

Just a beginner trying to learn more and more

View Profile

Try a Modern Authentication Solution

$0/ month

Free Sign Up
  • 7,000 Monthly Active Users
  • 1 Web or Mobile App
  • Email/Password Login
  • 3 Social Login Providers
  • User Data Storage
  • Transactional Email Template
  • Customizable Login Interfaces