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()
相关推荐
Metaphor69219 小时前
使用 Python 给 Word 文档设置背景
python·word
2401_8987176619 小时前
Go语言如何用systemd_Go语言systemd服务管理教程【总结】
jvm·数据库·python
X566119 小时前
SQL如何进行复杂逻辑下的分组求和_使用子查询方案
jvm·数据库·python
北冥有羽Victoria19 小时前
Django中间件实战:FBV/CBV日志全兼容
数据库·vscode·后端·python·django·sqlite·开源
ㄟ留恋さ寂寞19 小时前
如何授权AWR报告生成_GRANT SELECT ANY DICTIONARY诊断权限
jvm·数据库·python
_3762715319 小时前
mysql如何实现定时清理缓存数据_利用event scheduler执行
jvm·数据库·python
m0_7485548119 小时前
SQL如何实现多层级分组统计_使用GROUP BY多字段组合
jvm·数据库·python
夕除19 小时前
spring boot--08
开发语言·windows·python
kay...19 小时前
FreeSurfer 核磁共振重建
python·eeg
狐狐生风19 小时前
Python UV 完整安装教程
开发语言·python·uv