介绍
这篇文章将为大家详细讲解有关Python如何调用飞书实现发送消息,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
<强>一、创建飞书机器人强>
自定义飞书机器人操作步骤,具体详见飞书官方文档:《机器人|如何在群聊中使用机器人?》
<强>二、调用飞书发送消息强>
自定义机器人添加完成后,就能向其webhook地址发送帖子请求,从而在群聊中推送消息了。支持推送的消息格式有文本,富文,本图片消息,也可以分享群名片等。
参数msg_type代表消息类型,可传入:文本(文本)/,POST(富文本)/,图像(图片)/,share_chat(分享群名片)/,互动(消息卡片),可参照飞书接口文档:https://open.feishu.cn/document/ukTMukTMukTM/uUjNz4SN2MjL1YzM
发送文本消息
请求的消息体示例:
{ “open_id":“ou_5ad573a6411d72b8305fda3a9c15c70e" “root_id":“om_40eb06e7b84dc71c03e009ad3c754195" “chat_id":“oc_5ad11d72b830411d72b836c20" “user_id":“92 e39a99" “email":“fanlv@gmail.com" “msg_type":“text" “content": { “text":“文本content< user_id=\“ou_88a56e7e8e9f680b682f6905cc09098e \“祝辞test极特;“ } }
<>强旋度请求演示强>
curl - x \ https://open.feishu.cn/open-apis/message/v4/send/\ - h & # 39;授权:无记名t-fee42159a366c575f2cd2b2acde2ed1e94c89d5f& # 39;\ - h & # 39; - type: application/json # 39;\ - d & # 39; { “chat_id":“oc_f5b1a7eb27ae2c7b6adc2a74faf339ff" “msg_type":“text" “content": { “text":“文本content< user_id=\“ou_88a56e7e8e9f680b682f6905cc09098e \“祝辞test极特;“ } }& # 39;
<强>使用Python封装飞书请求强>
接下来我们以发送文本格式消息类型,进行以下封装,上代码:
# - * -编码:utf - 8 - * & # 39;& # 39;& # 39; @File: feiShuTalk.py @Time: 2020/11/9 11 @Author: DY @Version: V1.0.0 @Desciption: & # 39;& # 39;& # 39; 进口的要求 进口json 导入日志 导入的时间 进口urllib 进口urllib3 urllib3.disable_warnings () 试一试: JSONDecodeError=json.decoder.JSONDecodeError 除了AttributeError: JSONDecodeError=ValueError def is_not_null_and_blank_str(内容): “““ 非空字符串 :param内容:字符串 返回:非空——真的,空——假的 “““ 如果内容和content.strip (): 还真 其他: 返回假 类FeiShutalkChatbot(对象): def __init__(自我、webhook秘密=None, pc_slide=False, fail_notice=False): & # 39;& # 39;& # 39; 机器人初始化 :param webhook:飞书群自定义机器人webhook地址 :param秘密:机器人安全设置页面勾选“加签”时需要传入的密钥 :param pc_slide:消息链接打开方式,默认假为浏览器打开,设置为真时为PC端侧边栏打开 :param fail_notice:消息发送失败提醒,默认为假不提醒,开发者可以根据返回的消息发送结果自行判断和处理 & # 39;& # 39;& # 39; 超级(FeiShutalkChatbot自我). __init__ () 自我。头={& # 39;内容类型# 39;:& # 39;application/json;charset=utf - 8 # 39;} 自我。webhook=webhook 自我。秘密=秘密 自我。pc_slide=pc_slide 自我。fail_notice=fail_notice def send_text(自我、味精、open_id=[]): “““ 消息类型为文本类型 :param味精:消息内容 返回:返回消息发送结果 “““ data={“msg_type":“text",“at": {}} 如果is_not_null_and_blank_str(味精):#传入味精非空 数据(“content")={“text":味精} 其他: logging.error(“文本类型,消息内容不能为空!“) 提高ValueError(“文本类型,消息内容不能为空!“) logging.debug(& # 39;文本类型:% & # 39;%数据) 返回self.post(数据) def post(自我、数据): “““ 发送消息(内容utf - 8编码) :param数据:消息数据(字典) 返回:返回消息发送结果 “““ 试一试: post_data=json.dumps(数据) 响应=requests.post(自我。webhook header=自我。头,data=post_data验证=False) requests.exceptions除外。HTTPError exc: logging.error(“消息发送失败,HTTP错误:% d,原因:% s"% (exc.response。status_code, exc.response.reason)) 提高 除了requests.exceptions.ConnectionError: logging.error(“消息发送失败,HTTP连接错误!“) 提高 除了requests.exceptions.Timeout: logging.error(“消息发送失败,超时错误!“) 提高 除了requests.exceptions.RequestException: logging.error(“消息发送失败,请求例外!“) 提高 其他: 试一试: 结果=response.json () 除了JSONDecodeError: logging.error(“服务器响应异常,状态码:% s,响应内容:% s"%(响应。status_code, response.text)) 返回{& # 39;errcode& # 39;: 500 & # 39; errmsg& # 39;: & # 39;服务器响应异常& # 39;} 其他: logging.debug(& # 39;发送结果:% & # 39;%的结果) #消息发送失败提醒(errcode不为0,表示消息发送异常),默认不提醒,开发者可以根据返回的消息发送结果自行判断和处理 如果自我。fail_notice和result.get (& # 39; errcode& # 39;, True): time_now=time.strftime (“% Y - % m - H % d %: % m: % S", time.localtime (time.time ())) error_data={ “msgtype":“text" “text": { “content":“[注意,自动通知]飞书机器人消息发送失败,时间:% s,原因:% s,请及时跟进,谢谢!“% ( time_now,结果[& # 39;errmsg& # 39;]如果result.get (& # 39; errmsg& # 39;, False) & # 39;其它未知异常& # 39;) }, “at": { “isAtAll":假的 } } logging.error(“消息发送失败,自动通知:% s"% error_data) requests.post(自我。webhook header=自我。头,data=json.dumps (error_data)) 返回结果Python如何调用飞书实现发送消息