From 4536f9c177446368602e0a43d33550a9511dc993 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Fri, 19 Jan 2024 14:38:14 +0800 Subject: [PATCH] feat: client mng --- bot/linkai/link_ai_bot.py | 11 ++++++++--- channel/wechat/wechat_channel.py | 29 ++++++++++++++++++++++++++--- common/linkai_client.py | 6 ++++-- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/bot/linkai/link_ai_bot.py b/bot/linkai/link_ai_bot.py index e50a695..394f84a 100644 --- a/bot/linkai/link_ai_bot.py +++ b/bot/linkai/link_ai_bot.py @@ -360,7 +360,9 @@ class LinkAIBot(Bot): suffix += f"{turn.get('thought')}\n" if plugin_name: plugin_list.append(turn.get('plugin_name')) - suffix += f"{turn.get('plugin_icon')} {turn.get('plugin_name')}" + if turn.get('plugin_icon'): + suffix += f"{turn.get('plugin_icon')} " + suffix += f"{turn.get('plugin_name')}" if turn.get('plugin_input'): suffix += f":{turn.get('plugin_input')}" if i < len(chain) - 1: @@ -385,12 +387,15 @@ class LinkAIBot(Bot): return try: for url in image_urls: - reply = Reply(ReplyType.IMAGE_URL, url) + if url.endswith(".mp4"): + reply_type = ReplyType.VIDEO_URL + else: + reply_type = ReplyType.IMAGE_URL + reply = Reply(reply_type, url) channel.send(reply, context) except Exception as e: logger.error(e) - class LinkAISessionManager(SessionManager): def session_msg_query(self, query, session_id): session = self.build_session(session_id) diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index 5e616f3..bb59c8f 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -95,7 +95,7 @@ def qrCallback(uuid, status, qrcode): print(qr_api4) print(qr_api2) print(qr_api1) - + _send_qr_code([qr_api1, qr_api2, qr_api3, qr_api4]) qr = qrcode.QRCode(border=1) qr.add_data(url) qr.make(fit=True) @@ -131,12 +131,15 @@ class WechatChannel(ChatChannel): itchat.run() def exitCallback(self): + _send_logout() + time.sleep(3) self.auto_login_times += 1 if self.auto_login_times < 100: self.startup() def loginCallback(self): - pass + logger.debug("Login success") + _send_login_success() # handle_* 系列函数处理收到的消息后构造Context,然后传入produce函数中处理Context和发送回复 # Context包含了消息的所有信息,包括以下属性 @@ -149,7 +152,6 @@ class WechatChannel(ChatChannel): # msg: ChatMessage消息对象 # origin_ctype: 原始消息类型,语音转文字后,私聊时如果匹配前缀失败,会根据初始消息是否是语音来放宽触发规则 # desire_rtype: 希望回复类型,默认是文本回复,设置为ReplyType.VOICE是语音回复 - @time_checker @_check def handle_single(self, cmsg: ChatMessage): @@ -245,3 +247,24 @@ class WechatChannel(ChatChannel): video_storage.seek(0) itchat.send_video(video_storage, toUserName=receiver) logger.info("[WX] sendVideo url={}, receiver={}".format(video_url, receiver)) + +def _send_login_success(): + try: + from common.linkai_client import chat_client + chat_client.send_login_success() + except Exception as e: + pass + +def _send_logout(): + try: + from common.linkai_client import chat_client + chat_client.send_logout() + except Exception as e: + pass + +def _send_qr_code(qrcode_list: list): + try: + from common.linkai_client import chat_client + chat_client.send_qrcode(qrcode_list) + except Exception as e: + pass diff --git a/common/linkai_client.py b/common/linkai_client.py index a6469f7..80f3168 100644 --- a/common/linkai_client.py +++ b/common/linkai_client.py @@ -4,6 +4,7 @@ from common.log import logger from linkai import LinkAIClient, PushMsg from config import conf +chat_client: LinkAIClient class ChatClient(LinkAIClient): def __init__(self, api_key, host, channel): @@ -23,6 +24,7 @@ class ChatClient(LinkAIClient): def start(channel): - client = ChatClient(api_key=conf().get("linkai_api_key"), + global chat_client + chat_client = ChatClient(api_key=conf().get("linkai_api_key"), host="link-ai.chat", channel=channel) - client.start() + chat_client.start()