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)