Telegram Agent
This tutorial focuses on building a Telegram bot with a specific persona, in this case, Elon Musk. The bot leverages the Galadriel framework to handle message input/output, integrate with a language model, and provide custom tools. This setup allows for interactive and engaging conversations within a Telegram chat.
Features
- 🤖 Responds to messages in Telegram chats with Elon Musk’s personality
- 🌤️ Can check weather conditions using the Composio Weather API
- ⏰ Provides current time information
- 🔄 Maintains context and can engage in multi-turn conversations
Framework Components Used
This example demonstrates several key features of the Galadriel framework:
TelegramClient
: Handles Telegram message input/outputToolCallingAgent
: Base agent class for handling tool-based interactionsLiteLLMModel
: Integration with language models via LiteLLM- Custom tools:
- Composio Weather API (converted using
convert_action
) - Time tool
- Composio Weather API (converted using
Prerequisites
Before you begin, make sure you have the following:
- Python 3.10 or higher
- A Telegram account
- An OpenAI API key
- A Composio API key (optional, for weather information)
Step 1: Setting Up Your Environment
-
Create a new directory for your project:
-
Create a virtual environment:
-
Install the Galadriel framework:
Step 2: Creating a Telegram Bot
- Follow the instructions in this Telegram Bot Tutorial to create a new bot and obtain a token.
Step 3: Setting Up Environment Variables
-
Create a
.env
file in your project directory with the following variables:Replace the placeholder values with your actual API keys and bot token.
Step 4: Implementing the Telegram Agent
-
Create a file named
agent.py
and add the following code: -
Create a file named
character_agent.py
and add the following code: -
Create a file named
tools.py
and add the following code: -
Create a file named
agent.json
and add the following code:
Step 5: Running the Agent
-
Run the
agent.py
file: -
Start a conversation with your bot on Telegram!
Code Explanation
Dependencies
LiteLLMModel
: Used for accessing and managing the OpenAI GPT-4o model.TelegramClient
: Handles communication with the Telegram bot, sending and receiving messages.ToolCallingAgent
: The agent implementation that processes user messages and generates responses.convert_action
: Converts Composio actions into Galadriel tools.
Bot Logic
- The
CharacterAgent
class extends theToolCallingAgent
to simulate Elon Musk’s persona. - The
TELEGRAM_SYSTEM_PROMPT
template defines the system prompt for the bot, incorporating information from theagent.json
file. - The
execute
method handles incoming messages, formats the prompt, and generates a response using the OpenAI GPT-4o model. - The
Composio
andTime
tool and used to enhance agent capabilities.
API
- LiteLLMModel: The language model used for generating responses.
- TelegramClient: Connects to the Telegram bot API.
Customization
Modifying the Agent’s Persona
- Edit the
agent.json
file to change the agent’s name, system prompt, bio, lore, topics, and style. - Update the
TELEGRAM_SYSTEM_PROMPT
incharacter_agent.py
to reference the new attributes inagent.json
. - Modify the
character_json_path
variable in theCharacterAgent
class to point to the newagent.json
file.
Adding New Tools
-
Create a new Python file for your tool or use one of the available built-in tools from Galadriel or Composio
-
Import the new tool in
agent.py
. -
Add the new tool to the
tools
list in theCharacterAgent
class.
Running the Agent with Different Clients
-
Modify the
inputs
andoutputs
lists in theAgentRuntime
class to use a different client. -
Update the client-specific configurations, such as API keys and tokens.
Conclusion
You’ve successfully created a Telegram bot that simulates Elon Musk’s personality using the Galadriel framework. By leveraging different components and customization options, you can create your own unique AI agents that seamlessly integrate with various platforms and provide engaging experiences to users.