Making our First Telegram Bot [Part -2]

feature-image

In this Post we will be discussing the ways of making a bot, and making a simple Bot for ourselves

How to make a Bot

Making a Telegram bot can be as easy as using a Bot Building bots on Telegram or by coding/scripting a Bot for ourselves which is preferable as it gives us a lot of flexibility for making a bot tailored for us.

Which programming Language to use?

You can make a bot in many different programming languages
A few of them are:

  • PHP
  • Node.js
  • Rust
  • Python
  • Ruby
  • Swift
  • Kotlin
  • Java
  • Go

In this tutorial we will be using Python and Specifically the python-telegram-bot library.

Pre-requisites

  • A machine with Python 3.6 or higher installed
  • A Text-Editor like VSCode, Atom or PyCharm
  • A basic understanding of Python Programming Language

Coding/Scripting the Bot

Before we get started let’s get the required libraries installed

pip3 install python-telegram-bot

let’s import all the necessary libraries and add a basic logger

import logging
from telegram import Bot
from telegram.ext import Updater,CommandHandler
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
logger = logging.getLogger()

If we want DEBUG logs we can do the following

logger.setLevel(logging.DEBUG)

Now lets make a instance of Bot in our program with our unique API_KEY passed to it

TOKEN="<-YOUR TOKEN GOES HERE->"
bot = Bot(token=TOKEN)

We need handlers that will handle the commands and messages, the bot receives from the user, let us define all these handlers in the main function, the MessageHandler in our case accepts all text messages from the user andCommandHandler accepts /start command and responds to them.

def main():
    updater=Updater(token=TOKEN,use_context=True)
    dp = updater.dispatcher
    dp.add_handler(CommandHandler('start',start_command))
    dp.add_handler(MessageHandler(filters=Filters.text,callback=echo_message))

Let’s write the callback functions for the MessageHandler and CommandHandler namely echo_message and start_command

def start_command(update,context):
    user_id = update.effective_user.id
    bot.send_message(chat_id=user_id,text="Hey there\nI'm blogtest19\n")

def echo_message(update,context):
    user_id = update.effective_user.id
    message = update.message
    bot.send_message(chat_id=user_id,text=message)

In the above mentioned code update is a instance of Update Class and the effective_user.id attribute is the id of the user who generated the update in our case a message or a /start Command

Now adding the following line to the main function will enable our script to fetch the latest updates from the Telegram Servers with respect to our bot

updater.start_polling()

We can start the script by adding to make sure that the bot works only when this script in particular is run

if __name__ = "__main__":
    main()

You can find this code on my Github Repository
Stay tuned for the next part in this series