Automatic Reply on Telegram

In this article I am going to show you how you can use Telegram bots to automatic reply on Telegram Groups. Learn to automate Telegram replies based on keywords written in the group. Now, without further ado, let’s get into action.

If you want to know all about Telegram Automation, Tips and Tricks then you can read all the different topics here.

Automatic Reply on Telegram – Prerequisite

In this article I am going to use Python Programming language and Telegram Bot APIs for automating replies on Telegram. In case you are comfortable in any other programming language then feel free to use that as the underlying concept remains the same.

Telegram Bot API – Official Website

Python Official Documents

APIs Required to Send Automatic Reply on Telegram

There are hundreds of official Telegram Bot APIs available, However in this article we are interested in only two Telegram APIs which are mentioned below. First API will be helping us to get latest messages which are being sent in Telegram Group and second API will help you to send any message you want to send in the group.

https://api.telegram.org/bot{token}/getUpdates

and

https://api.telegram.org/bot{}/sendMessage?chat_id={Telegram Group ID}&text={Message Which you want to Send}&parse_mode=HTML

Creating a Bot in Telegram using Botfather

First step is to create a bot in Telegram. Please follow below steps to create a Bot in Telegram.

Step#01 : In Telegram, Search for BotFather

Telegram Bot Father

Step#02 : Click on Start

Step#03: Write /newbot command so that BotFather will start process of creating a Telegram Bot

Step#04: Botfather will ask you what is the name you would like to give to your bot? You can enter name of your choice.

Step#05: Botfather will ask you to write a username for your bot? Usernames must end with _bot . As you can see in the screenshot that I have given the username which was not available, so I have to use another username which is vikas_jha_assistant_bot

Step#06: Great, Now Botfather has created API for you. It also shares a Token Key which you need to use whenever you are running any API using Bot.

Change Bot Privacy to Disable

By default when Telegram bot is created, its privacy is set to Enabled. It means Bot can no longer read any messages sent in a group. So make sure you disable it by running a /setprivacy command in botfather as shown below:

This is important step so make sure you do not miss it.

Disabling Telegrams Bot APIs Privacy

Adding Bot to Group

You will need to add newly created bot to the Group in which you want to automate sending replies.

Writing Python Program

Here is a Python program which you need to start sending reply to anyone who mentions your bot in the group.

import requests


token = '1717896538:AAENVR_3BjYpL3MAoWyAMMqvmzhZZEWvzf8'


base_url = 'https://api.telegram.org/bot{}/getUpdates'.format(token)
resp = requests.get(base_url)
data = resp.json()

for item in data["result"]:
    try:
        if item["message"]["entities"][0]["type"] == "mention":

            chat_id = item["message"]["chat"]["id"]
            user_id = item["message"]["from"]["id"]
            user_name = item["message"]["from"]["username"]
            message = item["message"]["text"]
            
            if "pagi" in message.lower():
                
                text = '''
                <a href="tg://user?id={}">@{}</a>
                Hi, How are you doing? Do let me know if you need any help.

                '''.format(user_id,user_name)
                
                to_url = 'https://api.telegram.org/bot{}/sendMessage?chat_id={}&text={}&parse_mode=HTML'.format(token, chat_id, text)
                resp = requests.get(to_url)
        else:
            pass
            
    except:
        pass
        
        
print("Program ended")

Python Program Explanation

I have explained complete program in this video so I would request you to check out this video here.

Scroll to Top