diff --git a/.gitignore b/.gitignore index f9c9c6b..aa03009 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .DS_Store .idea -__pycache__ +__pycache__/ venv +*.pyc diff --git a/bot/__pycache__/bot.cpython-36.pyc b/bot/__pycache__/bot.cpython-36.pyc deleted file mode 100644 index a006b42..0000000 Binary files a/bot/__pycache__/bot.cpython-36.pyc and /dev/null differ diff --git a/bot/__pycache__/bot_factory.cpython-36.pyc b/bot/__pycache__/bot_factory.cpython-36.pyc deleted file mode 100644 index 68002af..0000000 Binary files a/bot/__pycache__/bot_factory.cpython-36.pyc and /dev/null differ diff --git a/bot/baidu/__pycache__/baidu_unit_bot.cpython-36.pyc b/bot/baidu/__pycache__/baidu_unit_bot.cpython-36.pyc deleted file mode 100644 index d453432..0000000 Binary files a/bot/baidu/__pycache__/baidu_unit_bot.cpython-36.pyc and /dev/null differ diff --git a/bot/bot_factory.py b/bot/bot_factory.py index eacf996..7794968 100644 --- a/bot/bot_factory.py +++ b/bot/bot_factory.py @@ -16,4 +16,4 @@ def create_bot(bot_type): return BaiduUnitBot() elif bot_type == 'chatGPT': return ChatGPTBot() - raise RuntimeError \ No newline at end of file + raise RuntimeError diff --git a/bot/chatgpt/chat_gpt_bot.py b/bot/chatgpt/chat_gpt_bot.py index 3bc17a9..2928915 100644 --- a/bot/chatgpt/chat_gpt_bot.py +++ b/bot/chatgpt/chat_gpt_bot.py @@ -4,7 +4,7 @@ from revChatGPT.revChatGPT import Chatbot config = { "Authorization": "", # This is optional - "session_token": "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0.._BniuRSwMqdGn8t2.Hv_C4nsWF1FWnVGxNs19YZOGLDIWFlyC3FeLlTE9HUEgV1M8IFmhz1aH0j40vutVTzoEv3JDSEZN8je4kzewXE5tcKqhQF_Jh0SSHNL52OdRe_tqu_aE61L39NkvxEkmMRP2teXmWtiBIGtrPX3B77IbHIyqx9Kbk17awwl_WJokr39KlWhHE-Mpf9AwP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xkJtt4QuUuiRoK3jTf8T0UbqEZWihtp_quSuyWXHSdtY2XbzHrY3cWeUhdmTsg6VdWQVM7R2BlXj0tbRN3mhpggLXVIeJskp8h7MLOo90DlwX8j2sSWTggVjVLmL39dk2MV0orevquoLmZNg2vP_dYs-w7nJLRI1jdvYw3T87JlEV09gRL_YCzdr5vBx1PcAEkSe6E5W0qI6rXXNZ7DpXoCHAlVVuxgtW_nz7zkCv1twhYYTRCpI6TdIVCOcMU_D06-WYNgflzzqz_Cm2J5lHu4.UeCj_srwxF4V4x92z7lAjA" } chatbot = Chatbot(config) user_session = dict() @@ -13,28 +13,40 @@ last_session_refresh = time.time() class ChatGPTBot(Bot): def reply(self, query, context=None): + from_user_id = context['from_user_id'] + print("[GPT]query={}, user_id={}, session={}".format(query, from_user_id, user_session)) + + now = time.time() + global last_session_refresh + if now - last_session_refresh > 60 * 8: + print('[GPT]session refresh, now={}, last={}'.format(now, last_session_refresh)) + chatbot.refresh_session() + last_session_refresh = now + if from_user_id in user_session: - if time.time() - user_session[from_user_id]['last_reply_time'] < 60 * 3: + if time.time() - user_session[from_user_id]['last_reply_time'] < 60 * 5: chatbot.conversation_id = user_session[from_user_id]['conversation_id'] chatbot.parent_id = user_session[from_user_id]['parent_id'] + else: + chatbot.reset_chat() else: chatbot.reset_chat() - now = time.time() - global last_session_refresh - if now - last_session_refresh > 60 * 10: - chatbot.refresh_session() - last_session_refresh = now + print("[GPT]convId={}, parentId={}".format(chatbot.conversation_id, chatbot.parent_id)) - res = chatbot.get_chat_response(query, output="text") - user_cache = dict() - user_cache['last_reply_time'] = time.time() - user_cache['conversation_id'] = res['conversation_id'] - user_cache['parent_id'] = res['parent_id'] - user_session[from_user_id] = user_cache - print("[GPT]user={}, convId={}, content={}", from_user_id, res['conversation_id'], res['message']) - return res['message'] + try: + res = chatbot.get_chat_response(query, output="text") + print("[GPT]userId={}, res={}".format(from_user_id, res)) + user_cache = dict() + user_cache['last_reply_time'] = time.time() + user_cache['conversation_id'] = res['conversation_id'] + user_cache['parent_id'] = res['parent_id'] + user_session[from_user_id] = user_cache + return res['message'] + except Exception as e: + print(e) + return None diff --git a/bridge/__pycache__/bridge.cpython-36.pyc b/bridge/__pycache__/bridge.cpython-36.pyc deleted file mode 100644 index d25ba0c..0000000 Binary files a/bridge/__pycache__/bridge.cpython-36.pyc and /dev/null differ diff --git a/bridge/bridge.py b/bridge/bridge.py index 6c164e8..2911e2f 100644 --- a/bridge/bridge.py +++ b/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) diff --git a/channel/__pycache__/channel.cpython-36.pyc b/channel/__pycache__/channel.cpython-36.pyc deleted file mode 100644 index d38f44b..0000000 Binary files a/channel/__pycache__/channel.cpython-36.pyc and /dev/null differ diff --git a/channel/__pycache__/channel_factory.cpython-36.pyc b/channel/__pycache__/channel_factory.cpython-36.pyc deleted file mode 100644 index d4c4ddc..0000000 Binary files a/channel/__pycache__/channel_factory.cpython-36.pyc and /dev/null differ diff --git a/channel/wechat/__pycache__/wechat_channel.cpython-36.pyc b/channel/wechat/__pycache__/wechat_channel.cpython-36.pyc deleted file mode 100644 index 835416f..0000000 Binary files a/channel/wechat/__pycache__/wechat_channel.cpython-36.pyc and /dev/null differ diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index 8372b65..46cb4b5 100644 --- a/channel/wechat/wechat_channel.py +++ b/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'])