Skip to Content

Telegram Bot

In this tutorial, you will learn how to create a Telegram Bot that uses the Alith Python SDK to generate responses to messages. This bot will listen to messages in a Telegram channel and reply.

Note: Although we used Python in this tutorial, you can still use the Alith Rust SDK and Node.js SDK to complete this bot. The advantage of using the Alith Python SDK is that it improves development efficiency while still providing a production-level AI Bot. For example, you can deploy the Bot on AWS Lambda, leveraging the core Rust implementation and minimal Python dependencies, resulting in a much smaller cold start time compared to frameworks like Langchain.

Prerequisites

Before starting, ensure you have the following:

  • OpenAI API Key: Sign up at OpenAI and obtain your API key.
  • Telegram Bot Token: Create a Telegram Bot and retrieve the Bot Token.
  • Python Environment: Install Python (3.8 or higher) and set up a virtual environment.

Install Required Libraries

Install the necessary Python libraries using pip:

python3 -m pip install alith python-telegram-bot
  • alith: Alith Agent SDK for Python
  • python-telegram-bot: Official Telegram SDK for Python.

Create a Telegram Bot

  1. Search for BotFather in Telegram and interact with it.
  2. Send the /newbot command to create a new bot.
  3. Follow the prompts to provide a name and username for your bot, and save the generated Bot Token.

Set Up Environment Variables

Store your API keys and tokens as environment variables for security:

export OPENAI_API_KEY="your-openai-api-key" export TELEGRAM_BOT_TOKEN="your-telegram-bot-token"

Write the Telegram Bot Code

Create a Python script (e.g., tg-bot.py) and add the following code:

import os from telegram import Update from telegram.ext import ( Application, CommandHandler, MessageHandler, filters, CallbackContext, ) from alith import Agent # Initialize Alith Agent agent = Agent( name="Telegram Bot Agent", model="gpt-4", preamble="""You are an advanced AI assistant built by [Alith](https://github.com/0xLazAI/alith).""", ) # Initialize Telegram Bot bot_token = os.getenv("TELEGRAM_BOT_TOKEN") app = Application.builder().token(bot_token).build() # Define message handler async def handle_message(update: Update, context: CallbackContext) -> None: # Use the agent to generate a response response = agent.prompt(update.message.text) # Send the reply back to the Telegram chat await context.bot.send_message(chat_id=update.effective_chat.id, text=response) # Add handlers to the application app.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), handle_message)) # Start the bot if __name__ == "__main__": app.run_polling()

Run the Slack Bot

Run your Python script to start the bot:

python3 tg-bot.py

Test the Bot

  1. Interact with your Telegram Bot.
  2. Send messages, and the bot should reply.

Deploy the Bot

To keep the bot running 24/7, deploy it to a cloud platform like:

Enhance the Bot

Here are some ideas to improve your bot:

  1. Contextual Conversations: Store conversation history to enable multi-turn dialogues.
  2. Error Handling: Add error handling for API failures or invalid inputs.
  3. Custom Commands: Allow users to trigger specific actions (e.g., /ask for questions).
  4. Rate Limiting: Prevent abuse by limiting the number of requests per user.

References

Last updated on