首先需要在飞书开发者页面定制飞书消息卡片,然后将卡片的json复制出来即可。
import json
import requests
from requests_toolbelt import MultipartEncoderclass Send_feishu_msg : app_id = "cli_xxxxxxxxxxxxxxx" app_secret = "nEkQ8A2x13_xxxxxxxxxxx" chatGPT_url = 'https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxxxx' headers = { "Content-Type" : "application/json; charset=utf-8" , } json_data = { "config" : { "wide_screen_mode" : True } , "header" : { "template" : "green" , "title" : { "tag" : "plain_text" , "content" : "📢📢📢xxxxxxxxxxx📢📢📢" } } , "elements" : [ { "tag" : "markdown" , "content" : "📌**xxxxxxxxxxx**" } , { "alt" : { "content" : "" , "tag" : "plain_text" } , "img_key" : "${Picture_1}" , "tag" : "img" , "mode" : "fit_horizontal" , "compact_width" : False } , { "tag" : "hr" } , { "tag" : "markdown" , "content" : "📌**xxxxxxxxxxx**" } , { "tag" : "img" , "img_key" : "${Picture_2}" , "alt" : { "tag" : "plain_text" , "content" : "" } , "mode" : "fit_horizontal" , "preview" : True , "compact_width" : False } , { "tag" : "action" , "actions" : [ { "tag" : "button" , "text" : { "tag" : "plain_text" , "content" : "点击查看更多详情" } , "type" : "primary" , "multi_url" : { "url" : "https://lotuscars.feishu.cn/sheets/xxxxxxxxx" , "pc_url" : "" , "android_url" : "" , "ios_url" : "" } } ] } , { "tag" : "div" , "text" : { "content" : "<at id=all></at>" , "tag" : "lark_md" } } ] } def __init__ ( self) : url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/xxxxxxxx" payload_data = { "app_id" : self. app_id, "app_secret" : self. app_secret} response = requests. post( url= url, data= json. dumps( payload_data) , headers= self. headers) . json( ) self. token = response[ 'tenant_access_token' ] def uploadImage ( self, picture_path) : get_image_key_url = "https://open.feishu.cn/open-apis/im/v1/xxxxxx" form = { 'image_type' : 'message' , 'image' : ( open ( picture_path, 'rb' ) ) } multi_form = MultipartEncoder( form) image_key_headers = { 'Authorization' : 'Bearer ' + self. token, } image_key_headers[ 'Content-Type' ] = multi_form. content_typeresponse = requests. request( "POST" , get_image_key_url, headers= image_key_headers, data= multi_form) content = json. loads( response. content) img_key = str ( content[ 'data' ] [ 'image_key' ] ) print ( response. headers[ 'X-Tt-Logid' ] ) print ( f'The img_key is { img_key} ' ) return img_keydef send_msg_talk ( self, picture1_img_key, picture2_img_key) : self. json_data[ 'elements' ] [ 1 ] [ 'img_key' ] = picture1_img_keyself. json_data[ 'elements' ] [ 4 ] [ 'img_key' ] = picture2_img_keymsg_card = json. dumps( self. json_data) body = json. dumps( { "msg_type" : "interactive" , "card" : msg_card} ) response = requests. post( url= self. chatGPT_url, data= body, headers= self. headers) print ( response)