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()
相关推荐
用户8356290780517 小时前
使用 Python 控制 Excel 工作表的分页位置
后端·python
用户8356290780517 小时前
Python 如何在 Word 中添加和管理文档变量
后端·python
鲁邦通物联网8 小时前
既有电梯机器人梯控选型:现场可行性评估方法
机器人·巡检机器人·机器人梯控·agv梯控·非侵入式采集·机器人乘梯·机器人自主乘梯
用户0190275816110 小时前
Dify / Coze 低代码 Agent 怎么接 A 股行情?
python
Dumbness11 小时前
不传 day 参数,座位图显示全空:用 Python 标准库手写 Web 框架,请求到业务代码前的 5 个细节
后端·python
databook12 小时前
收上来一堆 Excel,怎么保证没改错
python·数据分析
wangxiaoxuan21712 小时前
TF2 的原理与使用:把坐标、运动和观测时间一起算清楚
计算机视觉·机器人·科沃斯八界
2601_9679873712 小时前
Python 教程:默认字典(defaultdict)全维度深度指南
python·性能优化·数据处理·collections·defaultdict
鱼听禅13 小时前
Ubuntu安装YOLOv8进行菌落识别训练
linux·python·yolo·ubuntu
硅谷秋水13 小时前
EgoHumanoid‑V2 方法分析
人工智能·机器学习·计算机视觉·语言模型·机器人