当前位置:首页 > 编程笔记 > 正文
已解决

python飞书群机器人通过webhook发送消息

来自网友在路上 11058105提问 提问时间:2023-11-03 15:30:48阅读次数: 105

最佳答案 问答题库1058位专家为你答疑解惑

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()
查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"python飞书群机器人通过webhook发送消息":http://eshow365.cn/6-31152-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!