zhayujie пре 3 година
родитељ
комит
b17f49f96b
6 измењених фајлова са 17 додато и 10 уклоњено
  1. 1 1
      bot/baidu/baidu_unit_bot.py
  2. 1 1
      bot/bot.py
  3. 0 0
      bot/chatgpt/chat_gpt_bot.py
  4. 2 2
      bridge/bridge.py
  5. 2 2
      channel/channel.py
  6. 11 4
      channel/wechat/wechat_channel.py

+ 1 - 1
bot/baidu/baidu_unit_bot.py

@@ -5,7 +5,7 @@ from bot.bot import Bot
 
 
 class BaiduUnitBot(Bot):
-    def reply(self, query):
+    def reply(self, query, context=None):
         token = self.get_token()
         url = 'https://aip.baidubce.com/rpc/2.0/unit/service/v3/chat?access_token=' + token
         post_data = "{\"version\":\"3.0\",\"service_id\":\"S73177\",\"session_id\":\"\",\"log_id\":\"7758521\",\"skill_ids\":[\"1221886\"],\"request\":{\"terminal_id\":\"88888\",\"query\":\"" + query + "\", \"hyper_params\": {\"chat_custom_bot_profile\": 1}}}"

+ 1 - 1
bot/bot.py

@@ -4,7 +4,7 @@ Auto-replay chat robot abstract class
 
 
 class Bot(object):
-    def reply(self, query):
+    def reply(self, query, context=None):
         """
         bot auto-reply content
         :param req: received message

Разлика између датотеке није приказан због своје велике величине
+ 0 - 0
bot/chatgpt/chat_gpt_bot.py


+ 2 - 2
bridge/bridge.py

@@ -5,5 +5,5 @@ class Bridge(object):
     def __init__(self):
         pass
 
-    def fetch_reply_content(self, query):
-        return bot_factory.create_bot("chatGPT").reply(query)
+    def fetch_reply_content(self, query, context):
+        return bot_factory.create_bot("chatGPT").reply(query, context)

+ 2 - 2
channel/channel.py

@@ -27,5 +27,5 @@ class Channel(object):
         """
         raise NotImplementedError
 
-    def build_reply_content(self, query):
-        return Bridge().fetch_reply_content(query)
+    def build_reply_content(self, query, context=None):
+        return Bridge().fetch_reply_content(query, context)

+ 11 - 4
channel/wechat/wechat_channel.py

@@ -5,7 +5,9 @@ import itchat
 import json
 from itchat.content import *
 from channel.channel import Channel
+from concurrent.futures import ThreadPoolExecutor
 
+thead_pool = ThreadPoolExecutor(max_workers=8)
 
 @itchat.msg_register([TEXT])
 def handler_receive_msg(msg):
@@ -18,20 +20,25 @@ class WechatChannel(Channel):
 
     def startup(self):
         # login by scan QRCode
-        itchat.auto_login()
+        itchat.auto_login(enableCmdQR=2)
 
         # start message listener
         itchat.run()
 
     def handle(self, msg):
-        print("handle: ", msg)
-        print(json.dumps(msg, ensure_ascii=False))
+        # print("handle: ", msg)
+        print("[WX]receive msg: " + json.dumps(msg, ensure_ascii=False))
         from_user_id = msg['FromUserName']
         other_user_id = msg['User']['UserName']
         if from_user_id == other_user_id:
-            self.send(super().build_reply_content(msg['Text']), from_user_id)
+            thead_pool.submit(self._do_send, msg['Text'], from_user_id)
 
     def send(self, msg, receiver):
         # time.sleep(random.randint(1, 3))
         print(msg, receiver)
         itchat.send(msg + " [bot]", toUserName=receiver)
+
+    def _do_send(self, send_msg, reply_user_id):
+        context = dict()
+        context['from_user_id'] = reply_user_id
+        self.send(super().build_reply_content(send_msg, context), reply_user_id)

Неке датотеке нису приказане због велике количине промена