Skip to main content

Comprehensive Telegram Bot API integration for Strands Agents

Project description

Strands Telegram

PyPI version Python versions License: MIT

Comprehensive Telegram Bot API integration for Strands Agents

A complete Telegram Bot API tool that provides seamless integration with Strands Agents, supporting all major Telegram Bot API operations with full flexibility and rich features.

🚀 Features

📨 Message Operations

  • Text Messages: Send rich text with HTML/Markdown formatting
  • Media Support: Photos, videos, audio, documents, voice messages
  • Interactive Elements: Polls, dice games, location sharing, contact cards
  • Message Management: Edit, delete, forward, copy, pin/unpin messages

🎮 Interactive Features

  • Inline Keyboards: Create interactive button menus
  • Custom Keyboards: Reply keyboards with custom layouts
  • Callback Queries: Handle button press events
  • Rich Media: Stickers, animations, and multimedia content

👥 Group & Channel Management

  • User Management: Kick, ban, unban, promote users
  • Permissions: Set chat permissions and restrictions
  • Administration: Manage chat settings, photos, descriptions
  • Member Info: Get chat members, administrators, and statistics

🔧 Bot Management

  • Webhook Support: Set up and manage webhooks
  • Bot Information: Get bot details and configuration
  • Updates Handling: Retrieve and process bot updates
  • File Operations: Upload, download, and manage files

📦 Installation

pip install strands-telegram

🤖 Bot Setup

Before using this tool, you need to create a Telegram bot and get an API token:

1. Create a Telegram Bot

  1. Open Telegram and search for @BotFather
  2. Start a chat with BotFather by clicking /start
  3. Create new bot with /newbot command
  4. Choose a name for your bot (e.g., "My Awesome Bot")
  5. Choose a username for your bot (must end with "bot", e.g., "myawesomebot")
  6. Save the token - BotFather will give you a token like 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11

2. Configure Bot Settings (Optional)

You can customize your bot using BotFather commands:

  • /setdescription - Set bot description
  • /setcommands - Set bot commands menu
  • /setprivacy - Enable/disable privacy mode for groups
  • /setjoingroups - Allow bot to be added to groups

3. Get Your Bot Token

The token from BotFather is your TELEGRAM_BOT_TOKEN. Keep it secure!

4. Set Environment Variable

export TELEGRAM_BOT_TOKEN="your_bot_token_here"

📚 Official Documentation

🛠️ Usage

Quick Start

from strands import Agent
from strands_telegram import telegram

# Create agent with Telegram tool
agent = Agent(tools=[telegram])

# Send a message
agent.tool.telegram(
    action="send_message",
    chat_id="@username",
    text="Hello from Strands! 🚀"
)

Environment Setup

# Set your Telegram Bot Token (get from @BotFather)
export TELEGRAM_BOT_TOKEN="your_bot_token_here"

Advanced Examples

Interactive Keyboard

# Send message with inline keyboard
agent.tool.telegram(
    action="send_message",
    chat_id="123456789",
    text="Choose an option:",
    inline_keyboard=[
        [{"text": "Option 1", "callback_data": "opt1"}],
        [{"text": "Option 2", "callback_data": "opt2"}]
    ]
)

Send Photo with Caption

# Send photo from local file
agent.tool.telegram(
    action="send_photo",
    chat_id="123456789",
    file_path="/path/to/photo.jpg",
    text="Check out this amazing photo! 📸"
)

# Send photo from URL
agent.tool.telegram(
    action="send_photo",
    chat_id="123456789",
    file_url="https://example.com/photo.jpg",
    text="Photo from the web 🌐"
)

Create Poll

# Create a poll
agent.tool.telegram(
    action="send_poll",
    chat_id="123456789",
    question="What's your favorite Strands feature?",
    options=["AI Tools", "Multi-agent", "Workflows", "All of them!"]
)

Group Management

# Promote user to admin
agent.tool.telegram(
    action="promote_chat_member",
    chat_id="-100123456789",
    user_id=987654321
)

# Set chat permissions
agent.tool.telegram(
    action="set_chat_permissions",
    chat_id="-100123456789",
    permissions={
        "can_send_messages": True,
        "can_send_media_messages": False
    }
)

📋 Supported Actions

Message Operations

  • send_message - Send text messages
  • send_photo - Send photos
  • send_video - Send videos
  • send_audio - Send audio files
  • send_document - Send documents
  • send_voice - Send voice messages
  • send_sticker - Send stickers
  • send_location - Send location coordinates
  • send_contact - Send contact information
  • send_poll - Create polls
  • send_dice - Send dice animations

Message Management

  • edit_message - Edit message text
  • delete_message - Delete messages
  • forward_message - Forward messages
  • copy_message - Copy messages
  • pin_message - Pin messages
  • unpin_message - Unpin messages

Bot Information

  • get_me - Get bot information
  • get_updates - Get bot updates
  • get_chat - Get chat information
  • get_chat_member - Get chat member info
  • get_chat_administrators - Get chat administrators

Webhook Management

  • set_webhook - Set webhook URL
  • delete_webhook - Delete webhook
  • get_webhook_info - Get webhook information

Group/Channel Management

  • kick_chat_member - Remove members
  • unban_chat_member - Unban members
  • restrict_chat_member - Restrict members
  • promote_chat_member - Promote to admin
  • set_chat_permissions - Set permissions
  • set_chat_title - Change chat title
  • set_chat_description - Set description
  • leave_chat - Leave chat

File Operations

  • get_file - Get file information
  • download_file - Download files

🔒 Security & Best Practices

Token Security

  • Never hardcode tokens in your code
  • Use environment variables for bot tokens
  • Rotate tokens regularly for production use

Error Handling

The tool provides comprehensive error handling:

result = agent.tool.telegram(action="send_message", chat_id="invalid", text="test")
if result["status"] == "error":
    print(f"Error: {result['content'][0]['text']}")

Rate Limiting

  • Telegram has rate limits (30 messages/second to different chats)
  • The tool handles basic rate limiting
  • For high-volume bots, implement additional rate limiting

🌟 Integration with Strands Agents

This tool is designed to work seamlessly with Strands Agents:

from strands import Agent
from strands_telegram import telegram

# Agent with Telegram capabilities
agent = Agent(
    system_prompt="You are a helpful Telegram bot assistant.",
    tools=[telegram]
)

# Agent can now use Telegram in conversations
response = agent("Send a welcome message to the user")

📚 API Reference

telegram(action, **kwargs)

Parameters:

  • action (str): The Telegram API action to perform
  • chat_id (str|int): Target chat ID or username
  • text (str): Message text content
  • parse_mode (str): Text parsing mode (HTML, Markdown, MarkdownV2)
  • file_path (str): Local file path for uploads
  • file_url (str): URL for remote files
  • inline_keyboard (List[List[Dict]]): Inline keyboard markup
  • Additional parameters specific to each action

Returns:

  • Dict with status and response content

🤝 Contributing

Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.

Development Setup

git clone https://github.com/eraykeskinmac/strands-telegram
cd strands-telegram
pip install -e ".[dev]"

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🔗 Links

📞 Support

  • Create an issue on GitHub for bug reports
  • Join our community discussions for questions
  • Check the Strands Agents SDK documentation for integration help

Made with ❤️ for the Strands Agents community

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

strands_telegram-1.0.2.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

strands_telegram-1.0.2-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file strands_telegram-1.0.2.tar.gz.

File metadata

  • Download URL: strands_telegram-1.0.2.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for strands_telegram-1.0.2.tar.gz
Algorithm Hash digest
SHA256 02bce7446daed39520bc847d2e31ba8ce795ab4ad170f72192744fa4dff10392
MD5 cc686dbfff26c913d6feefee5694051f
BLAKE2b-256 2cc6ab943288a12403a1e40ab47ba3309011935e605ac5db71ddad6509c393fa

See more details on using hashes here.

Provenance

The following attestation bundles were made for strands_telegram-1.0.2.tar.gz:

Publisher: publish.yml on eraykeskinmac/strands-telegram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file strands_telegram-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for strands_telegram-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dc507abb43dec4f7dc6cf3ab591323c5f5869b94ca0a23b595c717b23e1bcc14
MD5 f70e51086b92876347ba7a9e1008a712
BLAKE2b-256 53b825c4ba4103db0a55a4f2f22f7dd1105034100b762f3c2f4e0dbb2138fade

See more details on using hashes here.

Provenance

The following attestation bundles were made for strands_telegram-1.0.2-py3-none-any.whl:

Publisher: publish.yml on eraykeskinmac/strands-telegram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page