添加群机器人
安全设置选择关键字,关键字填写发送消息的固定文本,选择后点击完成,复制Webhook,后边会用
不废话直接上代码
# -*- coding: UTF-8 -*-
"""
=================================================
@Project -> File :py3_dev -> dingding
@IDE :PyCharm
@Author :相**
@Date :2020/6/18 11:36
@Desc :
@用途 :钉钉群消息推送
@email :[email protected]
==================================================
"""
import json
import requests
class DingDing(object):
def __init__(self,
title, news,
at=False,
dev=False,
isAtAll=True,
url_1='https://images0.cnblogs.com/blog2015/708426/201508/172356420975864.gif',
):
"""
模拟钉钉机器人发消息
:param title: 标题
:param news: 消息内容
:param at: @谁的手机号,列表形式,列表的元素为字符串;不填写默认不@任何人
:param isAtAll: 为True时@所有人;为False时,不@任何人;不填写,默认不@任何人;
"""
self.dev = dev
self.title = title
self.isAtAll = isAtAll
self.news = news
self.at = at
self.img_url_1 = url_1
self.run()
def run(self):
# 群机器人
url = 'https://oapi.dingtalk.com/robot/send?access_token=*******'
# 设置@人员,列表,列表内的元素为字符串
newsData = {
"msgtype": "markdown",
"markdown": {
"title": self.title,
"text":
"![](" + self.img_url_1 + ")" + "\n\n" +
'标题一:%s'%self.news[0] + "\n\n"
'标题二:%s'%self.news[1]+ "\n\n"
'标题三:%s'%self.news[2]+ "\n\n"
},
"at": {
"atMobiles": [],
"isAtAll": False
}
}
if self.at:
newsData['at']['atMobiles'].append(self.at)
# 设置@所有人
if self.isAtAll:
newsData['at']['isAtAll'] = True
headers = {
'Content-Type': 'application/json; charset=utf-8'
}
# 关闭ssl安全警告
requests.packages.urllib3.disable_warnings()
f = requests.post(url, data=json.dumps(newsData), headers=headers, verify=False)
if __name__ == '__main__':
DingDing(title='群消息标题', news=['标题一', '标题二', '标题三'], isAtAll=True)