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()
相关推荐
逆境清醒22 分钟前
python教程总目录(更新中ing。。。)
开发语言·python
小北方城市网28 分钟前
GEO 智变新篇:质效双升 + 责任共生,打造 AI 时代本地商业长效增长引擎
大数据·人工智能·python·数据库架构
普罗米修斯Aaron_Swartz34 分钟前
Lerobot-sim2real运行问题记录
机器人
Hello_wshuo42 分钟前
锅炉温控系统优化
linux·python·物联网
weixin_470740361 小时前
python生成环境部署
开发语言·python
piaopiaolanghua1 小时前
Python中的SGP4轨道预报库
python·sgp4
Eiceblue1 小时前
Python 实现 CSV 转 TXT 格式 (单文件 + 批量处理)
开发语言·python·visual studio code
Iridescent11211 小时前
Iridescent:Day49
python
曲幽1 小时前
从安装到上线:一份 Nginx 实战指南,让你的 Web 应用稳建安全
python·nginx·flask·fastapi·web·gunicorn·uvicorn
vibag1 小时前
LangSmith监控
人工智能·python·语言模型·langchain·大模型