一,企业微信报警步骤
1. 获取 access token
- 以后调用其他 api 必须使用此 token 进行身份验证
- 企业 id
- 应用的 secrect
2. 使用 信息发送的 api 进行发送信息
- access_token
- agetid 这是应用的 id ,并且 id 必须是和上面的 secrect
是成对儿的,也就是同一个应用的 agentid 和 secrect
- 发送目标
- userid
- 组 id
- tag id
- 发送内容
"""
# 1. 获取 access token
get_token_url= 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={ID}&corpsecret={SECRECT}'
get_token_url = get_token_url.format(
ID='更换成你自己的', # 登录网页后,点我的企业
SECRECT='更换成你自己的' # 在网页上点击应用
)
import requests
# 发送请求,并得到 相应对象
r_token = requests.get(url=get_token_url)
# 得到相应的信息 字典的类型
token_dic = r_token.json()
token = token_dic.get('access_token')
# 2. 使用 信息发送的 api 进行发送信息
# 具体消息
text_content = "你的快递已到<a href='https://www.baidu.com'>百度</a>"
# 定义发现的消息体
data_body = {
"touser" : "更换成你自己的", # userid 也就是网页上显示的账户对应的值
"msgtype" : "text", # 消息类型为 纯文本
"agentid" : 1000002, # 应用的 agentid 更换成你自己的
"text" : {
"content" : text_content # 具体的消息内容放在这里
},
"safe": 0 # 是否加密传输: 0 不加密 1 加密
}
# 发送信息的 url
send_msg_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}'
# 使用 POST 方法开始发送信息
r = requests.post(
url=send_msg_url.format(token), # 把获取到的 token 格式化进来
json=data_body # 发送的消息体
)
print(r.status_code) # 返回的状态码
print(r.text) # 返回的内容
网友评论