@@ -161,22 +161,72 @@ def __init__(self, dingtalk_client, incoming_message):
161161 self .title = None
162162 self .logo = None
163163 self .markdown = ""
164+ self .static_markdown = ""
165+ self .button_list = None
164166 self .inputing_status = False
167+ self .order = [
168+ "msgTitle" ,
169+ "msgContent" ,
170+ "staticMsgContent" ,
171+ "msgTextList" ,
172+ "msgImages" ,
173+ "msgSlider" ,
174+ "msgButtons" ,
175+ ]
165176
166177 def set_title_and_logo (self , title : str , logo : str ):
167178 self .title = title
168179 self .logo = logo
169180
181+ def set_order (self , order : list ):
182+ self .order = order
183+
184+ def get_card_data (self , flow_status = None ):
185+ card_data = {
186+ "msgContent" : self .markdown ,
187+ "staticMsgContent" : self .static_markdown ,
188+ }
189+
190+ if flow_status is not None :
191+ card_data ["flowStatus" ] = flow_status
192+
193+ if self .title is not None and self .title != "" :
194+ card_data ["msgTitle" ] = self .title
195+
196+ if self .logo is not None and self .logo != "" :
197+ card_data ["logo" ] = self .logo
198+
199+ sys_full_json_obj = {
200+ "order" : self .order ,
201+ }
202+
203+ if self .button_list is not None and len (self .button_list ) > 0 :
204+ sys_full_json_obj ["msgButtons" ] = self .button_list
205+
206+ if self .incoming_message .hosting_context is not None :
207+ sys_full_json_obj ["source" ] = {
208+ "text" : "由{nick}的数字助理回答" .format (nick = self .incoming_message .hosting_context .nick )
209+ }
210+
211+ card_data ["sys_full_json_obj" ] = json .dumps (sys_full_json_obj )
212+
213+ return card_data
214+
170215 def ai_start (self , recipients : list = None , support_forward : bool = True ):
171216 """
172217 开始执行中
173218 :return:
174219 """
220+ if self .card_instance_id is not None and self .card_instance_id != "" :
221+ return
222+
175223 self .card_instance_id = self .start (self .card_template_id , {}, recipients = recipients ,
176224 support_forward = support_forward )
177225 self .inputing_status = False
178226
179- def ai_streaming (self , markdown : str , append : bool = False ):
227+ def ai_streaming (self ,
228+ markdown : str ,
229+ append : bool = False ):
180230 """
181231 打字机模式
182232 :param append: 两种更新模式,append=true,追加的方式;append=false,全量替换。
@@ -188,30 +238,7 @@ def ai_streaming(self, markdown: str, append: bool = False):
188238 return
189239
190240 if not self .inputing_status :
191- card_data = {
192- "flowStatus" : AICardStatus .INPUTING ,
193- "msgContent" : ""
194- }
195-
196- if self .title is not None and self .title != "" :
197- card_data ["msgTitle" ] = self .title
198-
199- if self .logo is not None and self .logo != "" :
200- card_data ["logo" ] = self .logo
201-
202- order = [
203- "msgTitle" ,
204- "msgContent" ,
205- "msgMarkdown"
206- "msgTextList" ,
207- "msgImages" ,
208- "msgSlider" ,
209- "msgButtons" ,
210- ]
211-
212- card_data ["sys_full_json_obj" ] = json .dumps ({"order" : order })
213-
214- self .put_card_data (self .card_instance_id , card_data )
241+ self .put_card_data (self .card_instance_id , self .get_card_data (AICardStatus .INPUTING ))
215242
216243 self .inputing_status = True
217244
@@ -223,10 +250,10 @@ def ai_streaming(self, markdown: str, append: bool = False):
223250 self .streaming (self .card_instance_id , "msgContent" , self .markdown , append = False , finished = False ,
224251 failed = False )
225252
226- def update (self , markdown : str = "" , button_list : list = None , tips : str = "" ):
227- return self . ai_finish ( markdown = markdown , button_list = button_list , tips = tips )
228-
229- def ai_finish ( self , markdown : str = "" , button_list : list = None , tips : str = "" ):
253+ def ai_finish (self ,
254+ markdown : str = None ,
255+ button_list : list = None ,
256+ tips : str = "" ):
230257 """
231258 完成态
232259 :param tips:
@@ -238,43 +265,36 @@ def ai_finish(self, markdown: str = "", button_list: list = None, tips: str = ""
238265 self .logger .error ('AIMarkdownCardInstance.ai_finish failed, you should send card first.' )
239266 return
240267
241- if markdown == "" or markdown is None :
242- markdown = self .markdown
243- else :
268+ if markdown is not None :
244269 self .markdown = markdown
245270
246- sys_full_json_obj = {
247- "order" : [
248- "msgTitle" ,
249- "msgContent" ,
250- "msgMarkdown"
251- "msgTextList" ,
252- "msgImages" ,
253- "msgSlider" ,
254- "msgButtons" ,
255- ],
256- }
257-
258- if button_list is not None and len (button_list ) > 0 :
259- sys_full_json_obj ["msgButtons" ] = button_list
271+ if button_list is not None :
272+ self .button_list = button_list
260273
261- if self .incoming_message .hosting_context is not None :
262- sys_full_json_obj ["source" ] = {
263- "text" : "由{nick}的数字助理回答" .format (nick = self .incoming_message .hosting_context .nick )
264- }
274+ self .finish (self .card_instance_id , self .get_card_data ())
265275
266- card_data = {
267- "msgContent" : markdown ,
268- "sys_full_json_obj" : json .dumps (sys_full_json_obj )
269- }
276+ def update (self ,
277+ static_markdown : str = None ,
278+ button_list : list = None ,
279+ tips : str = "" ):
280+ """
281+ 非流式内容输出
282+ :param static_markdown:
283+ :param button_list:
284+ :param tips:
285+ :return:
286+ """
287+ if self .card_instance_id is None or self .card_instance_id == "" :
288+ self .logger .error ('AIMarkdownCardInstance.update failed, you should send card first.' )
289+ return
270290
271- if self . title is not None and self . title != "" :
272- card_data [ "msgTitle" ] = self . title
291+ if button_list is not None :
292+ self . button_list = button_list
273293
274- if self . logo is not None and self . logo != "" :
275- card_data [ "logo" ] = self . logo
294+ if static_markdown is not None :
295+ self . static_markdown = static_markdown
276296
277- self .finish (self .card_instance_id , card_data )
297+ self .finish (self .card_instance_id , self . get_card_data () )
278298
279299 def ai_fail (self ):
280300 """
@@ -339,7 +359,7 @@ def reply(self,
339359 sys_full_json_obj = {
340360 "order" : [
341361 "msgTitle" ,
342- "msgMarkdown " ,
362+ "staticMsgContent " ,
343363 "msgSlider" ,
344364 "msgImages" ,
345365 "msgTextList" ,
@@ -366,7 +386,7 @@ def reply(self,
366386 })
367387
368388 card_data = {
369- "msgMarkdown " : markdown ,
389+ "staticMsgContent " : markdown ,
370390 "sys_full_json_obj" : json .dumps (sys_full_json_obj )
371391 }
372392
0 commit comments