python如何实现钉钉机器人自动打卡天天早下班(python,开发技术)

时间:2024-05-09 04:24:50 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

一,新建钉钉机器人

1.钉钉群右上角点击群设置,选择智能群助手,点击添加机器人,选择自定义机器人;

python如何实现钉钉机器人自动打卡天天早下班

2.给机器人起个名字,消息推送开启,复制出 webhook,后面会用到,勾选自定义关键词,填写关键词(关键词可以随便填写,但是一定要记住,后面会用);

python如何实现钉钉机器人自动打卡天天早下班

python如何实现钉钉机器人自动打卡天天早下班

二,钉钉机器人发送消息

url 就是创建机器人时的 webhook,data 中的 atMobiles 可填写多个手机号,发送的消息会直接 @ 这个人,text 的 content 里面一定要加上创建机器人时设置的关键词,msgtype 意思时文本格式,也可以 link 格式,就可以放链接了;

defsend_text(self):url="https://oapi.dingtalk.com/robot/send?access_token=43c4dab2ac31125e605c458b4b9561a73"headers={'Content-Type':'application/json'}data={"at":{"atMobiles":["18206264857"],"atUserIds":["user123"],"isAtAll":False},"text":{"content":"砍价小程序接口自动化测试"},"msgtype":"text"},"msgtype":"text"}requests.post(url,headers=headers,data=json.dumps(data))

三,钉钉机器人实际的应用

1.监控接口自动化结果

python如何实现钉钉机器人自动打卡天天早下班

实现思路是:jenkins 定时执行自动化——执行完后生成 html 报告——BeautifulSoup 模块解析 html 报告——发送钉钉消息

如下代码:

解析 html 的模块:

fromcommon.handle_pathimporthtml_pathfrombs4importBeautifulSoupclassGetHtml:"""读取测试报告,解析html获得测试用例总数,通过数等,发送到钉钉"""defget_total(self):withopen(html_path,"r",encoding="utf-8")asf:file=f.read()soup=BeautifulSoup(file,'html.parser')#使用BeautifulSoup库解析网页内容item=soup.find_all("p")[1].string#使用BeautifulSoup库的标签方法找到你需要的内容returnstr(item)defget_pass(self):withopen(html_path,"r",encoding="utf-8")asf:file=f.read()soup=BeautifulSoup(file,'html.parser')#使用BeautifulSoup库解析网页内容item=soup.find_all("span",class_="passed")[0].string#使用BeautifulSoup库的标签方法找到你需要的内容returnstr(item)defget_skipped(self):withopen(html_path,"r",encoding="utf-8")asf:file=f.read()soup=BeautifulSoup(file,'html.parser')#使用BeautifulSoup库解析网页内容item=soup.find_all("span",class_="skipped")[0].string#使用BeautifulSoup库的标签方法找到你需要的内容returnstr(item)defget_failed(self):withopen(html_path,"r",encoding="utf-8")asf:file=f.read()soup=BeautifulSoup(file,'html.parser')#使用BeautifulSoup库解析网页内容item=soup.find_all("span",class_="failed")[0].string#使用BeautifulSoup库的标签方法找到你需要的内容returnstr(item)defget_error(self):withopen(html_path,"r",encoding="utf-8")asf:file=f.read()soup=BeautifulSoup(file,'html.parser')#使用BeautifulSoup库解析网页内容item=soup.find_all("span",class_="error")[0].string#使用BeautifulSoup库的标签方法找到你需要的内容returnstr(item)defget_xfailed(self):withopen(html_path,"r",encoding="utf-8")asf:file=f.read()soup=BeautifulSoup(file,'html.parser')#使用BeautifulSoup库解析网页内容item=soup.find_all("span",class_="xfailed")[0].string#使用BeautifulSoup库的标签方法找到你需要的内容returnstr(item)defget_xpassed(self):withopen(html_path,"r",encoding="utf-8")asf:file=f.read()soup=BeautifulSoup(file,'html.parser')#使用BeautifulSoup库解析网页内容item=soup.find_all("span",class_="xpassed")[0].string#使用BeautifulSoup库的标签方法找到你需要的内容returnstr(item)if__name__=='__main__':t=GetHtml()t.get_xpassed()

如下代码:

发送钉钉消息的模块:

importrequestsimportjsonfromcommon.handle_readhtmlimportGetHtmlclassSendMassage:"""发送测试结果到钉钉群"""result=GetHtml()total=result.get_total()passed=result.get_pass()skipped=result.get_skipped()failed=result.get_failed()error=result.get_error()xfailed=result.get_xfailed()xpassed=result.get_xpassed()defsend_text(self):url="https://oapi.dingtalk.com/robot/send?access_token=43c4dab2ac3152e605c458b4b9561a73"headers={'Content-Type':'application/json'}data={"at":{"atMobiles":["18206233880"],"atUserIds":["user123"],"isAtAll":False},"text":{"content":"砍价小程序接口自动化测试\ntotal:{}\npassed:{},\nskipped:{},\nfailed:{},\nerror:{},\nxfailed:{},\nxpassed:{}".format(self.total,self.passed,self.skipped,self.failed,self.error,self.xfailed,self.xpassed)},"msgtype":"text"}requests.post(url,headers=headers,data=json.dumps(data))if__name__=='__main__':s=SendMassage()s.send_text()

jenkins 配置的 shell 为:

先执行接口自动化脚本,等待一会然后发送钉钉消息;

${PYTHON}main.pysleep100${PYTHON}handle_dingding.py

接口自动化发钉钉群消息还可以再优化,比如可以加上断言失败的错误日志等;

python如何实现钉钉机器人自动打卡天天早下班

2,监控 qa 环境错误日志

此处发送的 qq 邮件,消息查看不方便,且不好共享,可以优化为发钉钉群消息,然后将开发也拉到群里,提高效率;

3,jira 上有钉钉机器人插件,可以每天发送消息 @ 某某开发 还有 N 个待处理 bug,@ 某某测试 还有 N 个待验证 bug,以及监控看板指标达到阈值报警等;

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:python如何实现钉钉机器人自动打卡天天早下班的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:C#中的延时函数sleep怎么使用下一篇:

59 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18