python實現釘釘機器人自動打卡天天早下班
一,新建釘釘機器人
1.釘釘群右上角點擊群設置,選擇智能群助手,點擊添加機器人,選擇自定義機器人;
2.給機器人起個名字,消息推送開啟,復制出 webhook,后面會用到,勾選自定義關鍵詞,填寫關鍵詞(關鍵詞可以隨便填寫,但是一定要記住,后面會用);
二,釘釘機器人發(fā)送消息
url 就是創(chuàng)建機器人時的 webhook,data 中的 atMobiles 可填寫多個手機號,發(fā)送的消息會直接 @ 這個人,text 的 content 里面一定要加上創(chuàng)建機器人時設置的關鍵詞,msgtype 意思時文本格式,也可以 link 格式,就可以放鏈接了;
def send_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.監(jiān)控接口自動化結果
實現思路是:jenkins 定時執(zhí)行自動化——執(zhí)行完后生成 html 報告——BeautifulSoup 模塊解析 html 報告——發(fā)送釘釘消息
如下代碼:
解析 html 的模塊:
from common.handle_path import html_path from bs4 import BeautifulSoup class GetHtml: """ 讀取測試報告,解析html 獲得測試用例總數,通過數等,發(fā)送到釘釘 """ def get_total(self): with open(html_path, "r", encoding="utf-8") as f: file = f.read() soup = BeautifulSoup(file, 'html.parser') # 使用BeautifulSoup庫解析網頁內容 item = soup.find_all("p")[1].string # 使用BeautifulSoup庫的標簽方法找到你需要的內容 return str(item) def get_pass(self): with open(html_path, "r", encoding="utf-8") as f: file = f.read() soup = BeautifulSoup(file, 'html.parser') # 使用BeautifulSoup庫解析網頁內容 item = soup.find_all("span",class_="passed")[0].string # 使用BeautifulSoup庫的標簽方法找到你需要的內容 return str(item) def get_skipped(self): with open(html_path, "r", encoding="utf-8") as f: file = f.read() soup = BeautifulSoup(file, 'html.parser') # 使用BeautifulSoup庫解析網頁內容 item = soup.find_all("span",class_="skipped")[0].string # 使用BeautifulSoup庫的標簽方法找到你需要的內容 return str(item) def get_failed(self): with open(html_path, "r", encoding="utf-8") as f: file = f.read() soup = BeautifulSoup(file, 'html.parser') # 使用BeautifulSoup庫解析網頁內容 item = soup.find_all("span",class_="failed")[0].string # 使用BeautifulSoup庫的標簽方法找到你需要的內容 return str(item) def get_error(self): with open(html_path, "r", encoding="utf-8") as f: file = f.read() soup = BeautifulSoup(file, 'html.parser') # 使用BeautifulSoup庫解析網頁內容 item = soup.find_all("span",class_="error")[0].string # 使用BeautifulSoup庫的標簽方法找到你需要的內容 return str(item) def get_xfailed(self): with open(html_path, "r", encoding="utf-8") as f: file = f.read() soup = BeautifulSoup(file, 'html.parser') # 使用BeautifulSoup庫解析網頁內容 item = soup.find_all("span",class_="xfailed")[0].string # 使用BeautifulSoup庫的標簽方法找到你需要的內容 return str(item) def get_xpassed(self): with open(html_path, "r", encoding="utf-8") as f: file = f.read() soup = BeautifulSoup(file, 'html.parser') # 使用BeautifulSoup庫解析網頁內容 item = soup.find_all("span",class_="xpassed")[0].string # 使用BeautifulSoup庫的標簽方法找到你需要的內容 return str(item) if __name__ == '__main__': t = GetHtml() t.get_xpassed()
如下代碼:
發(fā)送釘釘消息的模塊:
import requests import json from common.handle_readhtml import GetHtml class SendMassage: """ 發(fā)送測試結果到釘釘群 """ 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() def send_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":"砍價小程序接口自動化測試 \n total : {}\n passed : {},\n skipped : {},\n failed : {},\n error : {},\n xfailed : {},\n xpassed : {}".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 為:
先執(zhí)行接口自動化腳本,等待一會然后發(fā)送釘釘消息;
${PYTHON} main.py sleep 100 ${PYTHON} handle_dingding.py
接口自動化發(fā)釘釘群消息還可以再優(yōu)化,比如可以加上斷言失敗的錯誤日志等;
2,監(jiān)控 qa 環(huán)境錯誤日志
這里貼上周游大佬的一篇博客:http://www.dbjr.com.cn/article/250972.htm
此處發(fā)送的 qq 郵件,消息查看不方便,且不好共享,可以優(yōu)化為發(fā)釘釘群消息,然后將開發(fā)也拉到群里,提高效率;
3,jira 上有釘釘機器人插件,可以每天發(fā)送消息 @ 某某開發(fā) 還有 N 個待處理 bug,@ 某某測試 還有 N 個待驗證 bug,以及監(jiān)控看板指標達到閾值報警等;
以上就是python實現釘釘機器人自動打卡天天下早班的詳細內容,更多關于python釘釘機器人打卡的資料請關注腳本之家其它相關文章!
相關文章
基于tensorflow指定GPU運行及GPU資源分配的幾種方式小結
今天小編就為大家分享一篇基于tensorflow指定GPU運行及GPU資源分配的幾種方式小結,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02Django定制Admin頁面詳細實例(展示頁面和編輯頁面)
django自帶的admin因為功能和樣式比較簡陋,常常需要再次定制,下面這篇文章主要給大家介紹了關于Django定制Admin頁面(展示頁面和編輯頁面)的相關資料,需要的朋友可以參考下2023-06-06