python调用钉钉机器人(发送群消息)
本帖最后由 虎尼玛 于 2020-3-4 11:12 编辑最近捣鼓着把一些报警信息发送到移动端,考虑通过itchat或wxpy向微信群发送消息(群里也有相关帖子),无奈微信封了新号的web登陆功能,断了念想.
又试着捣鼓企业微信,实现了相关功能.但是公司已经有了企业微信号(我没管理权限),让其他同事重新加自己建的新号很是麻烦,再次否决.
恰逢部门推钉钉,瞄准群机器人,看了下开发文档,很容易就实现了发送报警消息:victory:,特分享下
先建群机器人 再帖段代码(requests库需要自己安装下)
#!/usr/bin/python
#_*_coding:utf-8_*_
import requests,json
def sendinfo_ding(data):
url = 'xx'#你的机器人webhook地址
program = {
"msgtype": "text",
"text": {"content": data},
}
headers = {'Content-Type': 'application/json'}
f = requests.post(url, data=json.dumps(program), headers=headers)
sendinfo_ding('服务都挂了,速度来嗨!')
https://attach.52pojie.cn//forum/201906/25/144535sihmonmrbmpcbwd3.png?lhttps://attach.52pojie.cn//forum/201906/25/144626aouy63pvslpvupcc.png?l 坐久落花多 发表于 2019-6-25 16:37
企业微信的求分享啊
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
import requests
import json,os
class WeChat_SMS:
def __init__(self):
self.CORPID = 'xx'#企业ID,登陆企业微信,在我的企业-->企业信息里查看
self.CORPSECRET = 'xx'#自建应用,每个自建应用里都有单独的secret
self.AGENTID = 'xx' #应用代码
self.TOUSER = "@all" #@all 全体成员
self.TOPARY = "1" #部门ID
def _get_access_token(self):
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
values = {'corpid': self.CORPID,'corpsecret': self.CORPSECRET,}
req = requests.post(url, params=values)
data = json.loads(req.text)
#print (data)
return data["access_token"]
def get_access_token(self):
try:
with open('access_token.conf', 'r') as f:
t, access_token = f.read().split()
except:
with open('access_token.conf', 'w') as f:
access_token = self._get_access_token()
cur_time = time.time()
f.write('\t'.join())
return access_token
else:
cur_time = time.time()
if 0 < cur_time - float(t) < 7200:
return access_token
else:
with open('access_token.conf', 'w') as f:
access_token = self._get_access_token()
f.write('\t'.join())
return access_token
def send_data(self, msg):
send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + self.get_access_token()
send_values = {
"touser": self.TOUSER,
"msgtype": "text",
"agentid": self.AGENTID,
"text": {
"content": msg
},
"safe": "0"
}
send_msges=(bytes(json.dumps(send_values), 'utf-8'))
respone = requests.post(send_url, send_msges)
respone = respone.json()
return respone["errmsg"]
if __name__ == '__main__':
wx = WeChat_SMS()
wx.send_data(msg="服务都挂了,速度来嗨!") 请教:怎么实现自定义钉钉机器人@自动回复,或者@后调用另一API接口再返回post到钉钉API
不错不错 厉害厉害 不错不错可以直接拿去用:lol jokebox 发表于 2019-6-25 16:04
不错不错可以直接拿去用
免费评分走一波{:1_893:}
企业微信的求分享啊
谢谢,这个好用 不错不错!很好 虎尼玛 发表于 2019-6-25 16:51
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
收到,谢谢
页:
[1]
2