Przeglądaj źródła

feat: wechat group reply

zhayujie 3 lat temu
rodzic
commit
a1f45344b6

+ 2 - 1
.gitignore

@@ -1,4 +1,5 @@
 .DS_Store
 .idea
-__pycache__
+__pycache__/
 venv
+*.pyc

BIN
bot/__pycache__/bot.cpython-36.pyc


BIN
bot/__pycache__/bot_factory.cpython-36.pyc


BIN
bot/baidu/__pycache__/baidu_unit_bot.cpython-36.pyc


+ 1 - 1
bot/bot_factory.py

@@ -16,4 +16,4 @@ def create_bot(bot_type):
         return BaiduUnitBot()
     elif bot_type == 'chatGPT':
         return ChatGPTBot()
-    raise RuntimeError
+    raise RuntimeError

Plik diff jest za duży
+ 0 - 0
bot/chatgpt/chat_gpt_bot.py


BIN
bridge/__pycache__/bridge.cpython-36.pyc


+ 1 - 0
bridge/bridge.py

@@ -7,3 +7,4 @@ class Bridge(object):
 
     def fetch_reply_content(self, query, context):
         return bot_factory.create_bot("chatGPT").reply(query, context)
+        # return bot_factory.create_bot("baidu").reply(query, context)

BIN
channel/__pycache__/channel.cpython-36.pyc


BIN
channel/__pycache__/channel_factory.cpython-36.pyc


BIN
channel/wechat/__pycache__/wechat_channel.cpython-36.pyc


+ 46 - 8
channel/wechat/wechat_channel.py

@@ -9,11 +9,17 @@ from concurrent.futures import ThreadPoolExecutor
 
 thead_pool = ThreadPoolExecutor(max_workers=8)
 
-@itchat.msg_register([TEXT])
-def handler_receive_msg(msg):
+
+@itchat.msg_register(TEXT)
+def handler_single_msg(msg):
     WechatChannel().handle(msg)
 
 
+@itchat.msg_register(TEXT, isGroupChat=True)
+def handler_group_msg(msg):
+    WechatChannel().handle_group(msg)
+
+
 class WechatChannel(Channel):
     def __init__(self):
         pass
@@ -26,19 +32,51 @@ class WechatChannel(Channel):
         itchat.run()
 
     def handle(self, msg):
-        # 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:
-            thead_pool.submit(self._do_send, msg['Text'], from_user_id)
+        content = msg['Text']
+        if from_user_id == other_user_id and (content.lower().startswith('bot') or content.lower().startswith('@bot')):
+            str_list = content.split('bot', 1)
+            if len(str_list) == 2:
+                content = str_list[1].strip()
+            thead_pool.submit(self._do_send, content, from_user_id)
+
+    def handle_group(self, msg):
+        group_white_list = ['学就完事了', '小宝群', '全天乱斗模式', '戒赌吧', '命苦还要快乐','攒钱让姐妹当小三的组织']
+        print("[WX]receive group msg: " + json.dumps(msg, ensure_ascii=False))
+        group_id = msg['User']['UserName']
+        group_name = msg['User'].get('NickName', None)
+        if not group_name:
+            return ""
+        origin_content =  msg['Content']
+        content = msg['Content']
+        content_list = content.split(' ', 1)
+        context_special_list = content.split('\u2005', 1)
+        if len(context_special_list) == 2:
+            content = context_special_list[1]
+        elif len(content_list) == 2:
+            content = content_list[1]
+
+        if group_name in group_white_list and (msg['IsAt'] or origin_content.lower().startswith('@bot')):
+            thead_pool.submit(self._do_send_group, content, msg)
 
     def send(self, msg, receiver):
         # time.sleep(random.randint(1, 3))
-        print(msg, receiver)
-        itchat.send(msg + " [bot]", toUserName=receiver)
+        print('[WX] sendMsg={}, receiver={}'.format(msg, receiver))
+        itchat.send(msg, 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)
+        content = super().build_reply_content(send_msg, context)
+        if content:
+            self.send("[bot] " + content, reply_user_id)
+
+    def _do_send_group(self, content, msg):
+        context = dict()
+        context['from_user_id'] = msg['ActualUserName']
+        reply_text = super().build_reply_content(content, context)
+        reply_text = '@' + msg['ActualNickName'] + ' ' + reply_text
+        if reply_text:
+            self.send(reply_text, msg['User']['UserName'])

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików