Telegram

TelegramTools enable an Agent to send messages and media to Telegram chats using the Telegram Bot API. 9 tool methods cover text, photos, documents, video, audio, animations, stickers, editing, and deleting messages.

Prerequisites

1uv pip install -U "kern-ai[telegram]"
1export TELEGRAM_TOKEN=***

Example

The following agent will send a message to a Telegram chat.

1from kern.agent import Agent
2from kern.tools.telegram import TelegramTools
3
4# How to get the token and chat_id:
5# 1. Create a new bot with BotFather on Telegram. https://core.telegram.org/bots/features#creating-a-new-bot
6# 2. Get the token from BotFather.
7# 3. Send a message to the bot.
8# 4. Get the chat_id by going to the URL:
9# https://api.telegram.org/bot/<your-bot-token>/getUpdates
10
11telegram_token = "<enter-your-bot-token>"
12chat_id = "<enter-your-chat-id>"
13
14agent = Agent(
15 name="telegram",
16 tools=[TelegramTools(token=telegram_token, chat_id=chat_id)],
17)
18
19agent.print_response("Send message to telegram chat a paragraph about the moon")

Toolkit Params

ParameterTypeDefaultDescription
chat_idOptional[str]NoneDefault chat ID. Falls back to TELEGRAM_CHAT_ID env var.
tokenOptional[str]NoneBot token. Falls back to TELEGRAM_TOKEN env var.
enable_send_messageboolTrueEnable send_message tool.
enable_send_photoboolFalseEnable send_photo tool.
enable_send_documentboolFalseEnable send_document tool.
enable_send_videoboolFalseEnable send_video tool.
enable_send_audioboolFalseEnable send_audio tool.
enable_send_animationboolFalseEnable send_animation tool (GIFs).
enable_send_stickerboolFalseEnable send_sticker tool.
enable_edit_messageboolFalseEnable edit_message tool.
enable_delete_messageboolFalseEnable delete_message tool.
allboolFalseEnable all tools. Overrides individual flags.

Toolkit Functions

FunctionDescription
send_messageSend a text message to a chat.
send_photoSend a photo (bytes) with optional caption.
send_documentSend a document (bytes) with filename and optional caption.
send_videoSend a video (bytes) with optional caption.
send_audioSend an audio file (bytes) with optional caption and title.
send_animationSend an animation/GIF (bytes) with optional caption.
send_stickerSend a sticker (bytes).
edit_messageEdit a previously sent message by message_id.
delete_messageDelete a message by message_id.

All methods return a JSON string with {"status": "success", "message_id": ...} on success or {"status": "error", "message": ...} on failure.

Developer Resources