需要实现的是:
1.企微推送 定制化消息
思路:
(1)把需要推送的内容处理好
(2)调用企微api
(3)推送时间的设置触发--可利用jekins的Build Triggers定时构建
上具体实现:
(1)定时推送发版提醒消息
该功能实现:每天定时推送提醒开发合并代码提醒,自动部署发版
代码:
def send_message(self):
# 正式key
wx_key = "xx"
wx_webhookurl = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={}'.format(wx_key)
headers = {'Content-Type': 'application/json'}
hbpeople = ["san.zhang"]
msg = '定时提示: 抓紧合并代码了,10分钟后将启动自动部署 test/uat'
testdata = json.dumps({"msgtype": "text", "text": {"content": msg, "mentioned_list": hbpeople}})
r = requests.post(wx_webhookurl, data=testdata, headers=headers, verify=False)
print(r.text)
return r
(2)调用第三方接口进行处理后定时推送
该功能实现:每天定时推送禅道或 jira(其他平台均可)
待QA&开发认领任务 / QA&开发延期的任务/ 待产品验收任务 /本周上线的任务等等按需汇总后推送至群里@对应的人
~~可根据项目组需要定制化编写~~
代码:
message:调用第三方接口进行处理后的推送的 msg
send_message:企微推送消息
# 企业微信机器人推送消息
def send_message(self, content):
project_name = self.project_name()
content = project_name + content
print(content)
post_content = {"msgtype": "markdown", "markdown": {"content": content}}
result = requests.post(url=self.ROBOT_URL, json=post_content, verify=False)
print(result.text)
(3)推送测试报告
该功能实现:把测试报告url 推送群里
代码:
#报告可访问化:将测试报告通过接口上传至文件服务
def upload_report(filepath, filename):
url = "xx"
payload = {}
files = [xx]
headers = {
'bucket-name': 'xx',
'token': gettoken()
}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
return response.json()['data'][0]['path']
reportpath = upload_report(filepath, filename)
#报告持久化:将测试报告链接上传到数据库
uploadurl = "xx"
headers = {'Content-Type': 'application/json'}
uploadparam = json.dumps({xx})
#推送消息定制化
testdata = json.dumps({
"msgtype": "markdown",
"markdown": {
"content":
'xx报告如下:\n'
'成功:<font color="info">' + str(success) + '条</font>\n'
+ '失败:<font color="warning">' + str(fail) + '条</font>\n'
'>失败的用例为:\n' + str_fail_case
+ '\n [测试报告链接]('+reportpath+')'
}
}
)
#调用企微推送
r = requests.post(wx_webhookurl, data=testdata, headers=headers, verify=False)