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()
相关推荐
南京码讯光电技术有限公司1 分钟前
AGV、巡检机器人运行频繁掉线?工业级无线AP,实现移动作业传输不中断
机器人
小江的记录本2 分钟前
【大语言模型】大语言模型——核心概念(预训练、SFT监督微调、RLHF/RLAIF对齐、Token、Embedding、上下文窗口)
java·人工智能·后端·python·算法·语言模型·自然语言处理
坐吃山猪2 分钟前
Python04_序列和字符串
python
tang7451639629 分钟前
mac的ideal中调用driver = webdriver.Chrome() 启动不了的原因
chrome·python·macos
码云数智-大飞16 分钟前
CSS 优先级详解:告别样式冲突,掌控网页“层叠”艺术
人工智能·python·tensorflow
迦南的迦 亚索的索20 分钟前
PYTHON_DAY21_数据分析
开发语言·python·数据分析
阿Y加油吧21 分钟前
栈的经典应用:字符串解码 & 每日温度 深度解析
数据结构·python·算法
ID_1800790547325 分钟前
如何使用 Python 调用小红书笔记评论 API 时进行并发控制?
开发语言·笔记·python
树下水月34 分钟前
使用python 一键生成,PGSQL的数据字典
python
高山流水&上善35 分钟前
基于Qwen3-0.6B的光纤基础测试问答系统设计与实现
python·django