playbook/docs/tsl/modules/wechat_message.md

61 lines
2.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 发送微信消息使用说明
## 摘要
- 通过“天软科技服务号”向微信客户端发送模板消息。
- 支持同步/异步发送,异步可用消息 ID 查询状态。
## 前置条件
- 用户关注并绑定天软科技服务号,获得 `userid`
- 给他人发送消息需对方授权其 `userid`
## 核心接口
- `send_wechat_message(userid, url, title, first, remark[, k1..k6])`
- 同步发送;成功返回 `success`,否则返回错误信息。
- `send_wechat_message_async(userid, url, title, first, remark[, k1..k6])`
- 异步发送;返回 16 位消息 ID时间戳 + 随机数)。
- `get_wechat_message_status(userid, username, wechat_message_id)`
- 查询异步发送结果;返回 “成功” 或错误信息。
## 参数说明(模板消息)
- `userid`:绑定账户后获取的用户标识(必需)。
- `url`:详情地址(必需,可为空字符串)。
- `title`:模板消息标题(必需)。
- `first`:副标题(一般需要)。
- `remark`:备注(一般需要)。
- `k1..k6`:模板关键字参数(根据模板类型变化)。常见模板示例:
- 监控报告通知:运行状态/时间
- 系统运行简报:系统名称/简报内容/发布时间
- 告警通知:告警内容/发生时间
## 同步 vs 异步
- 同步:阻塞等待结果,适合紧急通知。
- 异步:立即返回,适合非紧急通知;用 `get_wechat_message_status` 查询。
## 示例TSL
```tsl
// 定义参数
url := "http://tinysoft.com.cn";
user_id := "190F1826267E0EB45658FB81836636A7";
title := "监控报告通知";
first := "运行状态";
keyword_1 := "运行正常";
keyword_2 := "";
DateTimeToString(keyword_2, "YYYY-MM-DD HH:NN:SS", Now());
remark := "如有疑问请与技术人员联系!";
// 同步发送
echo send_wechat_message(user_id, url, title, first, remark, keyword_1, keyword_2);
// 异步发送
wechat_message_id := send_wechat_message_async(user_id, url, title, first, remark, keyword_1, keyword_2);
// 获取结果
echo get_wechat_message_status(user_id, "username", wechat_message_id);
```