Skip to main content
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/output
  • ToolCallingAgent: Base agent class for handling tool-based interactions
  • LiteLLMModel: Integration with language models via LiteLLM
  • Custom tools:
    • Composio Weather API (converted using convert_action)
    • Time tool

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

  1. Create a new directory for your project:
  2. Create a virtual environment:
  3. Install the Galadriel framework:

Step 2: Creating a Telegram Bot

  1. Follow the instructions in this Telegram Bot Tutorial to create a new bot and obtain a token.

Step 3: Setting Up Environment Variables

  1. 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

  1. Create a file named agent.py and add the following code:
  2. Create a file named character_agent.py and add the following code:
  3. Create a file named tools.py and add the following code:
  4. Create a file named agent.json and add the following code:

Step 5: Running the Agent

  1. Run the agent.py file:
  2. 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 the ToolCallingAgent to simulate Elon Musk’s persona.
  • The TELEGRAM_SYSTEM_PROMPT template defines the system prompt for the bot, incorporating information from the agent.json file.
  • The execute method handles incoming messages, formats the prompt, and generates a response using the OpenAI GPT-4o model.
  • The Composio and Time tool and used to enhance agent capabilities.

API

Customization

Modifying the Agent’s Persona

  1. Edit the agent.json file to change the agent’s name, system prompt, bio, lore, topics, and style.
  2. Update the TELEGRAM_SYSTEM_PROMPT in character_agent.py to reference the new attributes in agent.json.
  3. Modify the character_json_path variable in the CharacterAgent class to point to the new agent.json file.

Adding New Tools

  1. Create a new Python file for your tool or use one of the available built-in tools from Galadriel or Composio
  2. Import the new tool in agent.py.
  3. Add the new tool to the tools list in the CharacterAgent class.

Running the Agent with Different Clients

  1. Modify the inputs and outputs lists in the AgentRuntime class to use a different client.
  2. 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.