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()
相关推荐
张彦峰ZYF1 分钟前
深入 LangGraph State:Reducer 是如何让状态“自动合并”的
人工智能·python·大模型·langgraph
大蚂蚁2号11 分钟前
本地视频转文字免费工具
python·音视频
鲁邦通物联网13 分钟前
边缘计算实战:无人工厂多楼层AGV物理隔离梯控状态机设计
人工智能·机器人·巡检机器人·机器人梯控·agv梯控·非侵入式采集·机器人自主乘梯
海岸线科技14 分钟前
飞书 PPAP Audit Agent:汽车供应链质量审核的智能化落地方案
人工智能·汽车·飞书
小悟空20 分钟前
[AI 生成] Nginx 502 Bad Gateway 排查手册(Python 后端篇)
python·nginx·gateway
莫宰特24 分钟前
NNMind:像搭积木一样设计神经网络
人工智能·python·深度学习·从零训练
极客侃科技25 分钟前
能接通飞书的AI主机:联想AI主机Mini适配主流IM办公场景
人工智能·飞书
Wonderful U26 分钟前
基于Python+Django的轻量化私有云盘系统:从零搭建安全可控的文件存储与共享平台
python·安全·django
txg66628 分钟前
机器人领域简报(2026年5月29日—6月4日)
人工智能·机器人·自动化
garmin Chen32 分钟前
Prompt工程入门:让AI按你的要求工作(1)--prompt概述与设计
java·人工智能·python·junit·prompt·agent