08-企业微信群机器人

  • 阅读: 654
  • 更新: 2022-06-11

企业微信群自定义机器人发送通知
https://developer.work.weixin.qq.com/document/path/91770

1. 代码实现

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import requests


def group_robot(msgtype, content, key, atuser=[]):
    """通过群机器人将消息发送到企业微信群
    msgtype 支持 text 和 markdown 格式
    key 是群机器人 webhook 地址
    atuser 表示@的人员,存储的是 userid 列表
    https://developer.work.weixin.qq.com/document/path/91770
    """
    if key.startswith('http'):
        url = key
    else:
        url = f'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={key}'

    data = {'msgtype': msgtype}
    for au in atuser:
        content += f' <@{au}>'

    data[msgtype] = {'content': content}
    r = requests.post(url, json=data)
    print(r.json())


if __name__ == '__main__':
    msgtype = 'text'
    content = '这是测试消息\nhttps://python-abc.xyz/'
    key = '9f571bb0-8692-abcd-abcd-48e79ed2f748'
    group_robot(msgtype, content, key)

2. 颜色支持

markdown 类型的消息支持 3 种内置字体颜色
对告警消息来说非常有意义

1
2
3
<font color="info">绿色</font>
<font color="comment">灰色</font>
<font color="warning">橙红色</font>

=== 全文完 ===


欢迎加入QQ群:855013471

京公网安备 11011302003970号 京ICP备2022012301号-1