11import requests ,json
2+ import re
3+ import base64
24from .apilist import *
35
46def getme (token :str ) -> requests .models .Response :
@@ -13,19 +15,15 @@ def getme(token:str) ->requests.models.Response:
1315 r = requests .get (apiurl + token + apilist ['getme' ])
1416 return r
1517
16- def sendmessage (token = '' ,chatid = 0 ,biaoti = "标题" ,add_Key = False ,ik = [[{"text" :"下一页" ,"callback_data" :"{\" type\" :\" next\" ,\" index\" :2,\" msg\" :\" 114514\" }" }]],text = '文本' ,biaoticolor = 'ffe4e4' ,type = "card " ,shade = ['ff764a' ,'ffb39aff' ],backgroundColor = 'ddeeff00' ,getjson = False ) -> requests .models .Response | str :
18+ def sendmessage (token = '' ,chatid = 0 ,biaoti = "标题" ,add_Key = False ,ik = [[{"text" :"下一页" ,"callback_data" :"{\" type\" :\" next\" ,\" index\" :2,\" msg\" :\" 114514\" }" }]],text = '文本' ,biaoticolor = 'ffe4e4' ,type = "text " ,shade = ['ff764a' ,'ffb39aff' ],backgroundColor = 'ddeeff00' ,getjson = False ) -> requests .models .Response | str :
1719 """发送消息
1820
1921 Args:
2022 token (str, optional): botToken. Defaults to ''.
2123 chatid (int, optional): 频道id. Defaults to 0.
22- biaoti (str, optional): 卡片标题. Defaults to "标题".
2324 ik (list, optional): 自定义键盘. Defaults to [[{"text":"下一页","callback_data":"{\" type\" :\" next\" ,\" index\" :2,\" msg\" :\" 114514\" }"}]].
24- text (str, optional): 正文内容,如果是card模式,换行符为 4个反斜线n. Defaults to '文本'.
25- biaoticolor (str, optional): 弃用,默认即可. Defaults to 'ffe4e4'.
26- type (str, optional): card(内置卡片)/fanbook(特殊消息解析模式)/text(纯文本). Defaults to "card".
27- shade (list, optional): 标题背景颜色,如果配置两项就是渐变色. Defaults to ['ff764a','ffb39aff'].
28- backgroundColor (str, optional): 标题文本颜色. Defaults to 'ddeeff'.
25+ text (str, optional): 正文内容.
26+ type (str, optional): fanbook(特殊消息解析模式)/text(纯文本). Defaults to "text".
2927 getjson (bool, optional): 设置为True就是只返回编码完成的json,不请求. Defaults to False.
3028 add_Key (bool, optional): 是否添加键盘. Defaults to False.
3129
@@ -39,6 +37,7 @@ def sendmessage(token='',chatid=0,biaoti="标题",add_Key=False,ik=[[{"text":"
3937
4038 url = apiurl + token + apilist ['sendmessage' ]
4139 if type == "card" :
40+ print ("警告:card模式已经被废弃,请使用sendCard函数" )
4241 text1 = "{\" width\" :null,\" height\" :null,\" data\" :\" {\\ \" tag\\ \" :\\ \" column\\ \" ,\\ \" children\\ \" :[{\\ \" tag\\ \" :\\ \" container\\ \" ,\\ \" padding\\ \" :\\ \" 12,7\\ \" ,\\ \" gradient\\ \" :{\\ \" colors\\ \" :[\\ \" " + str (color1 )+ "\\ \" ,\\ \" " + str (color2 )+ "\\ \" ]},\\ \" child\\ \" :{\\ \" tag\\ \" :\\ \" text\\ \" ,\\ \" data\\ \" :\\ \" " + str (biaoti )+ "\\ \" ,\\ \" style\\ \" :{\\ \" color\\ \" :\\ \" #" + str (biaoticolor )+ "\\ \" ,\\ \" fontSize\\ \" :16,\\ \" fontWeight\\ \" :\\ \" medium\\ \" }},\\ \" backgroundColor\\ \" :\\ \" " + backgroundColor + "\\ \" },{\\ \" tag\\ \" :\\ \" container\\ \" ,\\ \" child\\ \" :{\\ \" tag\\ \" :\\ \" column\\ \" ,\\ \" padding\\ \" :\\ \" 12\\ \" ,\\ \" children\\ \" :[{\\ \" tag\\ \" :\\ \" container\\ \" ,\\ \" padding\\ \" :\\ \" 0,0,0,4\\ \" ,\\ \" alignment\\ \" :\\ \" -1,0\\ \" ,\\ \" child\\ \" :{\\ \" tag\\ \" :\\ \" markdown\\ \" ,\\ \" data\\ \" :\\ \" " + str (text )+ "\\ \" }}]},\\ \" backgroundColor\\ \" :\\ \" ffffff\\ \" }],\\ \" crossAxisAlignment\\ \" :\\ \" stretch\\ \" }\" ,\" notification\" :null,\" come_from_icon\" :null,\" come_from_name\" :null,\" template\" :null,\" no_seat_toast\" :null,\" type\" :\" messageCard\" }"
4342 pm = "Fanbook"
4443 elif type == 'fanbook' :
@@ -86,4 +85,121 @@ def getPrivateChat(token:str,userid:int) ->requests.models.Response:
8685 "user_id" :int (userid )
8786 })
8887 postreturn = requests .post (url ,data = jsonfile ,headers = headers )
89- return postreturn
88+ return postreturn
89+
90+ def process_markdown (text ):
91+ # 正则表达式匹配 Markdown 格式的图片
92+ pattern = r'!\[.*?\]\((.*?)\)'
93+
94+ # 找到所有匹配的图片链接
95+ matches = re .finditer (pattern , text )
96+ image_links = [match .group (1 ) for match in matches ]
97+
98+ # 使用正则表达式删除图片
99+ modified_text = re .sub (pattern , '' , text )
100+
101+ # 根据删除后的图片位置分割文本
102+ split_text = re .split (pattern , text )
103+
104+ # 删除分割后的图片链接
105+ for i in range (len (split_text )):
106+ if split_text [i ] in image_links :
107+ split_text [i ]= '[图片]'
108+
109+ return image_links , modified_text , split_text
110+
111+ def sendCard (token :str ,chatid :int ,markdown :str ,color = ['#00afee' ,'#f2f2f2' ],bt = '标题' ,textcolor = '#f2f2f2' ,openbutton = False ,btcolor = '#00afee' ,burl = 'https://www.baidu.com' ,button = '按钮' ,come_from_name = None ,come_from_icon = None ) -> dict :
112+ """发送消息卡片
113+
114+ Args:
115+ token (str): 机器人token
116+ chatid (int): 频道id
117+ markdown (str): markdown文本
118+ color (list, optional): 标题颜色,16进制颜色,渐变色. Defaults to ['#00afee','#f2f2f2'].
119+ bt (str, optional): 标题文本. Defaults to '标题'.
120+ textcolor (str, optional): 标题文本颜色. Defaults to '#f2f2f2'.
121+ openbutton (bool, optional): 是否开启按钮. Defaults to False.
122+ btcolor (str, optional): 按钮颜色. Defaults to '#00afee'.
123+ burl (str, optional): 按钮按下时跳转的url. Defaults to 'https://www.baidu.com'.
124+ button (str, optional): 按钮文本. Defaults to '按钮'.
125+ come_from_name (_type_, optional): 显示在卡片下方的小字,一般用来显示来源服务器名称. Defaults to None.
126+ come_from_icon (_type_, optional): 显示在卡片下方的图标,一般用来显示来源服务器的图标. Defaults to None.
127+
128+ Returns:
129+ dict: fanbook api返回的json
130+ """
131+ imgindex = 0
132+ # fanbook不支持直接在markdown中插入图片,需要先提取图片链接并转换为base64编码,再插入图片组件
133+ image_links , modified_text , split_text = process_markdown (markdown )
134+ data = {
135+ "crossAxisAlignment" : "stretch" ,
136+ "tag" : "column" ,
137+ "children" : [
138+ {
139+ "tag" : "container" ,
140+ "padding" : "12,7" ,
141+ "gradient" : {
142+ "colors" : color
143+ },
144+ "child" : {
145+ "tag" : "text" ,
146+ "data" : bt ,
147+ "style" : {
148+ "color" : textcolor ,
149+ "fontSize" : 16 ,
150+ "fontWeight" : "medium"
151+ }
152+ },
153+ "backgroundColor" : "ddeeff00"
154+ }
155+ ],
156+ }
157+ for i in split_text :
158+ if i == '[图片]' :
159+ img = image_links [imgindex ]
160+ imgindex += 1
161+ # base64编码图片链接
162+ img = base64 .b64encode (img .encode ('utf-8' )).decode ('utf-8' )
163+ data ['children' ].append ({
164+ "tag" : "container" ,
165+ "child" : {
166+ "tag" : "column" ,
167+ "padding" : "12" ,
168+ "children" : [
169+ {
170+ "tag" : "container" ,
171+ "padding" : "12" ,
172+ "child" : {
173+ "tag" : "image" ,
174+ "src" : "1::00::0::" + img ,
175+ }
176+ }
177+ ]
178+ },
179+ "backgroundColor" : "ffffff"
180+ })
181+ else :
182+ data ['children' ].append ({
183+ "tag" : "container" ,
184+ "child" : {
185+ "tag" : "column" ,
186+ "crossAxisAlignment" : "start" ,
187+ "padding" : "12" ,
188+ "children" : [
189+ {
190+ "tag" : "container" ,
191+ "padding" : "0,8,0,0" ,
192+ "child" : {
193+ "tag" : "markdown" ,
194+ "data" : i
195+ }
196+ }
197+ ]
198+ },
199+ "backgroundColor" : "ffffff"
200+ })
201+ if openbutton :
202+ data ['children' ].append ({"tag" :"container" ,"padding" :"12,0,12,12" ,"child" :{"tag" :"button" ,"category" :"outlined" ,"color" :btcolor ,"size" :"medium" ,"widthUnlimited" :True ,"href" :burl ,"label" :button }})
203+ r = sendmessage (token = token ,chatid = chatid ,type = 'fanbook' ,text = json .dumps ({'width' : None , 'height' : None , 'data' :json .dumps (data ) , 'notification' : None , 'come_from_icon' : None , 'come_from_name' : come_from_name , 'template' : None , 'no_seat_toast' : None , 'type' : 'messageCard' })).text
204+ return json .loads (r )
205+
0 commit comments