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()
相关推荐
李白的天不白几秒前
config/WebMvcConfig.java
开发语言·python
我血条子呢2 分钟前
飞书缓存移到D盘
缓存·飞书
xwz小王子17 分钟前
MiTaS 多分辨率触觉感知:当机器人学会用“指尖“思考,操作成功率从 31% 飙升到 80%
机器人
Elaine33618 分钟前
基于Django框架的静态个人名片网站设计
后端·python·django·mvt
数智工坊39 分钟前
机器人具身智能“三国杀“:强化学习、VLA与世界模型的技术路线之争
机器人
周杰伦的稻香43 分钟前
解决博客“零评论“困境:AI 评论机器人部署全记录
人工智能·机器人
henrylin99991 小时前
开源量化软件功能介绍
python·开源
C+-C资深大佬1 小时前
Python 新手学习指南
开发语言·python
学机械的鱼鱼1 小时前
一文读懂轮足翼复合机器人:结构特点与仿真学习路线规划
学习·机器人
嫂子的姐夫1 小时前
050-wx小程序合肥住房
爬虫·python·小程序·逆向