mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-07-21 06:07:13 +08:00
wechatmp: adjust log
This commit is contained in:
@@ -62,7 +62,7 @@ class Query:
|
|||||||
channel.running.add(from_user)
|
channel.running.add(from_user)
|
||||||
channel.produce(context)
|
channel.produce(context)
|
||||||
else:
|
else:
|
||||||
trigger_prefix = conf().get("single_chat_prefix", [""])
|
trigger_prefix = conf().get("single_chat_prefix", [""])[0]
|
||||||
if trigger_prefix or not supported:
|
if trigger_prefix or not supported:
|
||||||
if trigger_prefix:
|
if trigger_prefix:
|
||||||
content = textwrap.dedent(
|
content = textwrap.dedent(
|
||||||
@@ -92,13 +92,13 @@ class Query:
|
|||||||
request_cnt = channel.request_cnt.get(message_id, 0) + 1
|
request_cnt = channel.request_cnt.get(message_id, 0) + 1
|
||||||
channel.request_cnt[message_id] = request_cnt
|
channel.request_cnt[message_id] = request_cnt
|
||||||
logger.info(
|
logger.info(
|
||||||
"[wechatmp] Request {} from {} {}\n{}\n{}:{}".format(
|
"[wechatmp] Request {} from {} {} {}:{}\n{}".format(
|
||||||
request_cnt,
|
request_cnt,
|
||||||
from_user,
|
from_user,
|
||||||
message_id,
|
message_id,
|
||||||
message,
|
|
||||||
web.ctx.env.get("REMOTE_ADDR"),
|
web.ctx.env.get("REMOTE_ADDR"),
|
||||||
web.ctx.env.get("REMOTE_PORT"),
|
web.ctx.env.get("REMOTE_PORT"),
|
||||||
|
message
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -168,12 +168,30 @@ class Query:
|
|||||||
elif (reply_type == "voice"):
|
elif (reply_type == "voice"):
|
||||||
media_id = content
|
media_id = content
|
||||||
asyncio.run_coroutine_threadsafe(channel.delete_media(media_id), channel.delete_media_loop)
|
asyncio.run_coroutine_threadsafe(channel.delete_media(media_id), channel.delete_media_loop)
|
||||||
|
logger.info(
|
||||||
|
"[wechatmp] Request {} do send to {} {}: {} voice media_id {}".format(
|
||||||
|
request_cnt,
|
||||||
|
from_user,
|
||||||
|
message_id,
|
||||||
|
message,
|
||||||
|
media_id,
|
||||||
|
)
|
||||||
|
)
|
||||||
replyPost = VoiceMsg(from_user, to_user, media_id).send()
|
replyPost = VoiceMsg(from_user, to_user, media_id).send()
|
||||||
return replyPost
|
return replyPost
|
||||||
|
|
||||||
elif (reply_type == "image"):
|
elif (reply_type == "image"):
|
||||||
media_id = content
|
media_id = content
|
||||||
asyncio.run_coroutine_threadsafe(channel.delete_media(media_id), channel.delete_media_loop)
|
asyncio.run_coroutine_threadsafe(channel.delete_media(media_id), channel.delete_media_loop)
|
||||||
|
logger.info(
|
||||||
|
"[wechatmp] Request {} do send to {} {}: {} image media_id {}".format(
|
||||||
|
request_cnt,
|
||||||
|
from_user,
|
||||||
|
message_id,
|
||||||
|
message,
|
||||||
|
media_id,
|
||||||
|
)
|
||||||
|
)
|
||||||
replyPost = ImageMsg(from_user, to_user, media_id).send()
|
replyPost = ImageMsg(from_user, to_user, media_id).send()
|
||||||
return replyPost
|
return replyPost
|
||||||
|
|
||||||
|
|||||||
@@ -70,24 +70,22 @@ class WechatMPChannel(ChatChannel):
|
|||||||
if self.passive_reply:
|
if self.passive_reply:
|
||||||
if reply.type == ReplyType.TEXT or reply.type == ReplyType.INFO or reply.type == ReplyType.ERROR:
|
if reply.type == ReplyType.TEXT or reply.type == ReplyType.INFO or reply.type == ReplyType.ERROR:
|
||||||
reply_text = reply.content
|
reply_text = reply.content
|
||||||
logger.info("[wechatmp] reply to {} cached:\n{}".format(receiver, reply_text))
|
logger.info("[wechatmp] text cached, receiver {}\n{}".format(receiver, reply_text))
|
||||||
self.cache_dict[receiver] = ("text", reply_text)
|
self.cache_dict[receiver] = ("text", reply_text)
|
||||||
elif reply.type == ReplyType.VOICE:
|
elif reply.type == ReplyType.VOICE:
|
||||||
voice_file_path = reply.content
|
voice_file_path = reply.content
|
||||||
logger.info("[wechatmp] voice file path {}".format(voice_file_path))
|
logger.debug("[wechatmp] voice file path {}".format(voice_file_path))
|
||||||
with open(voice_file_path, 'rb') as f:
|
with open(voice_file_path, 'rb') as f:
|
||||||
filename = receiver + "-" + context["msg"].msg_id + ".mp3"
|
filename = receiver + "-" + context["msg"].msg_id + ".mp3"
|
||||||
media_id = self.client.upload_permanent_media("voice", (filename, f, "audio/mpeg"))
|
media_id = self.client.upload_permanent_media("voice", (filename, f, "audio/mpeg"))
|
||||||
# 根据文件大小估计一个微信自动审核的时间,审核结束前返回将会导致语音无法播放,这个估计有待验证
|
# 根据文件大小估计一个微信自动审核的时间,审核结束前返回将会导致语音无法播放,这个估计有待验证
|
||||||
f_size = os.fstat(f.fileno()).st_size
|
f_size = os.fstat(f.fileno()).st_size
|
||||||
print(f_size)
|
|
||||||
time.sleep(1.0 + 2 * f_size / 1024 / 1024)
|
time.sleep(1.0 + 2 * f_size / 1024 / 1024)
|
||||||
logger.info("[wechatmp] voice reply to {} uploaded: {}".format(receiver, media_id))
|
logger.info("[wechatmp] voice uploaded, receiver {}, media_id {}".format(receiver, media_id))
|
||||||
self.cache_dict[receiver] = ("voice", media_id)
|
self.cache_dict[receiver] = ("voice", media_id)
|
||||||
elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片
|
elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片
|
||||||
img_url = reply.content
|
img_url = reply.content
|
||||||
pic_res = requests.get(img_url, stream=True)
|
pic_res = requests.get(img_url, stream=True)
|
||||||
print(pic_res.headers)
|
|
||||||
image_storage = io.BytesIO()
|
image_storage = io.BytesIO()
|
||||||
for block in pic_res.iter_content(1024):
|
for block in pic_res.iter_content(1024):
|
||||||
image_storage.write(block)
|
image_storage.write(block)
|
||||||
@@ -96,7 +94,7 @@ class WechatMPChannel(ChatChannel):
|
|||||||
filename = receiver + "-" + context["msg"].msg_id + "." + image_type
|
filename = receiver + "-" + context["msg"].msg_id + "." + image_type
|
||||||
content_type = "image/" + image_type
|
content_type = "image/" + image_type
|
||||||
media_id = self.client.upload_permanent_media("image", (filename, image_storage, content_type))
|
media_id = self.client.upload_permanent_media("image", (filename, image_storage, content_type))
|
||||||
logger.info("[wechatmp] image reply to {} uploaded: {}".format(receiver, media_id))
|
logger.info("[wechatmp] image uploaded, receiver {}, media_id {}".format(receiver, media_id))
|
||||||
self.cache_dict[receiver] = ("image", media_id)
|
self.cache_dict[receiver] = ("image", media_id)
|
||||||
elif reply.type == ReplyType.IMAGE: # 从文件读取图片
|
elif reply.type == ReplyType.IMAGE: # 从文件读取图片
|
||||||
image_storage = reply.content
|
image_storage = reply.content
|
||||||
@@ -105,16 +103,16 @@ class WechatMPChannel(ChatChannel):
|
|||||||
filename = receiver + "-" + context["msg"].msg_id + "." + image_type
|
filename = receiver + "-" + context["msg"].msg_id + "." + image_type
|
||||||
content_type = "image/" + image_type
|
content_type = "image/" + image_type
|
||||||
media_id = self.client.upload_permanent_media("image", (filename, image_storage, content_type))
|
media_id = self.client.upload_permanent_media("image", (filename, image_storage, content_type))
|
||||||
logger.info("[wechatmp] image reply to {} uploaded: {}".format(receiver, media_id))
|
logger.info("[wechatmp] image uploaded, receiver {}, media_id {}".format(receiver, media_id))
|
||||||
self.cache_dict[receiver] = ("image", media_id)
|
self.cache_dict[receiver] = ("image", media_id)
|
||||||
else:
|
else:
|
||||||
if reply.type == ReplyType.TEXT or reply.type == ReplyType.INFO or reply.type == ReplyType.ERROR:
|
if reply.type == ReplyType.TEXT or reply.type == ReplyType.INFO or reply.type == ReplyType.ERROR:
|
||||||
reply_text = reply.content
|
reply_text = reply.content
|
||||||
self.client.send_text(receiver, reply_text)
|
self.client.send_text(receiver, reply_text)
|
||||||
logger.info("[wechatmp] Do send to {}: {}".format(receiver, reply_text))
|
logger.info("[wechatmp] Do send text to {}: {}".format(receiver, reply_text))
|
||||||
elif reply.type == ReplyType.VOICE:
|
elif reply.type == ReplyType.VOICE:
|
||||||
voice_file_path = reply.content
|
voice_file_path = reply.content
|
||||||
logger.info("[wechatmp] voice file path {}".format(voice_file_path))
|
logger.debug("[wechatmp] voice file path {}".format(voice_file_path))
|
||||||
with open(voice_file_path, 'rb') as f:
|
with open(voice_file_path, 'rb') as f:
|
||||||
filename = receiver + "-" + context["msg"].msg_id + ".mp3"
|
filename = receiver + "-" + context["msg"].msg_id + ".mp3"
|
||||||
media_id = self.client.upload_media("voice", (filename, f, "audio/mpeg"))
|
media_id = self.client.upload_media("voice", (filename, f, "audio/mpeg"))
|
||||||
@@ -123,7 +121,6 @@ class WechatMPChannel(ChatChannel):
|
|||||||
elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片
|
elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片
|
||||||
img_url = reply.content
|
img_url = reply.content
|
||||||
pic_res = requests.get(img_url, stream=True)
|
pic_res = requests.get(img_url, stream=True)
|
||||||
print(pic_res.headers)
|
|
||||||
image_storage = io.BytesIO()
|
image_storage = io.BytesIO()
|
||||||
for block in pic_res.iter_content(1024):
|
for block in pic_res.iter_content(1024):
|
||||||
image_storage.write(block)
|
image_storage.write(block)
|
||||||
@@ -134,7 +131,7 @@ class WechatMPChannel(ChatChannel):
|
|||||||
# content_type = pic_res.headers.get('content-type')
|
# content_type = pic_res.headers.get('content-type')
|
||||||
media_id = self.client.upload_media("image", (filename, image_storage, content_type))
|
media_id = self.client.upload_media("image", (filename, image_storage, content_type))
|
||||||
self.client.send_image(receiver, media_id)
|
self.client.send_image(receiver, media_id)
|
||||||
logger.info("[wechatmp] sendImage url={}, receiver={}".format(img_url, receiver))
|
logger.info("[wechatmp] Do send image to {}".format(receiver))
|
||||||
elif reply.type == ReplyType.IMAGE: # 从文件读取图片
|
elif reply.type == ReplyType.IMAGE: # 从文件读取图片
|
||||||
image_storage = reply.content
|
image_storage = reply.content
|
||||||
image_storage.seek(0)
|
image_storage.seek(0)
|
||||||
@@ -143,7 +140,7 @@ class WechatMPChannel(ChatChannel):
|
|||||||
content_type = "image/" + image_type
|
content_type = "image/" + image_type
|
||||||
media_id = self.client.upload_media("image", (filename, image_storage, content_type))
|
media_id = self.client.upload_media("image", (filename, image_storage, content_type))
|
||||||
self.client.send_image(receiver, media_id)
|
self.client.send_image(receiver, media_id)
|
||||||
logger.info("[wechatmp] sendImage, receiver={}".format(receiver))
|
logger.info("[wechatmp] Do send image to {}".format(receiver))
|
||||||
return
|
return
|
||||||
|
|
||||||
def _success_callback(self, session_id, context, **kwargs): # 线程异常结束时的回调函数
|
def _success_callback(self, session_id, context, **kwargs): # 线程异常结束时的回调函数
|
||||||
|
|||||||
Reference in New Issue
Block a user