import json
import requests
class Dingding(object):
"""
send message to dingding webhook robot
"""
def __init__(self, access_token):
"""
:param access_token: dingtalk webhook access_token
"""
self.access_token = self.__parse_token(access_token)
self.__headers = {"Content-Type": "application/json; charset=utf-8"}
self.dingtalk_webhook = "https://oapi.dingtalk.com/robot/send?access_token=%s" % self.access_token
def __parse_token(self, access_token):
"""
parse case token, access token must be 64 characters
:param access_token: dingtalk webhook access_token
:return: access_token
"""
if len(access_token) != 64:
raise ValueError("invalid access token")
return access_token
def __do_request(self, data):
"""
send request
:param data: dataa
:return: like {"errcode": 0, "errmsg":"ok"}
"""
try:
response = requests.post(url=self.dingtalk_webhook, data=json.dumps(data),
headers=self.__headers)
print("发送请求成功...")
return response.text
except Exception:
raise
def send_text(self, text, at_mobiles=[], at_all=False):
"""
发送文本消息
:p
Dingding通知
于 2022-11-25 12:21:31 首次发布
本文介绍了如何配置和使用钉钉机器人进行自动化通知。首先,你需要在钉钉群中添加一个自定义机器人,设置其名称和安全设置。然后,你会得到一个webhook,如'https://oapi.dingtalk.com/robot/send?access_token=sdfsdfsdf',其中access_token是关键。结合Python编程,可以利用这个webhook实现自动发送消息的功能。官方文档提供了详细的操作指南。
摘要由CSDN通过智能技术生成