formatting code

This commit is contained in:
lanvent
2023-03-12 01:22:49 +08:00
parent 9ae7b7773e
commit 9e07703eb1
3 changed files with 83 additions and 80 deletions

View File

@@ -136,9 +136,11 @@ class ChatGPTBot(Bot):
logger.exception(e) logger.exception(e)
return False, str(e) return False, str(e)
class SessionManager(object): class SessionManager(object):
def __init__(self): def __init__(self):
self.sessions = {} self.sessions = {}
def build_session_query(self, query, session_id): def build_session_query(self, query, session_id):
''' '''
build query with conversation history build query with conversation history

View File

@@ -2,6 +2,7 @@ from bot import bot_factory
from common.singleton import singleton from common.singleton import singleton
from voice import voice_factory from voice import voice_factory
@singleton @singleton
class Bridge(object): class Bridge(object):
def __init__(self): def __init__(self):
@@ -15,7 +16,6 @@ class Bridge(object):
except ModuleNotFoundError as e: except ModuleNotFoundError as e:
print(e) print(e)
# 以下所有函数需要得到一个reply字典格式如下 # 以下所有函数需要得到一个reply字典格式如下
# reply["type"] = "ERROR" / "TEXT" / "VOICE" / ... # reply["type"] = "ERROR" / "TEXT" / "VOICE" / ...
# reply["content"] = reply的内容 # reply["content"] = reply的内容

View File

@@ -68,7 +68,6 @@ class WechatChannel(Channel):
context['session_id'] = other_user_id context['session_id'] = other_user_id
thread_pool.submit(self.handle, context) thread_pool.submit(self.handle, context)
def handle_text(self, msg): def handle_text(self, msg):
logger.debug("[WX]receive text msg: " + json.dumps(msg, ensure_ascii=False)) logger.debug("[WX]receive text msg: " + json.dumps(msg, ensure_ascii=False))
content = msg['Text'] content = msg['Text']
@@ -96,7 +95,6 @@ class WechatChannel(Channel):
context['content'] = content context['content'] = content
thread_pool.submit(self.handle, context) thread_pool.submit(self.handle, context)
def handle_group(self, msg): def handle_group(self, msg):
logger.debug("[WX]receive group msg: " + json.dumps(msg, ensure_ascii=False)) logger.debug("[WX]receive group msg: " + json.dumps(msg, ensure_ascii=False))
group_name = msg['User'].get('NickName', None) group_name = msg['User'].get('NickName', None)
@@ -129,8 +127,8 @@ class WechatChannel(Channel):
context['content'] = content context['content'] = content
group_chat_in_one_session = conf().get('group_chat_in_one_session', []) group_chat_in_one_session = conf().get('group_chat_in_one_session', [])
if ('ALL_GROUP' in group_chat_in_one_session or \ if ('ALL_GROUP' in group_chat_in_one_session or
group_name in group_chat_in_one_session or \ group_name in group_chat_in_one_session or
check_contain(group_name, group_chat_in_one_session)): check_contain(group_name, group_chat_in_one_session)):
context['session_id'] = group_id context['session_id'] = group_id
else: else:
@@ -194,7 +192,9 @@ class WechatChannel(Channel):
if reply['type'] == 'TEXT': if reply['type'] == 'TEXT':
reply_text = reply['content'] reply_text = reply['content']
if context['isgroup']: if context['isgroup']:
reply_text = '@' + context['msg']['ActualNickName'] + ' ' + reply_text.strip() reply_text = '@' + \
context['msg']['ActualNickName'] + \
' ' + reply_text.strip()
reply_text = conf().get("group_chat_reply_prefix", "")+reply_text reply_text = conf().get("group_chat_reply_prefix", "")+reply_text
else: else:
reply_text = conf().get("single_chat_reply_prefix", "")+reply_text reply_text = conf().get("single_chat_reply_prefix", "")+reply_text
@@ -204,10 +204,12 @@ class WechatChannel(Channel):
elif reply['type'] == 'IMAGE_URL' or reply['type'] == 'VOICE': elif reply['type'] == 'IMAGE_URL' or reply['type'] == 'VOICE':
pass pass
else: else:
logger.error('[WX] unknown reply type: {}'.format(reply['type'])) logger.error(
'[WX] unknown reply type: {}'.format(reply['type']))
return return
if reply: if reply:
logger.debug('[WX] ready to send reply: {} to {}'.format(reply,context['receiver'])) logger.debug('[WX] ready to send reply: {} to {}'.format(
reply, context['receiver']))
self.send(reply, context['receiver']) self.send(reply, context['receiver'])
@@ -225,4 +227,3 @@ def check_contain(content, keyword_list):
if content.find(ky) != -1: if content.find(ky) != -1:
return True return True
return None return None