feat: 优化发送艾特消息
This commit is contained in:
parent
bd88102978
commit
43e6519eb4
@ -40,6 +40,10 @@ argument-hint: "需要 mention;可选 content。mention 可重复传入多个
|
|||||||
"content": {
|
"content": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "要发送的文本内容,可选。只艾特不附加正文时可以为空字符串。"
|
"description": "要发送的文本内容,可选。只艾特不附加正文时可以为空字符串。"
|
||||||
|
},
|
||||||
|
"ended": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": "是否结束当前对话。当 Agent 已经完成艾特和消息发送、要说的话已说完、要做的事已做完时,设置为 true。"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"anyOf": [{ "required": ["mention"] }, { "required": ["mentions"] }],
|
"anyOf": [{ "required": ["mention"] }, { "required": ["mentions"] }],
|
||||||
@ -52,6 +56,7 @@ argument-hint: "需要 mention;可选 content。mention 可重复传入多个
|
|||||||
- `--mention <昵称或备注>` 必填或可重复传入
|
- `--mention <昵称或备注>` 必填或可重复传入
|
||||||
- `--mentions <JSON数组>` 可选,用于一次传入多个昵称或备注
|
- `--mentions <JSON数组>` 可选,用于一次传入多个昵称或备注
|
||||||
- `--content <文本内容>` 可选
|
- `--content <文本内容>` 可选
|
||||||
|
- `--ended` 可选标志。当 Agent 已完成艾特和消息发送、要说的话已说完时传入。
|
||||||
|
|
||||||
## 成员匹配规则
|
## 成员匹配规则
|
||||||
|
|
||||||
@ -73,6 +78,12 @@ argument-hint: "需要 mention;可选 content。mention 可重复传入多个
|
|||||||
python3 scripts/send_mention_message.py --mention '张三' --content '看一下这个'
|
python3 scripts/send_mention_message.py --mention '张三' --content '看一下这个'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
当 Agent 认为任务已完成、对话可以结束时,加上 `--ended` 标志:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python3 scripts/send_mention_message.py --mention '张三' --content '看一下这个' --ended
|
||||||
|
```
|
||||||
|
|
||||||
4. 脚本会查询数据库表 `chat_room_members`,找到当前群内未退群成员的微信 ID。如果数据库没查询到这个人,你可能需要查询你的记忆,看看有没有一个人的别称叫这个名字。
|
4. 脚本会查询数据库表 `chat_room_members`,找到当前群内未退群成员的微信 ID。如果数据库没查询到这个人,你可能需要查询你的记忆,看看有没有一个人的别称叫这个名字。
|
||||||
5. 脚本调用客户端接口 `POST http://127.0.0.1:{ROBOT_WECHAT_CLIENT_PORT}/api/v1/robot/message/send/text` 发送消息,请求体包含 `to_wxid`、`content`、`at`。
|
5. 脚本调用客户端接口 `POST http://127.0.0.1:{ROBOT_WECHAT_CLIENT_PORT}/api/v1/robot/message/send/text` 发送消息,请求体包含 `to_wxid`、`content`、`at`。
|
||||||
|
|
||||||
@ -88,7 +99,14 @@ python3 scripts/send_mention_message.py --mention '张三' --content '看一下
|
|||||||
- 脚本首次运行时会自动创建虚拟环境并安装依赖,无需手动执行。
|
- 脚本首次运行时会自动创建虚拟环境并安装依赖,无需手动执行。
|
||||||
- 如需手动重新安装,可执行:`python3 scripts/bootstrap.py`
|
- 如需手动重新安装,可执行:`python3 scripts/bootstrap.py`
|
||||||
|
|
||||||
|
## ended 行为
|
||||||
|
|
||||||
|
- 当 `--ended` 传入时,脚本在正常输出末尾追加打印独立一行 `ended`。
|
||||||
|
- `ended` 字符串必须位于输出的最末尾,前面不能跟其他字符。
|
||||||
|
- Agent 检测到输出以 `ended` 结尾时,会自动退出 Agent 循环。
|
||||||
|
|
||||||
## 回复要求
|
## 回复要求
|
||||||
|
|
||||||
- 成功时,脚本输出「艾特消息发送成功」,表示消息已通过客户端接口直接发送,无需 AI 智能体再做额外处理。
|
- 成功时,脚本输出「艾特消息发送成功」,表示消息已通过客户端接口直接发送,无需 AI 智能体再做额外处理。
|
||||||
|
- 如果传入 `--ended`,输出末尾会追加 `ended`,Agent 会自动结束对话。
|
||||||
- 失败时,返回脚本输出的具体错误信息。
|
- 失败时,返回脚本输出的具体错误信息。
|
||||||
|
|||||||
@ -126,11 +126,12 @@ def _expand_json_array_values(values: list[str], label: str) -> list[str]:
|
|||||||
return expanded
|
return expanded
|
||||||
|
|
||||||
|
|
||||||
def _parse_cli_params(argv: list[str]) -> tuple[list[str], str]:
|
def _parse_cli_params(argv: list[str]) -> tuple[list[str], str, bool]:
|
||||||
parser = argparse.ArgumentParser(add_help=False)
|
parser = argparse.ArgumentParser(add_help=False)
|
||||||
parser.add_argument("--mention", action="append", default=[])
|
parser.add_argument("--mention", action="append", default=[])
|
||||||
parser.add_argument("--mentions", action="append", default=[])
|
parser.add_argument("--mentions", action="append", default=[])
|
||||||
parser.add_argument("--content", default="")
|
parser.add_argument("--content", default="")
|
||||||
|
parser.add_argument("--ended", action="store_true", default=False)
|
||||||
|
|
||||||
namespace, unknown = parser.parse_known_args(argv)
|
namespace, unknown = parser.parse_known_args(argv)
|
||||||
if unknown:
|
if unknown:
|
||||||
@ -145,7 +146,7 @@ def _parse_cli_params(argv: list[str]) -> tuple[list[str], str]:
|
|||||||
seen.add(key)
|
seen.add(key)
|
||||||
deduped.append(mention)
|
deduped.append(mention)
|
||||||
|
|
||||||
return deduped, namespace.content
|
return deduped, namespace.content, namespace.ended
|
||||||
|
|
||||||
|
|
||||||
def _escape_like(value: str) -> str:
|
def _escape_like(value: str) -> str:
|
||||||
@ -229,7 +230,7 @@ def main() -> int:
|
|||||||
return 1
|
return 1
|
||||||
|
|
||||||
try:
|
try:
|
||||||
mentions, content = _parse_cli_params(sys.argv[1:])
|
mentions, content, ended = _parse_cli_params(sys.argv[1:])
|
||||||
except (ValueError, json.JSONDecodeError) as exc:
|
except (ValueError, json.JSONDecodeError) as exc:
|
||||||
sys.stdout.write(f"参数格式错误: {exc}\n")
|
sys.stdout.write(f"参数格式错误: {exc}\n")
|
||||||
return 1
|
return 1
|
||||||
@ -278,6 +279,8 @@ def main() -> int:
|
|||||||
try:
|
try:
|
||||||
_send_text_message(client_port, chat_room_id, content, at_wechat_ids)
|
_send_text_message(client_port, chat_room_id, content, at_wechat_ids)
|
||||||
sys.stdout.write("艾特消息发送成功\n")
|
sys.stdout.write("艾特消息发送成功\n")
|
||||||
|
if ended:
|
||||||
|
sys.stdout.write("ended\n")
|
||||||
return 0
|
return 0
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
sys.stdout.write(f"艾特消息发送失败: {exc}\n")
|
sys.stdout.write(f"艾特消息发送失败: {exc}\n")
|
||||||
@ -286,7 +289,12 @@ def main() -> int:
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
try:
|
try:
|
||||||
raise SystemExit(main())
|
exit_code = main()
|
||||||
|
if exit_code == 0:
|
||||||
|
# ended may have already been printed above in the success path.
|
||||||
|
# If main() returned non-zero, ended is not printed.
|
||||||
|
pass
|
||||||
|
raise SystemExit(exit_code)
|
||||||
except SystemExit:
|
except SystemExit:
|
||||||
raise
|
raise
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user