How to Create a Telegram Bot to Auto-Reply Based on Keywords
Setting up a keyword-based auto-responder on Telegram requires a Telegram bot token and a lightweight script to parse incoming messages. This guide provides a direct, technical solution using Python and the pyTelegramBotAPI library to deploy a functional keyword auto-reply bot.
Step 1: Generate a Telegram Bot Token
To interact with the Telegram API, you must register a bot and obtain an authorization token:
- Open Telegram and search for the official @BotFather.
- Send the command
/newbot. - Follow the prompts to assign a name and a unique username for your bot.
- Copy the HTTP API token provided (it looks like
123456789:ABCdefGhIJKlmNoPQRsTUVwxyZ). Keep this token secure.
Step 2: Set Up Your Development Environment
You need Python 3 installed on your system. Install the required Telegram bot library via your terminal or command prompt:
pip install pytelegrambotapi
Step 3: Write the Auto-Reply Script
Create a file named bot.py and paste the following code. This script initializes the bot, defines a dictionary of keywords and their corresponding responses, and scans incoming messages for matches.
import telebot
# Replace with the token you received from BotFather
API_TOKEN = 'YOUR_BOT_TOKEN_HERE'
bot = telebot.TeleBot(API_TOKEN)
# Define your keyword-to-response mapping (use lowercase for case-insensitivity)
KEYWORD_RESPONSES = {
"pricing": "Our pricing starts at $19/month. You can view all plans on our website.",
"support": "For technical support, please open a ticket or email support@example.com.",
"hours": "We are open Monday through Friday, 9:00 AM to 5:00 PM EST.",
"hello": "Hello! How can I assist you today?"
}
# Handler to process all incoming text messages
@bot.message_handler(func=lambda message: True)
def handle_messages(message):
# Convert incoming text to lowercase to ensure case-insensitive matching
incoming_text = message.text.lower()
# Check if any defined keyword is present in the message
for keyword, response in KEYWORD_RESPONSES.items():
if keyword in incoming_text:
# Reply directly to the message containing the keyword
bot.reply_to(message, response)
break # Stop checking after the first match to prevent multiple replies
# Start the bot and keep it running
if __name__ == "__main__":
print("Keyword auto-reply bot is running...")
bot.infinity_polling()
Step 4: Run and Test the Bot
Execute the script from your terminal:
python bot.py
Open Telegram, search for your bot's username, click Start, and send a message containing one of your keywords (e.g., "What is your pricing?"). The bot will instantly reply with the mapped response.
Production Considerations and Limitations
- Uptime: Running the script on your local machine means the bot will stop when your computer goes to sleep or loses internet connection. For 24/7 uptime, deploy the script to a Virtual Private Server (VPS) or a cloud platform like Heroku, Render, or DigitalOcean.
- Rate Limits: Telegram limits bots to sending approximately 30 messages per second to avoid spamming. If your bot is added to high-traffic groups, implement rate-limiting or queuing mechanisms to prevent API blocks.
- Group Chats: If you add the bot to a group chat, ensure you disable "Group Privacy" in @BotFather (via Bot Settings > Group Privacy > Turn off) so the bot can read messages that do not directly tag its username.
Need this done fast? order it on Kwork.
I take on freelance fixes and builds in this area.