|
|
@@ -4,22 +4,20 @@ import os
|
|
|
import time
|
|
|
import imghdr
|
|
|
import requests
|
|
|
+import asyncio
|
|
|
+import threading
|
|
|
+from config import conf
|
|
|
from bridge.context import *
|
|
|
from bridge.reply import *
|
|
|
-from channel.chat_channel import ChatChannel
|
|
|
-from channel.wechatmp.wechatmp_client import WechatMPClient
|
|
|
-from channel.wechatmp.common import *
|
|
|
from common.log import logger
|
|
|
from common.singleton import singleton
|
|
|
-from config import conf
|
|
|
-
|
|
|
+from voice.audio_convert import any_to_mp3
|
|
|
+from channel.chat_channel import ChatChannel
|
|
|
+from channel.wechatmp.common import *
|
|
|
+from channel.wechatmp.wechatmp_client import WechatMPClient
|
|
|
from wechatpy.exceptions import WeChatClientException
|
|
|
-import asyncio
|
|
|
-from threading import Thread
|
|
|
|
|
|
import web
|
|
|
-
|
|
|
-from voice.audio_convert import any_to_mp3
|
|
|
# If using SSL, uncomment the following lines, and modify the certificate path.
|
|
|
# from cheroot.server import HTTPServer
|
|
|
# from cheroot.ssl.builtin import BuiltinSSLAdapter
|
|
|
@@ -46,7 +44,7 @@ class WechatMPChannel(ChatChannel):
|
|
|
self.request_cnt = dict()
|
|
|
# The permanent media need to be deleted to avoid media number limit
|
|
|
self.delete_media_loop = asyncio.new_event_loop()
|
|
|
- t = Thread(target=self.start_loop, args=(self.delete_media_loop,))
|
|
|
+ t = threading.Thread(target=self.start_loop, args=(self.delete_media_loop,))
|
|
|
t.setDaemon(True)
|
|
|
t.start()
|
|
|
|
|
|
@@ -75,20 +73,26 @@ class WechatMPChannel(ChatChannel):
|
|
|
if self.passive_reply:
|
|
|
if reply.type == ReplyType.TEXT or reply.type == ReplyType.INFO or reply.type == ReplyType.ERROR:
|
|
|
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)
|
|
|
elif reply.type == ReplyType.VOICE:
|
|
|
try:
|
|
|
- file_path = reply.content
|
|
|
- response = self.client.material.add("voice", open(file_path, "rb"))
|
|
|
- logger.debug("[wechatmp] upload voice response: {}".format(response))
|
|
|
+ voice_file_path = reply.content
|
|
|
+ with open(voice_file_path, 'rb') as f:
|
|
|
+ # support: <2M, <60s, mp3/wma/wav/amr
|
|
|
+ response = self.client.material.add("voice", f)
|
|
|
+ logger.debug("[wechatmp] upload voice response: {}".format(response))
|
|
|
+ # 根据文件大小估计一个微信自动审核的时间,审核结束前返回将会导致语音无法播放,这个估计有待验证
|
|
|
+ f_size = os.fstat(f.fileno()).st_size
|
|
|
+ time.sleep(1.0 + 2 * f_size / 1024 / 1024)
|
|
|
+ # todo check media_id
|
|
|
except WeChatClientException as e:
|
|
|
logger.error("[wechatmp] upload voice failed: {}".format(e))
|
|
|
return
|
|
|
- time.sleep(3) # todo check media_id
|
|
|
media_id = response["media_id"]
|
|
|
- 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)
|
|
|
+
|
|
|
elif reply.type == ReplyType.IMAGE_URL: # 从网络下载图片
|
|
|
img_url = reply.content
|
|
|
pic_res = requests.get(img_url, stream=True)
|
|
|
@@ -106,9 +110,7 @@ class WechatMPChannel(ChatChannel):
|
|
|
logger.error("[wechatmp] upload image failed: {}".format(e))
|
|
|
return
|
|
|
media_id = response["media_id"]
|
|
|
- logger.info(
|
|
|
- "[wechatmp] image reply url={}, receiver={}".format(img_url, receiver)
|
|
|
- )
|
|
|
+ logger.info("[wechatmp] image uploaded, receiver {}, media_id {}".format(receiver, media_id))
|
|
|
self.cache_dict[receiver] = ("image", media_id)
|
|
|
elif reply.type == ReplyType.IMAGE: # 从文件读取图片
|
|
|
image_storage = reply.content
|
|
|
@@ -123,15 +125,13 @@ class WechatMPChannel(ChatChannel):
|
|
|
logger.error("[wechatmp] upload image failed: {}".format(e))
|
|
|
return
|
|
|
media_id = response["media_id"]
|
|
|
- logger.info(
|
|
|
- "[wechatmp] image reply url={}, receiver={}".format(img_url, receiver)
|
|
|
- )
|
|
|
+ logger.info("[wechatmp] image uploaded, receiver {}, media_id {}".format(receiver, media_id))
|
|
|
self.cache_dict[receiver] = ("image", media_id)
|
|
|
else:
|
|
|
if reply.type == ReplyType.TEXT or reply.type == ReplyType.INFO or reply.type == ReplyType.ERROR:
|
|
|
reply_text = reply.content
|
|
|
self.client.message.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:
|
|
|
try:
|
|
|
file_path = reply.content
|
|
|
@@ -148,6 +148,7 @@ class WechatMPChannel(ChatChannel):
|
|
|
file_name = os.path.basename(file_path)
|
|
|
file_type = "audio/mpeg"
|
|
|
logger.info("[wechatmp] file_name: {}, file_type: {} ".format(file_name, file_type))
|
|
|
+ # support: <2M, <60s, AMR\MP3
|
|
|
response = self.client.media.upload("voice", (file_name, open(file_path, "rb"), file_type))
|
|
|
logger.debug("[wechatmp] upload voice response: {}".format(response))
|
|
|
except WeChatClientException as e:
|
|
|
@@ -171,12 +172,8 @@ class WechatMPChannel(ChatChannel):
|
|
|
except WeChatClientException as e:
|
|
|
logger.error("[wechatmp] upload image failed: {}".format(e))
|
|
|
return
|
|
|
- self.client.message.send_image(
|
|
|
- receiver, response["media_id"]
|
|
|
- )
|
|
|
- logger.info(
|
|
|
- "[wechatmp] sendImage url={}, receiver={}".format(img_url, receiver)
|
|
|
- )
|
|
|
+ self.client.message.send_image(receiver, response["media_id"])
|
|
|
+ logger.info("[wechatmp] Do send image to {}".format(receiver))
|
|
|
elif reply.type == ReplyType.IMAGE: # 从文件读取图片
|
|
|
image_storage = reply.content
|
|
|
image_storage.seek(0)
|
|
|
@@ -189,12 +186,8 @@ class WechatMPChannel(ChatChannel):
|
|
|
except WeChatClientException as e:
|
|
|
logger.error("[wechatmp] upload image failed: {}".format(e))
|
|
|
return
|
|
|
- self.client.message.send_image(
|
|
|
- receiver, response["media_id"]
|
|
|
- )
|
|
|
- logger.info(
|
|
|
- "[wechatmp] sendImage, receiver={}".format(receiver)
|
|
|
- )
|
|
|
+ self.client.message.send_image(receiver, response["media_id"])
|
|
|
+ logger.info("[wechatmp] Do send image to {}".format(receiver))
|
|
|
return
|
|
|
|
|
|
def _success_callback(self, session_id, context, **kwargs): # 线程异常结束时的回调函数
|