python飞书群机器人通过webhook发送消息

python飞书群机器人通过webhook发送消息

python 复制代码
import json
import logging

import requests



logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)


class FeishuTalk:
    """
    飞书群机器人通过webhook发送消息
    """

    def __init__(self, url: str = ""):
        test_robot_url = 'https://open.feishu.cn/open-apis/bot/v2/hook/xxx'
        self.url = url or test_robot_url
        self.mHeader = {'Content-Type': 'application/json; charset=UTF-8'}

    def send(self, data):
        """
        发送消息
        """
        try:
            info = requests.post(url=self.url, data=data, headers=self.mHeader)
            if info.json().get("StatusCode") == 0 and info.json().get("msg") == "success":
                logger.info(f"飞书通知发送成功,msg={info.json()}")
            else:
                logger.warning(f"飞书通知发送异常,{info.json()=}")
        except Exception as e:
            logger.warning("飞书通知发送异常")
            logger.warning(e)
            pass

    def send_msg(self):
        """
        发送消息
        """
        field_list = [{
            "is_short": True,
            "text": {
                "tag": "lark_md",
                "content": f"**姓名**:<font color=\"green\">毛豆</font>\n"
            }
        },
            {
                "is_short": True,
                "text": {
                    "tag": "lark_md",
                    "content": f"**年龄**:<font color=\"green\">6</font>\n"
                }
            }

        ]

        card = json.dumps({
            "elements": [
                {
                    "tag": "div",
                    "fields": field_list
                },
                {
                    "actions": [
                        {
                            "tag": "button",
                            "text": {
                                "content": "详情",
                                "tag": "plain_text"
                            },
                            "type": "primary",
                            "url": 'www.baidu.com'
                        },
                    ],
                    "tag": "action"
                }
            ],
            "header": {
                "template": "turquoise",
                "title": {
                    "content": "📬电通知",
                    "tag": "plain_text"
                }
            }
        })
        body = json.dumps({"msg_type": "interactive", "card": card})
        self.send(data=body)


if __name__ == "__main__":
    fs = FeishuTalk()
    fs.send_msg()
相关推荐
2zcode6 小时前
基于MATLAB的家用场景下扫地机器人路径规划研究设计
开发语言·matlab·机器人
计算机毕业编程指导师6 小时前
【大数据毕设推荐】Hadoop+Spark电影票房分析系统,Python+Django全栈实现 毕业设计 选题推荐 毕设选题 数据分析 机器学习 数据挖掘
大数据·hadoop·python·计算机·spark·毕业设计·电影票房
唐叔在学习6 小时前
TodoList:我的待办助手
python·程序员·ai编程·待办应用
郝学胜-神的一滴7 小时前
深度学习损失函数:从原理到实战之 Smooth L1 Loss
人工智能·pytorch·python·深度学习·机器学习·数据挖掘
hhzz7 小时前
第1天:初识Python
开发语言·python·学习编程
YJlio7 小时前
用女娲蒸馏 Mark Russinovich 排障思维:打造 Windows 桌面运维专家 Skill
运维·windows·飞书·ai办公·多维表格·飞书v7.63·飞书问卷
黄粱梦醒7 小时前
UV快速搭建新项目
python
小学生-山海8 小时前
【YOLO系列】基于YOLOv8/v11/v26与tkinter的车流量统计系统设计
python·yolo
szial8 小时前
Python Click 教程:从函数到专业命令行工具
开发语言·python
u0119608238 小时前
ray 依赖分发
python