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()
相关推荐
CTA量化套保2 小时前
近期零基础量化学习:先分阶段,再用 AI 检查缺口
人工智能·python
清水白石0083 小时前
Python 死锁排查全攻略:从线程卡死到锁依赖定位与工程化修复
linux·网络·python
Elias不吃糖3 小时前
Langfuse 入门:Trace、Prompt、Dataset、Experiment、Evaluator
前端·python·prompt·langfuse
TechWayfarer3 小时前
批量查IP归属地,在线API、脚本、离线库怎么选?三种方案实测对比
网络·python·网络协议·tcp/ip
七牛开发者5 小时前
为什么 Go 很适合 AI 辅助开发?
数据库·人工智能·python·elasticsearch·log4j
河南凹凸环境艺术设计5 小时前
性价比高的民宿酒店设计企业
大数据·人工智能·python
神经智研社7 小时前
ROS2-第十七章:网络通信,win11系统 通过镜像配置 打通 WSL 下的ros2与 同局域网(同IP段)的里其它设备上的ros2通信
机器人·机器人环境搭建·win11 ros2 开发环境
wuyk5558 小时前
Python 第三章:列表 List
服务器·开发语言·python
笨鸟先飞,勤能补拙8 小时前
从预测到决策:人工智能与机器学习的系统方法、工程闭环与现实边界
大数据·人工智能·windows·python·机器学习·密码学
MgArcher8 小时前
抖音a_bogus参数逆向实战:JSVMP环境模拟与Hook技术(2025最新)
javascript·爬虫·python