iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python实现钉钉机器人自动打卡天天早下班
  • 275
分享到

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

2024-04-02 19:04:59 275人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录一,新建钉钉机器人二,钉钉机器人发送消息三,钉钉机器人实际的应用一,新建钉钉机器人 1.钉钉群右上角点击群设置,选择智能群助手,点击添加机器人,选择自定义机器人; 2.给机器人

一,新建钉钉机器人

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

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

二,钉钉机器人发送消息

url 就是创建机器人时的 webhook,data 中的 atMobiles 可填写多个手机号,发送的消息会直接 @ 这个人,text 的 content 里面一定要加上创建机器人时设置的关键词,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.监控接口自动化结果

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

如下代码:

解析 html 的模块:

from common.handle_path import html_path
from bs4 import BeautifulSoup
class GetHtml:
    """
    读取测试报告,解析html  获得测试用例总数,通过数等,发送到钉钉
    """
    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()

如下代码:

发送钉钉消息的模块:

import requests
import json
from common.handle_readhtml import GetHtml
class SendMassage:
    """
    发送测试结果到钉钉群
    """
    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 为:

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

${python} main.py
sleep 100
${Python} handle_dingding.py

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

2,监控 qa 环境错误日志

这里贴上周游大佬的一篇博客:https://www.jb51.net/article/250972.htm

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

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

以上就是python实现钉钉机器人自动打卡天天下早班的详细内容,更多关于python钉钉机器人打卡的资料请关注编程网其它相关文章!

--结束END--

本文标题: python实现钉钉机器人自动打卡天天早下班

本文链接: https://www.lsjlt.com/news/118663.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作