TelegramSendTool
and TelegramRetrieveTool
are easy to add.
If you haven’t had a chance to read about how AG2’s Communication Platform agents and tools work, read the overview first.
Installation
Install AG2 with the LLM model provider and Telegram platform extra.Capabilities
TelegramSendTool
can:
- Construct and send a message to the configured channel. If a message is longer than the platforms permitted message length, they will split the message into multiple messages.
TelegramRetrieveTool
can:
- Retrieve the latest X messages from a channel.
- Retrieve messages since a given date.
- Retrieve messages since a given message ID.
- Retrieve a message given its ID.
- Retrieve messages using a search string (and this can be done in combination with the retrieval options above).
Consider
TelegramAgent
if you don’t want to build your agent with these tools, it is a ready-to-go AG2 agent that already incorporates the tools and messaging instructions.Platform configuration
The tools require authentication (API ID and hash) and target Bot/Group/Channel details in order to send/retrieve messages. The tools are different to the Discord and Slack tools in that you can send/retrieve messages to/from a bot channel, a group channel, a channel, or even your own private channel. This is all handled by giving the respective ID to thechat_id
parameter when creating the agent.
Here’s how to establish your API ID and Hash:
If you want to create a bot, which is optional but allows you to send messages to the bot channel:
- In Telegram, search for @BotFather
- Click on @BotFather (make sure it’s the correct one!) and click START
- Message
/newbot
- Give it a name then a username
- You’ll then receive a token for your bot and you see the links below to get the ID for your bot
chat_id
s you can send to:
- Bot chat
- Group chat
- Channel chat
- Private chat (noted in Telegram as
Saved Messages
)
Code example
Let’s add the Telegram Send and Retrieve tools to an agent and have them executed by another agent in as simple 2-agent chat.