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()
相关推荐
秦ぅ时44 分钟前
保姆级教程|OpenAI tts-1-hd模型调用全流程(Python+curl+懒人用法)
开发语言·python
Muyuan19981 小时前
25.Paper RAG Agent 优化记录:上传反馈、计算器安全与 Chunk 参数调整
python·安全·django·sqlite·fastapi
Java面试题总结1 小时前
使用 Python 设置 Excel 数据验证
开发语言·python·excel
沫儿笙1 小时前
安川机器人焊接节气装置
人工智能·机器人
小郑加油1 小时前
python学习Day10天:列表进阶 + 内置函数 + 代码简化
开发语言·python·学习
时空系1 小时前
第13篇:综合实战——制作我的小游戏 python中文编程
开发语言·python·ai编程
Li emily2 小时前
港股api接入指南:实时行情与历史数据获取
python·api·fastapi
AI技术增长2 小时前
Pytorch图像去噪实战(十三):DDIM加速扩散模型采样,让去噪从1000步降到50步
人工智能·pytorch·python
刀法如飞2 小时前
Python列表去重:从新手三连到高阶特技,20种解法全收录
python·算法·编程语言
小糖学代码2 小时前
LLM系列:1.python入门:16.正则表达式与文本处理 (re)
人工智能·pytorch·python·深度学习·神经网络·正则表达式