fix: retry when send failed

This commit is contained in:
lanvent
2023-03-30 00:23:57 +08:00
parent 349115b948
commit 62df27eaa1

View File

@@ -225,30 +225,36 @@ class WechatChannel(Channel):
thread_pool.submit(self.handle, context).add_done_callback(thread_pool_callback) thread_pool.submit(self.handle, context).add_done_callback(thread_pool_callback)
# 统一的发送函数每个Channel自行实现根据reply的type字段发送不同类型的消息 # 统一的发送函数每个Channel自行实现根据reply的type字段发送不同类型的消息
def send(self, reply: Reply, receiver): def send(self, reply: Reply, receiver, retry_cnt = 0):
if reply.type == ReplyType.TEXT: try:
itchat.send(reply.content, toUserName=receiver) if reply.type == ReplyType.TEXT:
logger.info('[WX] sendMsg={}, receiver={}'.format(reply, receiver)) itchat.send(reply.content, toUserName=receiver)
elif reply.type == ReplyType.ERROR or reply.type == ReplyType.INFO: logger.info('[WX] sendMsg={}, receiver={}'.format(reply, receiver))
itchat.send(reply.content, toUserName=receiver) elif reply.type == ReplyType.ERROR or reply.type == ReplyType.INFO:
logger.info('[WX] sendMsg={}, receiver={}'.format(reply, receiver)) itchat.send(reply.content, toUserName=receiver)
elif reply.type == ReplyType.VOICE: logger.info('[WX] sendMsg={}, receiver={}'.format(reply, receiver))
itchat.send_file(reply.content, toUserName=receiver) elif reply.type == ReplyType.VOICE:
logger.info('[WX] sendFile={}, receiver={}'.format(reply.content, receiver)) itchat.send_file(reply.content, toUserName=receiver)
elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片 logger.info('[WX] sendFile={}, receiver={}'.format(reply.content, receiver))
img_url = reply.content elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片
pic_res = requests.get(img_url, stream=True) img_url = reply.content
image_storage = io.BytesIO() pic_res = requests.get(img_url, stream=True)
for block in pic_res.iter_content(1024): image_storage = io.BytesIO()
image_storage.write(block) for block in pic_res.iter_content(1024):
image_storage.seek(0) image_storage.write(block)
itchat.send_image(image_storage, toUserName=receiver) image_storage.seek(0)
logger.info('[WX] sendImage url={}, receiver={}'.format(img_url,receiver)) itchat.send_image(image_storage, toUserName=receiver)
elif reply.type == ReplyType.IMAGE: # 从文件读取图片 logger.info('[WX] sendImage url={}, receiver={}'.format(img_url,receiver))
image_storage = reply.content elif reply.type == ReplyType.IMAGE: # 从文件读取图片
image_storage.seek(0) image_storage = reply.content
itchat.send_image(image_storage, toUserName=receiver) image_storage.seek(0)
logger.info('[WX] sendImage, receiver={}'.format(receiver)) itchat.send_image(image_storage, toUserName=receiver)
logger.info('[WX] sendImage, receiver={}'.format(receiver))
except Exception as e:
logger.error('[WX] sendMsg error: {}, receiver={}'.format(e, receiver))
if retry_cnt < 2:
time.sleep(3+3*retry_cnt)
self.send(reply, receiver, retry_cnt + 1)
# 处理消息 TODO: 如果wechaty解耦此处逻辑可以放置到父类 # 处理消息 TODO: 如果wechaty解耦此处逻辑可以放置到父类
def handle(self, context): def handle(self, context):