python飞书群机器人通过webhook发送消息
import json
import loggingimport requestslogger = 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_urlself.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)passdef 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()