python实现飞书群机器人消息通知(消息卡片)

python实现飞书群机器人消息通知

直接上代码

python 复制代码
"""
飞书群机器人发送通知
"""
import time
import urllib3
import datetime


urllib3.disable_warnings()


class FlybookRobotAlert():
    def __init__(self):
        self.webhook = webhook_url
        self.headers = {Content-Type: application/json; charset=UTF-8}

    def post_to_robot(self, post_data):
        '''
        给飞书机器人发送请求
        :param data:
        :return:
        '''
        try:
            resp = requests.request(method="POST", url=self.webhook, data=post_data, headers=self.headers).json()
            if resp.get("StatusCode") == 0 and resp.get("msg") == "success":
                Logger.info(f"飞书通知发送成功,msg={resp}")
            else:
                Logger.warning(f"飞书通知发送失败,{resp}")
        except Exception as e:
            Logger.warning("飞书通知发送异常")
            Logger.warning(e)
            pass

    def send_message(self, proj_name, total, passed, failed, skipped, adress="https://www.example.com"):

        # 飞书通知标题
        robot_headers = "自动化报告"
        # 自定义飞书通知内容
        robot_content = "ddd!自动化执行情况反馈,请相关测试童鞋注意!并及时跟进!"

        execute_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

        field_list = [
            {
                "is_short": False,
                "text": {
                    "tag": "lark_md",
                    "content": f"**用例执行完毕时间**:<font color=\"green\">{{}}</font>\n".format(execute_time)
                }
            },
            {
                "is_short": False,
                "text": {
                    "tag": "lark_md",
                    "content": f"**项目名称**:<font color=\"green\">{{}}</font>\n".format(proj_name)
                }
            },
            {
                "is_short": False,
                "text": {
                    "tag": "lark_md",
                    "content": f"**用例总数**:<font color=\"green\">{{}}</font>\n".format(total)
                }
            },
            {
                "is_short": False,
                "text": {
                    "tag": "lark_md",
                    "content": f"**通过用例数**:<font color=\"green\">{{}}</font>\n".format(passed)
                }
            },
            {
                "is_short": False,
                "text": {
                    "tag": "lark_md",
                    "content": f"**失败用例数**:<font color=\"green\">{{}}</font>\n".format(failed)
                }
            },
            {
                "is_short": False,
                "text": {
                    "tag": "lark_md",
                    "content": f"**跳过用例数**:<font color=\"green\">{{}}</font> \n".format(skipped)
                }
            }
        ]

        card = json.dumps({
            "config": {
                "wide_screen_mode": True
            },
            "elements": [
                {
                    "tag": "div",
                    "text": {
                        "content": robot_content,
                        "tag": "lark_md"
                    }
                },
                {
                    "tag": "div",
                    "fields": field_list
                },
                {
                    "actions": [{
                        "tag": "button",
                        "text": {
                            "tag": "plain_text",
                            "content": "allure报告,请点击查看"

                        },
                        "url": adress,
                        "type": "primary"
                    }],
                    "tag": "action"
                }
            ],
            "header": {
                "template": "red",
                "title": {
                    "content": robot_headers,
                    "tag": "plain_text"
                }
            }
        })

        msg_body = json.dumps({"msg_type": "interactive", "card": card})
        self.post_to_robot(data=msg_body)
        # {'StatusCode': 0, 'StatusMessage': 'success', 'code': 0, 'data': {}, 'msg': 'success'}
        return

if __name__ == '__main__':
    a = FlybookRobotAlert()
    a.send_message('你的项目名称GoGoGo', 4, 4, 0, 0)
相关推荐
灵途科技21 小时前
灵途科技亮相NEPCON ASIA 2025 以光电感知点亮具身智能未来
人工智能·科技·机器人
007php0071 天前
某游戏大厂 Java 面试题深度解析(四)
java·开发语言·python·面试·职场和发展·golang·php
景彡先生1 天前
Python pandas数据透视表(pivot_table)详解:从入门到实战,多维数据分析利器
python·数据分析·pandas
Blossom.1181 天前
把AI“编”进草垫:1KB决策树让宠物垫自己报「如厕记录」
java·人工智能·python·算法·决策树·机器学习·宠物
极客数模1 天前
2025年(第六届)“大湾区杯”粤港澳金融数学建模竞赛准备!严格遵循要求,拿下大奖!
大数据·python·数学建模·金融·分类·图论·boosting
倔强青铜三1 天前
苦练Python第73天:玩转对象持久化,pickle模块极速入门
人工智能·python·面试
程序员三藏1 天前
Postman持久化保存/设置断言详解
自动化测试·软件测试·python·测试工具·职场和发展·接口测试·postman
java1234_小锋1 天前
PyTorch2 Python深度学习 - 卷积神经网络(CNN)介绍实例 - 使用MNIST识别手写数字示例
python·深度学习·cnn·pytorch2
雍凉明月夜1 天前
人工智能学习中深度学习之python基础之迭代器、生成器、文件处理和模块等
python·深度学习·学习·pycharm