Skip to content

消息发送

PRX 的消息工具使 Agent 能够通过各种通信渠道发送消息。message_send 是高层抽象工具,自动路由到当前活跃的通信渠道(Telegram、Discord、WhatsApp、iMessage 等);gateway 是底层工具,直接通过 Axum HTTP/WebSocket 网关发送原始消息。

消息工具是 PRX 作为个人 AI 助手的核心能力之一。Agent 不仅能在对话中回复用户,还能主动发送通知、定期报告、提醒和告警。结合定时任务工具,Agent 可以实现"每天早上发送天气预报"、"代码构建失败时发送告警"等自动化消息场景。

message_send 在渠道活跃时自动注册,gatewayall_tools() 模式下始终可用。

配置

消息工具的行为由渠道配置决定。以下是常见渠道的配置示例:

toml
# Telegram 渠道
[channels.telegram]
enabled = true
bot_token = "123456:ABC-DEF..."
allowed_chat_ids = [12345678, -1001234567890]

# Discord 渠道
[channels.discord]
enabled = true
bot_token = "MTIzNDU2..."
guild_id = "123456789"

# 网关配置
[gateway]
host = "0.0.0.0"
port = 3120
websocket_enabled = true
cors_origins = ["http://localhost:3000"]

工具策略控制:

toml
[security.tool_policy.tools]
message_send = "allow"     # 允许消息发送
gateway = "supervised"     # 底层网关操作需审批

使用方法

message_send — 发送消息

发送文本消息到当前渠道:

json
{
  "tool": "message_send",
  "arguments": {
    "content": "构建完成!所有测试通过,共 142 个测试用例。",
    "type": "text"
  }
}

发送到指定渠道和接收者:

json
{
  "tool": "message_send",
  "arguments": {
    "content": "紧急:服务器 CPU 使用率超过 90%",
    "channel": "telegram",
    "recipient": "12345678",
    "type": "text"
  }
}

发送媒体消息:

json
{
  "tool": "message_send",
  "arguments": {
    "type": "media",
    "media_path": "/tmp/screenshot.png",
    "caption": "当前系统监控截图"
  }
}

发送语音消息:

json
{
  "tool": "message_send",
  "arguments": {
    "type": "voice",
    "content": "今天天气晴朗,最高温度 25 度。",
    "voice_provider": "tts"
  }
}

gateway — 底层网关访问

通过 HTTP 网关发送原始消息:

json
{
  "tool": "gateway",
  "arguments": {
    "action": "send",
    "method": "POST",
    "path": "/api/messages",
    "body": {
      "channel": "telegram",
      "chat_id": "12345678",
      "text": "来自 Agent 的消息",
      "parse_mode": "Markdown"
    }
  }
}

通过 WebSocket 推送:

json
{
  "tool": "gateway",
  "arguments": {
    "action": "ws_send",
    "topic": "notifications",
    "data": {
      "type": "alert",
      "message": "磁盘空间不足",
      "level": "warning"
    }
  }
}

参数

message_send 参数

参数类型必填默认值说明
contentstring条件消息内容(textvoice 类型必填)
typestring"text"消息类型:textmediavoice
channelstring当前渠道目标渠道名称(如 telegramdiscord
recipientstring当前对话接收者 ID
media_pathstring条件媒体文件路径(media 类型必填)
captionstring媒体附件的说明文字
parse_modestring文本解析模式(MarkdownHTML
voice_providerstring"tts"语音合成提供商

gateway 参数

参数类型必填说明
actionstring操作类型:send(HTTP)、ws_send(WebSocket)
methodstring条件HTTP 方法(send 操作必填)
pathstring条件API 路径(send 操作必填)
bodyobject请求体
topicstring条件WebSocket 主题(ws_send 操作必填)
dataobject条件WebSocket 数据(ws_send 操作必填)

消息路由

message_send 的自动路由逻辑:

message_send 调用

  ├─ 指定了 channel?
  │     是 → 路由到指定渠道
  │     否 → 使用当前活跃渠道

  ├─ 指定了 recipient?
  │     是 → 发送到指定接收者
  │     否 → 发送到当前对话

  └─ 根据 type 选择发送方式
        text  → 纯文本消息
        media → 文件/图片附件
        voice → TTS 转语音后发送

支持的渠道

渠道标识消息类型
Telegramtelegramtext、media、voice
Discorddiscordtext、media
WhatsAppwhatsapptext、media
WhatsApp Webwhatsapp_webtext、media
iMessageimessagetext、media
Signalsignaltext、media
Matrixmatrixtext、media
Emailemailtext(HTML)、media(附件)
CLIclitext
Larklarktext、media
DingTalkdingtalktext、media

安全性

消息发送控制

消息发送可能泄露 Agent 处理的信息。安全建议:

风险说明缓解措施
信息泄露Agent 可能将敏感信息发送到不当渠道限制可用渠道和接收者
垃圾消息Agent 可能发送过多消息速率限制
社会工程Agent 可能被诱导发送误导性消息监督模式

渠道权限

通过 allowed_chat_ids(Telegram)等渠道级配置限制消息接收者:

toml
[channels.telegram]
allowed_chat_ids = [12345678]  # 仅允许向指定聊天发送

网关安全

gateway 工具提供底层网关访问,安全风险更高。建议:

toml
[security.tool_policy.tools]
gateway = "supervised"    # 底层操作需审批

网关支持 CORS 配置,限制 WebSocket 连接的来源:

toml
[gateway]
cors_origins = ["http://localhost:3000"]

语音消息安全

语音消息涉及 TTS(文字转语音)处理。PRX 自动处理 MP3 生成和 M4A 转换,但需注意:

  • TTS 可能调用外部 API(如 OpenAI TTS)
  • 生成的音频文件存储在临时目录
  • 发送完成后自动清理临时文件

相关文档

Released under the Apache-2.0 License.