@@ -140,6 +140,42 @@ def send(self, reply: Reply, context: Context):
140140 media_id = response ["media_id" ]
141141 logger .info ("[wechatmp] image uploaded, receiver {}, media_id {}" .format (receiver , media_id ))
142142 self .cache_dict [receiver ].append (("image" , media_id ))
143+ elif reply .type == ReplyType .VIDEO_URL : # 从网络下载视频
144+ video_url = reply .content
145+ video_res = requests .get (video_url , stream = True )
146+ video_storage = io .BytesIO ()
147+ for block in video_res .iter_content (1024 ):
148+ video_storage .write (block )
149+ video_storage .seek (0 )
150+ video_type = 'mp4'
151+ filename = receiver + "-" + str (context ["msg" ].msg_id ) + "." + video_type
152+ content_type = "video/" + video_type
153+ try :
154+ response = self .client .material .add ("video" , (filename , video_storage , content_type ))
155+ logger .debug ("[wechatmp] upload video response: {}" .format (response ))
156+ except WeChatClientException as e :
157+ logger .error ("[wechatmp] upload video failed: {}" .format (e ))
158+ return
159+ media_id = response ["media_id" ]
160+ logger .info ("[wechatmp] video uploaded, receiver {}, media_id {}" .format (receiver , media_id ))
161+ self .cache_dict [receiver ].append (("video" , media_id ))
162+
163+ elif reply .type == ReplyType .VIDEO : # 从文件读取视频
164+ video_storage = reply .content
165+ video_storage .seek (0 )
166+ video_type = 'mp4'
167+ filename = receiver + "-" + str (context ["msg" ].msg_id ) + "." + video_type
168+ content_type = "video/" + video_type
169+ try :
170+ response = self .client .material .add ("video" , (filename , video_storage , content_type ))
171+ logger .debug ("[wechatmp] upload video response: {}" .format (response ))
172+ except WeChatClientException as e :
173+ logger .error ("[wechatmp] upload video failed: {}" .format (e ))
174+ return
175+ media_id = response ["media_id" ]
176+ logger .info ("[wechatmp] video uploaded, receiver {}, media_id {}" .format (receiver , media_id ))
177+ self .cache_dict [receiver ].append (("video" , media_id ))
178+
143179 else :
144180 if reply .type == ReplyType .TEXT or reply .type == ReplyType .INFO or reply .type == ReplyType .ERROR :
145181 reply_text = reply .content
@@ -222,6 +258,38 @@ def send(self, reply: Reply, context: Context):
222258 return
223259 self .client .message .send_image (receiver , response ["media_id" ])
224260 logger .info ("[wechatmp] Do send image to {}" .format (receiver ))
261+ elif reply .type == ReplyType .VIDEO_URL : # 从网络下载视频
262+ video_url = reply .content
263+ video_res = requests .get (video_url , stream = True )
264+ video_storage = io .BytesIO ()
265+ for block in video_res .iter_content (1024 ):
266+ video_storage .write (block )
267+ video_storage .seek (0 )
268+ video_type = 'mp4'
269+ filename = receiver + "-" + str (context ["msg" ].msg_id ) + "." + video_type
270+ content_type = "video/" + video_type
271+ try :
272+ response = self .client .media .upload ("video" , (filename , video_storage , content_type ))
273+ logger .debug ("[wechatmp] upload video response: {}" .format (response ))
274+ except WeChatClientException as e :
275+ logger .error ("[wechatmp] upload video failed: {}" .format (e ))
276+ return
277+ self .client .message .send_video (receiver , response ["media_id" ])
278+ logger .info ("[wechatmp] Do send video to {}" .format (receiver ))
279+ elif reply .type == ReplyType .VIDEO : # 从文件读取视频
280+ video_storage = reply .content
281+ video_storage .seek (0 )
282+ video_type = 'mp4'
283+ filename = receiver + "-" + str (context ["msg" ].msg_id ) + "." + video_type
284+ content_type = "video/" + video_type
285+ try :
286+ response = self .client .media .upload ("video" , (filename , video_storage , content_type ))
287+ logger .debug ("[wechatmp] upload video response: {}" .format (response ))
288+ except WeChatClientException as e :
289+ logger .error ("[wechatmp] upload video failed: {}" .format (e ))
290+ return
291+ self .client .message .send_video (receiver , response ["media_id" ])
292+ logger .info ("[wechatmp] Do send video to {}" .format (receiver ))
225293 return
226294
227295 def _success_callback (self , session_id , context , ** kwargs ): # 线程异常结束时的回调函数
0 commit comments