#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021-10-14 16:59
# @Author : herry
# @File : DDTALK.py
# @software: PyCharm
# @describe: DDTALK
import base64
import hashlib
import hmac
import urllib
import time
from dingtalkchatbot.chatbot import DingtalkChatbot
import requests
import json
'''
钉钉机器人发送群消息实现步骤:
1、在钉钉后台添加企业机器人,会自动创建一个群,也可以在群设置页面,添加一个群webhook自定义机器人,这个机器人就绑定了这个群。
2、在群设置中获取webhook地址,和设置安全方式(必须设置一种,可以设置关键字、加密、IP地址)
3、发送订单消息,发送订单消息有两种发送,
一种是直接HTTP接口调用;一种是引入钉钉服务模块,
注意:企业机器人可以添加到不同的群,在添加到其他群后,安全策略失效
群webhook机器人只能当前群使用
'''
class DDTalk:
def sign(self, secret, timestamp):
##加密签名#########################
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
return sign
def send_Text_DingTalkBySecret(self, webhook, secret, msg, is_at_all=False,at_mobiles=[],at_dingtalk_ids=[]):
'''
直接调用钉钉封装好的方法进行发送消息
:param at_dingtalk_ids: 被@ 人的[钉钉id]
:param at_mobiles: 被@人的[手机号]
:param webhook: webhook地址
:param secret: 秘钥
:param msg: 消息内容
:param is_at_all: 是否@全部
:return:
'''
timestamp = str(round(time.time() * 1000))
sign_str = self.sign(secret, timestamp)
##调用请求发送消息##################
webhook = webhook + "×tamp=" + str(timestamp) + "&sign=" + sign_str
xiaoyi = DingtalkChatbot(webhook);
# 指定钉钉号@
# xiaoyi.send_text(msg=msg, at_dingtalk_ids=at_dingtalk_ids)
# 指定手机号@
# xiaoyi.send_text(msg=msg, at_mobiles=at_mobiles)
# @所有人和指定手机号时,指定的手机号的会失效
# xiaoyi.send_text(msg=msg, is_at_all=True, at_mobiles=at_mobiles)
# 同时指定一个人的手机号和钉钉号时,只会@一次
# xiaoyi.send_text(msg=msg, at_dingtalk_ids=at_dingtalk_ids, at_mobiles=at_mobiles)
# @所有人,is_at_all=Ture时
# xiaoyi.send_text(msg=msg, is_at_all=is_at_all)
xiaoyi.send_text(msg=msg,at_dingtalk_ids=at_dingtalk_ids, at_mobiles=at_mobiles,is_at_all=is_at_all)
def send_Link(self, webhook, secret, title, text, message_url, pic_url=None):
"""
采用钉钉内置方法,发送Link形式的消息
:param webhook:
:param secret:
:param title:
:param text:
:param message_url:
:param pic_url:
:return:
"""
timestamp = str(round(time.time() * 1000))
sign_str = self.sign(secret, timestamp)
##调用请求发送消息##################
webhook = webhook + "×tamp=" + str(timestamp) + "&sign=" + sign_str
xiaoyi = DingtalkChatbot(webhook);
xiaoyi.send_link(title=title, text=text, message_url=message_url,
pic_url=pic_url)
def send_Message_Text(self,webhook, content,isAtAll=False):
'''
HTTP 接口访问形式调用
:param isAtAll:
:param content:
:param webhook:
:param message: 消息格式需要指明格式(消息类型),和消息内容,demo
文档地址:https://developers.dingtalk.com/document/robots/custom-robot-access
:return:
'''
message = {
"msgtype": "text",
"text": {
"content": content
},
"at": {
"isAtAll": isAtAll
}
}
header = {"Content-Type": "application/json", "Charset": "UTF-8"}
message_json = json.dumps(message)
res = requests.post(url=webhook, data=message_json, headers=header)
return (res.text)
def send_Message_URL(self,webhook, content,title,picUrl,messageUrl,isAtAll=False):
'''
HTTP 接口访问形式调用
:param isAtAll:
:param messageUrl:
:param picUrl:
:param title:
:param content:
:param webhook:
:param message: 消息格式需要指明格式(消息类型),和消息内容,demo
文档地址:https://developers.dingtalk.com/document/robots/custom-robot-access
:return:
'''
message_link = {
"msgtype": "link",
"link": {
"text": content,
"title": title,
"picUrl": picUrl,
"messageUrl": messageUrl
} ,
"at": {
"isAtAll": isAtAll
}
}
header = {"Content-Type": "application/json", "Charset": "UTF-8"}
message_json = json.dumps(message_link)
res = requests.post(url=webhook, data=message_json, headers=header)
return (res.text)
def send_Message_markDown(self,webhook, content,title,isAtAll=False):
'''
HTTP 接口访问形式调用
:param webhook:
:param message: 消息格式需要指明格式(消息类型),和消息内容,demo
文档地址:https://developers.dingtalk.com/document/robots/custom-robot-access
:return:
'''
message_markDown = {
"msgtype": "markdown",
"markdown": {
"title": title,
"text": content
},
"at": {
"isAtAll": isAtAll
}
}
header = {"Content-Type": "application/json", "Charset": "UTF-8"}
message_json = json.dumps(message_markDown)
res = requests.post(url=webhook, data=message_json, headers=header)
return (res.text)
if __name__ == "__main__":
webhook = 'webhook地址'
secret = "XXXXX"
message="记得提交周六的加班申请哦~"
title="测试数据"
message_url="https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI"
pic_url="https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png"
ddtalk=DDTalk()
print(ddtalk.send_Text_DingTalkBySecret(webhook=webhook, secret=secret, msg=message))
print(ddtalk.send_Link(webhook=webhook,secret=secret, title=title,text=message,message_url=message_url,pic_url=pic_url))
#
#
print(ddtalk.send_Message_Text(webhook=webhook, content=message,isAtAll=True))
print(ddtalk.send_Message_URL(webhook=webhook,content=message,title=title,picUrl=pic_url,messageUrl=message_url,isAtAll=False ))
-------------------------------------最后---------------------------------
更多软件测试相关内容请关注“软件测试道与术”公众号或扫描下方二维码