lark-send-message-as-bot.vala/SKILL.md

4.1 KiB
Raw Blame History

name description
lark-send-message-as-bot 以 Bot 身份通过飞书发送消息。支持两种目标: (1) 给个人用户发私聊消息(基于 user_id[注意] ./vala_users_list.md中已经记录了vala全员的user_id请首先查询这个文档 (2) 给群组发消息(基于 chat_id 当 agent 需要主动推送消息、通知用户、向群组发送信息时使用。 触发场景发消息、通知某人、推送到群、Bot 发消息、主动消息。

以 Bot 身份发送飞书消息

前置条件

  • Bot 应用的凭证已配置在 /root/.openclaw/credentials/<agent_name>/config.json
  • 目标用户必须在 Bot 应用的可用范围内(开发者后台配置)
  • 目标群必须已将 Bot 添加为群成员
  • Bot 应用需要配置 contact:user.employee_id:readonly 权限。

发送消息

获取 tenant_access_token

所有发送操作前先获取 token

APP_ID=$(jq -r '.apps[0].appId' /root/.openclaw/credentials/<agent_name>/config.json)
APP_SECRET=$(jq -r '.apps[0].appSecret' /root/.openclaw/credentials/<agent_name>/config.json)

TOKEN=$(curl -s -X POST "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal" \
  -H "Content-Type: application/json" \
  -d "{\"app_id\":\"$APP_ID\",\"app_secret\":\"$APP_SECRET\"}" \
  | jq -r '.tenant_access_token')

token 有效期 2 小时,同一批操作复用即可。

给个人用户发消息

[注意]首先尝试基于 ./vala_users_list.md 直接获取对应的 user_id。

使用 receive_id_type=user_idreceive_id 填查询到的 user_id

curl -s -X POST "https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=user_id" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "receive_id": "<user_id>",
    "msg_type": "text",
    "content": "{\"text\":\"消息内容\"}"
  }'

给群组发消息

使用 receive_id_type=chat_idreceive_id 填群 ID

curl -s -X POST "https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=chat_id" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "receive_id": "<chat_id>",
    "msg_type": "text",
    "content": "{\"text\":\"消息内容\"}"
  }'

消息类型

msg_typecontent 对应关系:

msg_type content 格式 示例
text {"text":"纯文本"} 普通文本消息
post {"zh_cn":{"title":"标题","content":[[{"tag":"text","text":"正文"}]]}} 富文本(支持 @、链接、图片)
interactive 卡片 JSON 消息卡片

富文本 post 支持的 tagtexta(链接)、at@用户)、img(图片)。

ID 查询方式

查询 user_id

[注意]首先尝试基于 ./vala_users_list.md 直接获取对应的 user_id。

其他情况: 通过 Bot 所在群的成员列表获取(指定 member_id_type=user_id

LARKSUITE_CLI_CONFIG_DIR=/root/.openclaw/credentials/<agent_name> \
  lark-cli im chat.members get \
  --params '{"chat_id":"<群的chat_id>","member_id_type":"user_id"}' \
  --as bot

返回结果中 member_id 即为 user_id

注意: 不要使用 open_id 跨应用发消息,open_id 是应用级别的,不同 Bot 看到的同一用户 open_id 不同。user_id 是租户级唯一标识,所有 Bot 通用。

查询 chat_id

列出 Bot 所在的所有群:

LARKSUITE_CLI_CONFIG_DIR=/root/.openclaw/credentials/<agent_name> \
  lark-cli im chats list --params '{}' --as bot --page-all

返回结果中每个群的 chat_idname 一一对应。

常见错误

错误码 含义 解决方式
230013 Bot has NO availability to this user 在开发者后台将该用户加入应用的可用范围
230001 Bot is not in the chat 将 Bot 添加到目标群

安全规则

  • 发送前必须确认用户意图,禁止未经确认自动发送
  • 禁止在终端明文输出 appSecretaccessToken
  • <agent_name> 根据当前 agent 身份确定(如 xiaokui、xiaoxi、xiaoban 等),参考 lark-action-as-bot 技能的凭证目录表