Telegram连接服务

通过MTProto API将AI助手连接到Telegram的服务,支持消息发送、聊天管理等功能。

  • by ogk-code
  • Go
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 ogk-code/tg-mcp

使用说明

tg-mcp

MCP服务器,通过MTProto API将人工智能助手连接到Telegram。

内置于 gotd/td 和 去sdk.

特性

  • 认证:使用电话号码和2FA支持登录
  • 消息:发送和阅读消息
  • 聊天:列出对话框,获取最近消息的聊天概述
  • 管理:离开频道/群组,删除聊天记录

可用工具

工具 说明
auth_status 检查授权状态
auth_send_code 向手机发送登录码
auth_submit_code 提交代码(可选2FA密码)
auth_logout 注销并清除会话
list_chats 获取包含未读计数的对话框列表
get_chats_overview 在一个请求中获取所有包含最新消息的聊天记录
get_messages 从特定聊天中获取消息
send_message 向聊天室发送消息
leave_channel 离开频道或群组
delete_chat 删除聊天/对话

安装

先决条件

  1. 转到1.21+
  2. 电报API凭证来自https://my.telegram.org/apps

构建

git clone https://github.com/yourusername/tg-mcp.git
cd tg-mcp
go build -o tg-mcp .

Windows交叉编译:

GOOS=windows GOARCH=amd64 go build -o tg-mcp.exe .

配置

克劳德桌面

增添 claude_desktop_config.json:

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json

视窗: %APPDATA%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "telegram": {
      "command": "/path/to/tg-mcp",
      "args": [],
      "env": {
        "TG_APP_ID": "your_app_id",
        "TG_APP_HASH": "your_app_hash"
      }
    }
  }
}

Windows示例:

{
  "mcpServers": {
    "telegram": {
      "command": "C:\\Users\\YourName\\tg-mcp.exe",
      "args": [],
      "env": {
        "TG_APP_ID": "your_app_id",
        "TG_APP_HASH": "your_app_hash"
      }
    }
  }
}

克劳德代码

增添 ~/.claude.json:

{
  "mcpServers": {
    "telegram": {
      "type": "stdio",
      "command": "/path/to/tg-mcp",
      "env": {
        "TG_APP_ID": "your_app_id",
        "TG_APP_HASH": "your_app_hash"
      }
    }
  }
}

打开WebUI(通过mcpo)

使用 mcpo 公开为OpenAPI:

pip install mcpo
TG_APP_ID=your_id TG_APP_HASH=your_hash mcpo --port 8000 -- /path/to/tg-mcp

然后添加 http://localhost:8000 作为Open WebUI中的OpenAPI服务器。

环境变量

变量 必填 描述
TG_APP_ID 是 电报API ID
TG_APP_HASH 是 电报API哈希
TG_SESSION_FILE 否 自定义会话文件路径(默认: ~/.tg-mcp-session.json)

用法

配置后,重新启动MCP客户端(Claude Desktop、Claude Code等)。

首次授权

  1. 使用 auth_send_code 用你的电话号码
  2. 您将在Telegram中收到一个代码
  3. 使用 auth_submit_code 使用代码(如果启用了2FA,则使用密码)
  4. 完成!会话已保存以备将来使用

示例提示

  • “显示我最近的Telegram聊天记录”
  • “向@username发送一条问候信息”
  • “我的未读邮件是什么?”
  • “离开频道@spamchannel”

安全

  • 会话文件 (~/.tg-mcp-session.json)包含您的身份验证密钥——请将其保密!
  • APP_ID/APP_HASH 不敏感——它们标识应用程序,而不是您的帐户
  • 使用MTProto(用户API),而不是Bot API-完全帐户访问

许可证

麻省理工学院