python调用钉钉 自定义机器人API发送消息
#!/bin/python# -*- coding: utf-8 -*-"""# 发送消息到钉钉"""import jsonimport timeimport sysimport requests# dingidng notificationclass Dingding(object):def __init__(self, access_token, content='', at_people='
·
#!/bin/python
# -*- coding: utf-8 -*-
"""
# 发送消息到钉钉
"""
import json
import time
import sys
import requests
# dingidng notification
class Dingding(object):
def __init__(self, access_token, content='', at_people=''):
self.access_token = access_token
self.content = content
self.at_people = at_people
def get_token(self):
'''
钉钉管理后台 : http://open-dev.dingtalk.com
'''
access_token = 'https://oapi.dingtalk.com/robot/send?access_token=%s' % self.access_token
return access_token
def send_dingding(self):
'''
access_token: 网站
content: 发送的内容
msgtype : 类型
'''
msgtype = 'text'
if self.at_people == '':
values = {
'msgtype': 'text',
msgtype: {
'content': self.content
},
'at': {
'atMobiles': self.at_people,
},
}
else:
values = {
'msgtype': 'text',
msgtype: {
'content': self.content
},
'at': {
'atMobiles': [self.at_people]
},
}
headers = {'Content-Type': 'application/json; charset=UTF-8'}
values = json.dumps(values)
print(self.get_token(), values)
res = requests.post(self.get_token(), values, headers=headers)
errmsg = json.loads(res.text)['errmsg']
if errmsg == 'ok':
return 'ok'
return 'fail: %s' % res.text
content = ''.join(sys.argv[1:])
obj = Dingding('你的机器人Webhook API', content)
obj.send_dingding()
现在新版的机器人都需要自定义词 发送的内容要包括自定义词
python dingding10.py 啊test
点外卖的小伙伴可以能进群
更多推荐
已为社区贡献1条内容
所有评论(0)