From 2473334dfc1de12913b20ee4fe99cfcecbf16c3a Mon Sep 17 00:00:00 2001 From: zhayujie Date: Mon, 14 Aug 2023 23:09:51 +0800 Subject: [PATCH] fix: channel send compatibility and add log --- channel/wechat/wechat_channel.py | 4 ++++ plugins/linkai/midjourney.py | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index 34bb9b8..4d01d92 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -192,10 +192,14 @@ class WechatChannel(ChatChannel): logger.info("[WX] sendFile={}, receiver={}".format(reply.content, receiver)) elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片 img_url = reply.content + logger.debug(f"[WX] start download image, img_url={img_url}") pic_res = requests.get(img_url, stream=True) image_storage = io.BytesIO() + size = 0 for block in pic_res.iter_content(1024): + size += len(block) image_storage.write(block) + logger.info(f"[WX] download image success, size={size}, img_url={img_url}") image_storage.seek(0) itchat.send_image(image_storage, toUserName=receiver) logger.info("[WX] sendImage url={}, receiver={}".format(img_url, receiver)) diff --git a/plugins/linkai/midjourney.py b/plugins/linkai/midjourney.py index 735195a..c6c6cfa 100644 --- a/plugins/linkai/midjourney.py +++ b/plugins/linkai/midjourney.py @@ -310,7 +310,7 @@ class MJBot: # send img reply = Reply(ReplyType.IMAGE_URL, task.img_url) channel = e_context["channel"] - channel._send(reply, e_context["context"]) + _send(channel, reply, e_context["context"]) # send info trigger_prefix = conf().get("plugin_trigger_prefix", "$") @@ -327,7 +327,7 @@ class MJBot: text += f"\n\n🔄使用 {trigger_prefix}mjr 命令重新生成图片\n" text += f"例如:\n{trigger_prefix}mjr {task.img_id}" reply = Reply(ReplyType.INFO, text) - channel._send(reply, e_context["context"]) + _send(channel, reply, e_context["context"]) self._print_tasks() return @@ -406,6 +406,19 @@ class MJBot: return result +def _send(channel, reply: Reply, context, retry_cnt=0): + try: + channel.send(reply, context) + except Exception as e: + logger.error("[WX] sendMsg error: {}".format(str(e))) + if isinstance(e, NotImplementedError): + return + logger.exception(e) + if retry_cnt < 2: + time.sleep(3 + 3 * retry_cnt) + channel.send(reply, context, retry_cnt + 1) + + def check_prefix(content, prefix_list): if not prefix_list: return None