ModelBox Open Source

/

AgentKit
Core Concepts/Tools/Built-in Tools/Telegram Bot Tool

Telegram Bot Tool

Send messages and photos via Telegram Bot API

|

The Telegram Bot tool enables sending messages and photos via the Telegram Bot API.

Installation

import "github.com/model-box/agent-kit/tool/telegram"

Setup

Requirements

  1. Telegram Bot: Create a bot by messaging @BotFather on Telegram
  2. Bot Token: Save the token provided by BotFather
  3. Chat ID: Get the chat ID where you want to send messages

Environment Variables

export TELEGRAM_BOT_TOKEN="your-bot-token"

Getting Chat ID

  1. Add your bot to a chat or channel
  2. Send a message to the bot
  3. Visit: https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates
  4. Look for the chat.id field in the response

Usage

package main

import (
    "context"
    "os"
    
    "github.com/model-box/agent-kit/agent"
    "github.com/model-box/agent-kit/model"
    "github.com/model-box/agent-kit/session"
    "github.com/model-box/agent-kit/tool/telegram"
)

func main() {
    // Create Telegram tools
    telegramTools := telegram.NewTelegramTools()
    
    // Create model
    model := model.Model("gpt-4o").
        SetAPIKey(os.Getenv("OPENAI_API_KEY"))
    
    // Create agent with Telegram tools
    agent := agent.New().
        SetModel(model).
        SetSystemPrompt("You are a helpful assistant that can send Telegram messages.").
        AddTool(telegramTools.SendMessage()).
        AddTool(telegramTools.SendPhoto())
    
    // Create session and run
    session := session.New(agent)
    ctx := context.Background()
    
    response, err := session.Run(ctx, []agent.ChatMessage{
        agent.NewUserMessage("Send a Telegram message to chat -1001234567890 saying 'Hello!'"),
    }, nil)
    
    if err != nil {
        panic(err)
    }
    
    println(response.GetLastMessage().GetContent())
}

Available Tools

telegram_send_message

Send text messages with optional formatting.

ParameterTypeRequiredDescription
chat_idstringYesChat ID or username (@username)
textstringYesMessage text
parse_modestringNo"Markdown", "MarkdownV2", or "HTML"
disable_notificationboolNoSend silently
reply_to_message_idintNoReply to specific message

telegram_send_photo

Send photos with optional captions.

ParameterTypeRequiredDescription
chat_idstringYesChat ID or username (@username)
photostringYesPhoto URL
captionstringNoPhoto caption
parse_modestringNoCaption formatting
disable_notificationboolNoSend silently

Chat ID Formats

  • Private chats: Positive numbers (e.g., 123456789)
  • Groups: Negative numbers (e.g., -123456789)
  • Channels: Negative numbers starting with -100 (e.g., -1001234567890)
  • Usernames: @username format for public chats

Message Formatting

Markdown

*bold text*
_italic text_
[inline URL](http://www.example.com/)
`inline code`

HTML

<b>bold</b>
<i>italic</i>
<a href="http://www.example.com/">inline URL</a>
<code>inline code</code>

Rate Limits

  • 30 messages per second to different chats
  • 1 message per second to the same chat
  • 20 messages per minute to the same group
Edit on GitHub