Programming telegram echo bot

In this tutorial, we are going to learn to make a simple telegram echo bot. We assume that you are familiar to basic programming concepts. Follow the steps below:


1. Download and install the latest version of python from Python Official Website.

2. Then install python-telegram-bot using pip by entering pip install python-telegram-bot in your terminal

3. Now you need to create your bot and retrieve your bot token by talking to Telegram BotFather

4. Copy and paste the below snippet to your favorite text editor and run it! Code explanation is provided in comments


# Importing the Bot object
from telegram import Bot  

# Creating an instance of the Bot providing the token
echo_bot = Bot(token="put_the_token_BotFather_gave_you")  
# Offset is used to tell Telegram API to what update_id count we have processed
offset = 0  

# This is an infinite loop because we want the bot to run for ever
while True: 
  # get updates from telegram bot server 
    updates = echo_bot.get_updates(offset=offset)
    # Iterate through updates
    for update in updates: 
        # Increment the offset by one for every update to prevent processing an update more than once
        offset = update.update_id + 1  
        # Echo the received message back to the sender of the message!
        echo_bot.send_message(update.message.chat.id, update.message.text) 

telegram bot programming telegram echo bot

Comments

Comment