Telegram Bot MCP Server - publish, edit, and search messages in Telegram channels
Project description
Telegram Bot MCP Server
A Model Context Protocol (MCP) server that enables AI assistants to publish, edit, search, and manage messages in Telegram channels.
Features
- Publish Text Messages: Post new text messages to Telegram channels with formatting support (Markdown/HTML)
- Publish Photos: Post photos with optional captions to Telegram channels
- Edit Text Messages: Modify existing text-only messages in channels
- Edit Photo Captions: Update captions of existing photo messages
- Delete Messages: Remove messages from channels
- Search Messages: Search through cached messages (local cache)
- Channel Info: Retrieve channel metadata and statistics
Quick Reference
| Task | Message Type | Tool to Use |
|---|---|---|
| Publish a text message | Text | publish_message |
| Publish a photo | Photo | publish_photo |
| Edit a text-only message | Text | edit_message |
| Edit a photo's caption | Photo | edit_message_caption |
| Delete any message | Any | delete_message |
| Search cached messages | Any | search_messages |
| Get channel info | - | get_channel_info |
Important: You cannot use edit_message on photo messages or edit_message_caption on text messages!
Installation
Prerequisites
-
Python 3.10+ is required
-
Create a Telegram Bot:
- Open Telegram and search for @BotFather
- Send
/newbotand follow the instructions - Copy the bot token (format:
123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11)
-
Add Bot to Your Channel:
- Create a Telegram channel or use an existing one
- Add your bot as an administrator to the channel
- Grant the bot permissions:
- Post messages
- Edit messages of others
- Delete messages of others
-
Get Channel ID:
- For public channels, use the username format:
@channelname - For private channels, you need the numeric chat ID (e.g.,
-1001234567890) - You can get this by:
- Adding the bot to the channel
- Sending a message to the channel
- Checking the update using:
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates
- For public channels, use the username format:
Install from Source
# Clone the repository
git clone https://github.com/synteles/telegram-bot-mcp.git
cd telegram-bot-mcp
# Install with pip
pip install -e .
# Or install with uv (recommended)
uv pip install -e .
For Development
# Install with dev dependencies
pip install -e ".[dev]"
# Or with uv
uv pip install -e ".[dev]"
Configuration
Environment Variable
Set your Telegram bot token as an environment variable:
export TELEGRAM_BOT_TOKEN="your-bot-token-here"
Or add it to your .env file:
TELEGRAM_BOT_TOKEN=123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
Claude Desktop Integration
Add this to your Claude Desktop configuration file:
MacOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"telegram-bot": {
"command": "python",
"args": ["-m", "telegram_bot"],
"env": {
"TELEGRAM_BOT_TOKEN": "your-bot-token-here"
}
}
}
}
Or if installed via uv:
{
"mcpServers": {
"telegram-bot": {
"command": "uvx",
"args": ["telegram-bot-mcp"],
"env": {
"TELEGRAM_BOT_TOKEN": "your-bot-token-here"
}
}
}
}
Usage
Once configured, the server will be available to your MCP client (like Claude Desktop). You can ask the AI assistant to:
- "Post a message to @mychannel saying 'Hello from AI!'"
- "Publish a photo from /path/to/image.jpg to @mychannel with caption 'Check this out!'"
- "Edit message 12345 in @mychannel to say 'Updated message'"
- "Search for messages containing 'important' in @mychannel"
- "Delete message 12345 from @mychannel"
- "Get information about @mychannel"
Available Tools
publish_message
Publish a new message to a Telegram channel.
Parameters:
channel_id(str, required): Channel username (@mychannel) or chat IDtext(str, required): Message text to publishparse_mode(str, optional): Text formatting -"Markdown","HTML", or"None". Default:"Markdown"disable_web_page_preview(bool, optional): Disable link previews. Default:Falsedisable_notification(bool, optional): Send silently. Default:False
Returns:
{
"message_id": 12345,
"chat_id": -1001234567890,
"date": "2024-01-15T10:30:00",
"text": "Your message text",
"link": "https://t.me/mychannel/12345"
}
publish_photo
Publish a photo to a Telegram channel with an optional caption.
Parameters:
channel_id(str, required): Channel username (@mychannel) or chat IDphoto(str, required): Photo to send (file path, URL, or file_id)caption(str, optional): Caption text with Markdown/HTML formatting. Max 1024 charactersparse_mode(str, optional): Caption formatting -"Markdown","HTML", or"None". Default:"Markdown"disable_notification(bool, optional): Send silently. Default:False
Returns:
{
"message_id": 12345,
"chat_id": -1001234567890,
"date": "2024-01-15T10:30:00",
"caption": "Photo caption text",
"photo": {
"file_id": "AgACAgIAAxkBAAMCY...",
"file_unique_id": "AQADAgATxxx",
"width": 1280,
"height": 720,
"file_size": 102400
},
"link": "https://t.me/mychannel/12345"
}
edit_message
Edit an existing TEXT-ONLY message in a channel.
IMPORTANT: This only works for text messages. If the message contains a photo, use edit_message_caption instead.
Parameters:
channel_id(str, required): Channel username or chat IDmessage_id(int, required): ID of the message to editnew_text(str, required): New message textparse_mode(str, optional): Text formatting. Default:"Markdown"
edit_message_caption
Edit the caption of an existing PHOTO message in a channel.
IMPORTANT: This only works for messages with media. For text-only messages, use edit_message instead.
Parameters:
channel_id(str, required): Channel username or chat IDmessage_id(int, required): ID of the photo message to editnew_caption(str, required): New caption textparse_mode(str, optional): Caption formatting. Default:"Markdown"
delete_message
Delete a message from a channel.
Parameters:
channel_id(str, required): Channel username or chat IDmessage_id(int, required): ID of the message to delete
search_messages
Search messages in local cache (messages published/edited in current session).
Note: Telegram Bot API doesn't support native message search. This searches locally cached messages.
Parameters:
channel_id(str, required): Channel username or chat IDquery(str, optional): Search query (case-insensitive). If None, returns all cached messageslimit(int, optional): Maximum results to return. Default:10
get_channel_info
Get information about a Telegram channel.
Parameters:
channel_id(str, required): Channel username or chat ID
Returns:
{
"id": -1001234567890,
"title": "My Channel",
"username": "mychannel",
"type": "channel",
"description": "Channel description",
"invite_link": "https://t.me/mychannel",
"member_count": 1234,
"status": "success"
}
Development
Running Tests
# Install dev dependencies
pip install -e ".[dev]"
# Run all tests
pytest
# Run tests with verbose output
pytest -v
# Run tests with coverage
pytest --cov=telegram_bot --cov-report=html
# Run specific test
pytest tests/test_telegram.py::TestTelegramClient::test_publish_message
Code Quality
# Type checking
mypy telegram_bot
# Linting
ruff check .
# Formatting
ruff format .
Running the Server Locally
For testing purposes, you can run the server directly:
# Set your bot token
export TELEGRAM_BOT_TOKEN="your-token"
# Run the server
python -m telegram_bot
The server will start and communicate via stdio, following the MCP protocol.
Architecture
Component Structure
telegram-bot-mcp/
├── main.py # Entry point for the server
├── telegram_bot/
│ ├── __init__.py # Package initialization
│ ├── server.py # FastMCP server implementation with tool decorators
│ └── telegram_bot_client.py # TelegramBotClient wrapper
└── tests/
└── test_telegram.py # Comprehensive tests
Design Patterns
- Singleton Client: Global
TelegramBotClientinstance reused across tool calls - Async-First: All I/O operations are async for better performance
- Error Handling: Graceful error responses with status information
- Local Cache: Message caching for search functionality (since Telegram API doesn't support search)
Limitations
-
Message Types and Editing:
- Text messages: Use
edit_message(won't work on photo messages) - Photo messages: Use
edit_message_caption(won't work on text-only messages) - You cannot change a text message to a photo or vice versa
- The bot can only edit messages it sent itself
- Text messages: Use
-
Message Search: Only searches locally cached messages from current session (Telegram Bot API limitation)
- Messages are cached in-memory during the current session
- Cache is cleared when the server restarts
- You can still edit messages from previous sessions if you know the message_id
-
Message Retrieval: Telegram Bot API doesn't provide a way to fetch arbitrary messages by ID
-
Bot Permissions: Bot must be channel admin with appropriate permissions:
- Post messages
- Edit messages
- Delete messages
-
Edit Time Limit: Telegram has a 48-hour limit for editing messages
-
Rate Limits: Telegram enforces rate limits on bot API calls
Troubleshooting
"Telegram bot token is required" Error
Make sure you've set the TELEGRAM_BOT_TOKEN environment variable or configured it in your MCP client.
"Chat not found" Error
- For public channels, ensure you use
@channelnameformat - For private channels, use numeric chat ID (e.g.,
-1001234567890) - Verify the bot is added to the channel as an admin
"Not enough rights to send messages" Error
The bot needs admin permissions in the channel with:
- Post messages
- Edit messages
- Delete messages
"Message can't be edited" Error
This usually means:
- Wrong edit method for message type: Use
edit_message_captionfor photos,edit_messagefor text - Bot didn't send the message: Bots can only edit messages they sent themselves
- Message is too old: Telegram has a 48-hour limit for editing messages
License
MIT License - See LICENSE file for details
Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
Resources
- Model Context Protocol Documentation
- MCP Python SDK
- Telegram Bot API Documentation
- python-telegram-bot Library
Support
For issues and questions:
- GitHub Issues: Report a bug
- MCP Discord: Join the 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file telegram_bot_mcp-0.1.4.tar.gz.
File metadata
- Download URL: telegram_bot_mcp-0.1.4.tar.gz
- Upload date:
- Size: 92.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
981bab52f219161d03db9d5b2bfb5343cbd260d5b85b8296a1e4e4c03c628773
|
|
| MD5 |
754079035c513545fcbd5a7376c651ed
|
|
| BLAKE2b-256 |
e614dc0e5436c7e18134de801b88d013278d16976b124aab21dd8c3a22fd80b0
|
Provenance
The following attestation bundles were made for telegram_bot_mcp-0.1.4.tar.gz:
Publisher:
publish-packages.yml on Synteles/mcp-servers
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
telegram_bot_mcp-0.1.4.tar.gz -
Subject digest:
981bab52f219161d03db9d5b2bfb5343cbd260d5b85b8296a1e4e4c03c628773 - Sigstore transparency entry: 1206845734
- Sigstore integration time:
-
Permalink:
Synteles/mcp-servers@66736d7904cf5a4a80aa0a2c5b1f859a7efd910b -
Branch / Tag:
refs/tags/telegram-bot-mcp-v0.1.4 - Owner: https://github.com/Synteles
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-packages.yml@66736d7904cf5a4a80aa0a2c5b1f859a7efd910b -
Trigger Event:
push
-
Statement type:
File details
Details for the file telegram_bot_mcp-0.1.4-py3-none-any.whl.
File metadata
- Download URL: telegram_bot_mcp-0.1.4-py3-none-any.whl
- Upload date:
- Size: 22.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ebc7e54dfb2ce0b84a0510133f54b75eb370d7e2cb1bd6ec378ad7c0bc63ae7
|
|
| MD5 |
33baee3ae0b2fb6df6050075124c35cc
|
|
| BLAKE2b-256 |
44d05ed08f71d84ba7b491dd3be7f66ce00400a3f84256489c1b944425e24307
|
Provenance
The following attestation bundles were made for telegram_bot_mcp-0.1.4-py3-none-any.whl:
Publisher:
publish-packages.yml on Synteles/mcp-servers
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
telegram_bot_mcp-0.1.4-py3-none-any.whl -
Subject digest:
3ebc7e54dfb2ce0b84a0510133f54b75eb370d7e2cb1bd6ec378ad7c0bc63ae7 - Sigstore transparency entry: 1206845738
- Sigstore integration time:
-
Permalink:
Synteles/mcp-servers@66736d7904cf5a4a80aa0a2c5b1f859a7efd910b -
Branch / Tag:
refs/tags/telegram-bot-mcp-v0.1.4 - Owner: https://github.com/Synteles
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-packages.yml@66736d7904cf5a4a80aa0a2c5b1f859a7efd910b -
Trigger Event:
push
-
Statement type: