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()
相关推荐
风之所往_几秒前
Python 3.5 新特性全面总结
python
野生的小狗熊5 分钟前
【自学Agent开发之路】第二篇—从.NET到Python:Agent开发的本质就是投喂上下文
python
观测云7 分钟前
观测云故障中心 × 飞书联动:群内触发 OnCall 故障闭环最佳实践
飞书·告警·观测云
牵牛花主人11 分钟前
【无标题】
python·pandas
我送炭你添花11 分钟前
工业现场稳定性工程:能量秩序的守护之道(目录)
机器人·自动化·智能工厂
abcy07121318 分钟前
sqlalchemy 原生sql判断条件是否为空,为空则跳过
开发语言·python
知识分享小能手19 分钟前
数据预处理入门学习教程,从入门到精通, 实战演练——数据分析师岗位分析知识点详解(8)
python·学习·信息可视化
Wonderful U19 分钟前
Python+Django实战:打造智能生鲜果蔬进销存管理系统(采购入库、库存预警、销售开单、毛利统计)
数据库·python·django
yuhuofei202123 分钟前
【Python入门】Python中的集合set
python
天丁o26 分钟前
开源教程:用飞书长连接把本地 Codex CLI 接入机器人,支持进度心跳和可见过程
机器人·开源·飞书