@@ -228,9 +228,10 @@ def handle_recv_txt_msg(j):
228
228
wx_id = j ["wxid" ]
229
229
room_id = ""
230
230
content : str = j ["content" ]
231
+ reply = ""
231
232
232
- is_room : bool
233
233
is_ask : bool = False
234
+ is_room : bool
234
235
235
236
chatbot : Chatbot
236
237
@@ -253,44 +254,66 @@ def handle_recv_txt_msg(j):
253
254
is_ask = True
254
255
content = re .sub (groupChatKey , "" , content )
255
256
256
- if autoReply and is_ask and ((not is_room and privateReplyMode ) or (is_room and groupReplyMode )):
257
- if chatbot is None :
258
- chatbot = Chatbot (
259
- rev_config ,
260
- conversation_id = None ,
261
- parent_id = None ,
262
- )
257
+ if autoReply and ((not is_room and privateReplyMode ) or (is_room and groupReplyMode )):
258
+ if is_ask :
259
+ if chatbot is None :
260
+ chatbot = Chatbot (
261
+ rev_config ,
262
+ conversation_id = None ,
263
+ parent_id = None ,
264
+ )
265
+ if is_room :
266
+ global_dict [(wx_id , room_id )] = chatbot
267
+ else :
268
+ global_dict [(wx_id , "" )] = chatbot
269
+
270
+ print ("ask:" + content )
271
+ for data in chatbot .ask (
272
+ prompt = content ,
273
+ ):
274
+ reply += data ["message" ][len (reply ):]
275
+
276
+ elif content .startswith (helpKey ):
263
277
if is_room :
264
- global_dict [(wx_id , room_id )] = chatbot
265
- else :
266
- global_dict [(wx_id , "" )] = chatbot
278
+ reply = str (
279
+ b'\xe6 \xac \xa2 \xe8 \xbf \x8e \xe4 \xbd \xbf \xe7 \x94 \xa8 ChatGPT-weBot\xef \xbc \x8c \xe6 \x9c \xac \xe9 '
280
+ b'\xa1 \xb9 \xe7 \x9b \xae \xe5 \x9c \xa8 github \xe5 \x90 \x8c \xe5 \x90 \x8d \xe5 \xbc \x80 \xe6 \xba \x90 \n ' ,
281
+ 'utf-8' ) + helpKey + " 查看可用命令帮助\n " + groupChatKey + " 唤醒群内机器人\n " + resetChatKey + \
282
+ " 重置上下文\n " + regenerateKey + " 重新生成答案\n " + rollbackKey + " +数字n 回滚到倒数第n个问题"
267
283
268
- print ("ask:" + content )
269
- reply = ""
270
- for data in chatbot .ask (
271
- prompt = content ,
272
- ):
273
- reply += data ["message" ][len (reply ):]
284
+ else :
285
+ reply = str (
286
+ b'\xe6 \xac \xa2 \xe8 \xbf \x8e \xe4 \xbd \xbf \xe7 \x94 \xa8 ChatGPT-weBot\xef \xbc \x8c \xe6 \x9c \xac \xe9 '
287
+ b'\xa1 \xb9 \xe7 \x9b \xae \xe5 \x9c \xa8 github \xe5 \x90 \x8c \xe5 \x90 \x8d \xe5 \xbc \x80 \xe6 \xba \x90 \n ' ,
288
+ 'utf-8' ) + helpKey + " 查看可用命令帮助\n " + privateChatKey + " 唤醒机器人\n " + resetChatKey + \
289
+ " 重置上下文\n " + regenerateKey + " 重新生成答案\n " + rollbackKey + " +数字n 回滚到倒数第n个问题"
290
+ time .sleep (1.5 )
291
+
292
+ elif content .startswith (resetChatKey ):
293
+ if chatbot is not None :
294
+ chatbot .clear_conversations ()
295
+ del (global_dict [(wx_id , room_id )])
296
+ reply = "重置完成"
297
+ else :
298
+ reply = "您还没有开始第一次对话"
299
+ time .sleep (1.5 )
274
300
275
- if is_room :
276
- ws . send ( send_txt_msg ( text_string = reply , wx_id = room_id ))
301
+ elif content . startswith ( regenerateKey ): # todo
302
+ pass
277
303
304
+ elif content .startswith (rollbackKey ): # todo
305
+ pass
278
306
else :
279
- ws .send (send_txt_msg (text_string = reply , wx_id = wx_id ))
280
- print ("reply:" + reply )
281
-
282
- elif content .startswith (resetChatKey ): # todo
283
- pass
284
-
285
- elif content .startswith (regenerateKey ): # todo
286
- pass
287
-
288
- elif content .startswith (rollbackKey ): # todo
289
- pass
290
-
307
+ return
291
308
else :
292
309
return
293
310
311
+ if is_room :
312
+ ws .send (send_txt_msg (text_string = reply .strip (), wx_id = room_id ))
313
+ else :
314
+ ws .send (send_txt_msg (text_string = reply .strip (), wx_id = wx_id ))
315
+ print ("reply:" + reply )
316
+
294
317
295
318
def handle_recv_pic_msg (j ):
296
319
print (j )
@@ -366,9 +389,10 @@ def on_error(ws, error):
366
389
367
390
368
391
def on_close (ws ):
369
- for key , value in global_dict : # todo: still have bugs
392
+ for key , value in global_dict . items () : # todo: still have bugs
370
393
print ("clear conversation id:" + value .parent_id )
371
394
value .clear_conversations ()
395
+ del value
372
396
373
397
print (ws )
374
398
print ("closed" )
0 commit comments