Telegram通知服务

一个基于Model Context Protocol (MCP)的服务,用于通过Telegram发送通知。

  • by YardenPorat
  • JavaScript
  • 本地部署
  • 更新于
在 GitHub 查看 YardenPorat/telegram-notifier-mcp

使用说明

Telegram 通知器 MCP 服务器

一个模型上下文协议(MCP)服务器,提供了一个通过Telegram发送通知的工具。该服务器使用TypeScript和Node.js构建。

特点/特性

  • 单工具: send_telegram_notification - 向配置好的Telegram聊天室发送消息
  • 简单设置使用环境变量一次性配置
  • 错误处理配置和API问题的错误信息要清晰明了
  • TypeScript全面类型化,以提升开发体验

先决条件

  1. Node.js版本18或更高版本
  2. Telegram机器人令牌通过(某种方式)创建一个机器人 @BotFather(机器人之父) 在Telegram上
  3. 聊天ID获取你的聊天ID(你可以使用 @用户信息机器人)

安装

# Install dependencies
npm install

# Build the TypeScript code
npm run build

配置

环境变量

设置以下环境变量:

export TELEGRAM_BOT_TOKEN="your_bot_token_here"
export TELEGRAM_CHAT_ID="your_chat_id_here"

你也可以创建一个 .env 在项目根目录下的文件:

TELEGRAM_BOT_TOKEN=your_bot_token_here
TELEGRAM_CHAT_ID=your_chat_id_here

MCP客户端配置

在您的MCP客户端配置中添加(例如,Claude Desktop、Cline):

对于Claude Desktop(~/Library/Application Support/Claude/claude_desktop_config.json):

{
  "mcpServers": {
    "telegram-notifier": {
      "command": "node",
      "args": ["/Users/yarden.porat/projects/yarden/telegram-notifier-mcp/dist/index.js"],
      "env": {
        "TELEGRAM_BOT_TOKEN": "your_bot_token_here",
        "TELEGRAM_CHAT_ID": "your_chat_id_here"
      }
    }
  }
}

对于Cline(VS Code扩展):

在您的Cline MCP设置中添加:

{
  "telegram-notifier": {
    "command": "node",
    "args": ["/Users/yarden.porat/projects/yarden/telegram-notifier-mcp/dist/index.js"],
    "env": {
      "TELEGRAM_BOT_TOKEN": "your_bot_token_here",
      "TELEGRAM_CHAT_ID": "your_chat_id_here"
    }
  }
}

使用方法

配置完成后,MCP服务器提供了一个统一的工具:

send_telegram_notification

参数:

  • message (字符串,必填):要发送到Telegram的消息文本

示例:

Send a telegram notification with the message "Deployment completed successfully! 🚀"

AI助手将使用该工具将您的消息发送到已配置的Telegram聊天群。

发展

构建

npm run build

观看模式

npm run watch

测试

你可以通过设置环境变量并运行以下命令来手动测试服务器:

export TELEGRAM_BOT_TOKEN="your_bot_token_here"
export TELEGRAM_CHAT_ID="your_chat_id_here"
node dist/index.js

或者使用随附的shell脚本:

./send_telegrem_notification.sh "Test message"

项目结构

telegram-notifier-mcp/
├── src/
│   └── index.ts              # Main server implementation
├── dist/                     # Compiled JavaScript (generated)
├── package.json              # Node.js dependencies and scripts
├── tsconfig.json             # TypeScript configuration
├── README.md                 # This file
└── send_telegrem_notification.sh  # Shell script for testing

如何获取Telegram凭证

1. 创建一个Telegram机器人

  1. 打开Telegram并搜索 @BotFather(机器人之父)
  2. 发送 /newbot 命令
  3. 按照说明为你的机器人命名
  4. 复制提供的机器人令牌(格式: 123456789:ABCdefGHIjklMNOpqrsTUVwxyz)

2. 获取您的聊天ID

选项1:使用@userinfobot

  1. 搜索 @用户信息机器人 在Telegram上
  2. 与其开始聊天
  3. 您的聊天ID将会显示

选项2:使用您的机器人

  1. 与您新创建的机器人开始聊天
  2. 向它发送任何信息
  3. 访问: https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates
  4. 寻找 "chat":{"id": 在回应中

故障排除

机器人未发送消息

  • 确保你已经与你的机器人开始了聊天(发送 /start)
  • 验证您的机器人令牌是否正确
  • 确认聊天ID是否正确

MCP服务器无法连接

  • 确保路径为 dist/index.js 是绝对且正确的
  • 验证Node.js是否已安装且可访问
  • 检查构建是否成功完成(npm run build)

许可证

麻省理工学院(MIT)