mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-07-21 06:07:13 +08:00
feat: client mng
This commit is contained in:
@@ -360,7 +360,9 @@ class LinkAIBot(Bot):
|
|||||||
suffix += f"{turn.get('thought')}\n"
|
suffix += f"{turn.get('thought')}\n"
|
||||||
if plugin_name:
|
if plugin_name:
|
||||||
plugin_list.append(turn.get('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'):
|
if turn.get('plugin_input'):
|
||||||
suffix += f":{turn.get('plugin_input')}"
|
suffix += f":{turn.get('plugin_input')}"
|
||||||
if i < len(chain) - 1:
|
if i < len(chain) - 1:
|
||||||
@@ -385,12 +387,15 @@ class LinkAIBot(Bot):
|
|||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
for url in image_urls:
|
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)
|
channel.send(reply, context)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
|
|
||||||
|
|
||||||
class LinkAISessionManager(SessionManager):
|
class LinkAISessionManager(SessionManager):
|
||||||
def session_msg_query(self, query, session_id):
|
def session_msg_query(self, query, session_id):
|
||||||
session = self.build_session(session_id)
|
session = self.build_session(session_id)
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ def qrCallback(uuid, status, qrcode):
|
|||||||
print(qr_api4)
|
print(qr_api4)
|
||||||
print(qr_api2)
|
print(qr_api2)
|
||||||
print(qr_api1)
|
print(qr_api1)
|
||||||
|
_send_qr_code([qr_api1, qr_api2, qr_api3, qr_api4])
|
||||||
qr = qrcode.QRCode(border=1)
|
qr = qrcode.QRCode(border=1)
|
||||||
qr.add_data(url)
|
qr.add_data(url)
|
||||||
qr.make(fit=True)
|
qr.make(fit=True)
|
||||||
@@ -131,12 +131,15 @@ class WechatChannel(ChatChannel):
|
|||||||
itchat.run()
|
itchat.run()
|
||||||
|
|
||||||
def exitCallback(self):
|
def exitCallback(self):
|
||||||
|
_send_logout()
|
||||||
|
time.sleep(3)
|
||||||
self.auto_login_times += 1
|
self.auto_login_times += 1
|
||||||
if self.auto_login_times < 100:
|
if self.auto_login_times < 100:
|
||||||
self.startup()
|
self.startup()
|
||||||
|
|
||||||
def loginCallback(self):
|
def loginCallback(self):
|
||||||
pass
|
logger.debug("Login success")
|
||||||
|
_send_login_success()
|
||||||
|
|
||||||
# handle_* 系列函数处理收到的消息后构造Context,然后传入produce函数中处理Context和发送回复
|
# handle_* 系列函数处理收到的消息后构造Context,然后传入produce函数中处理Context和发送回复
|
||||||
# Context包含了消息的所有信息,包括以下属性
|
# Context包含了消息的所有信息,包括以下属性
|
||||||
@@ -149,7 +152,6 @@ class WechatChannel(ChatChannel):
|
|||||||
# msg: ChatMessage消息对象
|
# msg: ChatMessage消息对象
|
||||||
# origin_ctype: 原始消息类型,语音转文字后,私聊时如果匹配前缀失败,会根据初始消息是否是语音来放宽触发规则
|
# origin_ctype: 原始消息类型,语音转文字后,私聊时如果匹配前缀失败,会根据初始消息是否是语音来放宽触发规则
|
||||||
# desire_rtype: 希望回复类型,默认是文本回复,设置为ReplyType.VOICE是语音回复
|
# desire_rtype: 希望回复类型,默认是文本回复,设置为ReplyType.VOICE是语音回复
|
||||||
|
|
||||||
@time_checker
|
@time_checker
|
||||||
@_check
|
@_check
|
||||||
def handle_single(self, cmsg: ChatMessage):
|
def handle_single(self, cmsg: ChatMessage):
|
||||||
@@ -245,3 +247,24 @@ class WechatChannel(ChatChannel):
|
|||||||
video_storage.seek(0)
|
video_storage.seek(0)
|
||||||
itchat.send_video(video_storage, toUserName=receiver)
|
itchat.send_video(video_storage, toUserName=receiver)
|
||||||
logger.info("[WX] sendVideo url={}, receiver={}".format(video_url, 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
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ from common.log import logger
|
|||||||
from linkai import LinkAIClient, PushMsg
|
from linkai import LinkAIClient, PushMsg
|
||||||
from config import conf
|
from config import conf
|
||||||
|
|
||||||
|
chat_client: LinkAIClient
|
||||||
|
|
||||||
class ChatClient(LinkAIClient):
|
class ChatClient(LinkAIClient):
|
||||||
def __init__(self, api_key, host, channel):
|
def __init__(self, api_key, host, channel):
|
||||||
@@ -23,6 +24,7 @@ class ChatClient(LinkAIClient):
|
|||||||
|
|
||||||
|
|
||||||
def start(channel):
|
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)
|
host="link-ai.chat", channel=channel)
|
||||||
client.start()
|
chat_client.start()
|
||||||
|
|||||||
Reference in New Issue
Block a user