python 根据企业微信群聊机器人 自动向企业微信发送消息
import requests
import json
def robot(key, data):
"""_summary_
Args:
key (_type_): _description_
data (_type_): _description_
"""
# 请求头
headers = {'content-type': 'application/json'}
r = requests.post(key, headers=headers, data=json.dumps(data))
r.encoding = 'utf-8'
return r.text
def bot_push(key, data):
"""发送请求结果
Args:
key (_type_): _description_
data (_type_): _description_
Returns:
_type_: _description_
"""
try:
res = robot(key, data)
return res
except Exception as e:
print(e)
def bot_push_text(key, msg):
"""发送文本
Args:
key (_type_): _description_
msg (_type_): _description_
"""
webhook_data = {
"msgtype": "text",
"text": {
"content": msg
}
}
# 机器人发送
response_data = bot_push(key, webhook_data)
return response_data
if __name__ == '__main__':
webhook_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=?'
a = bot_push_text(webhook_url, "测试")
print(a)
浙公网安备 33010602011771号