weixin_rust/media/
mod.rs

1use crate::agent::Agent;
2use crate::agent::tools;
3
4pub struct MediaService{
5    pub agent:Agent
6}
7
8impl MediaService {
9
10    ///上传临时图片素材
11    ///# 微信接口
12    /// https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={}&type=image
13    pub fn upload_temp_image(&mut self,file_name:&str,file_bytes:Vec<u8>)->Result<serde_json::Value, reqwest::Error>{
14        self.agent.fresh_token();
15        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={}&type=image",self.agent.get_access_token());
16        tools::upload(url,String::from(file_name),file_bytes)
17    }
18
19    ///上传临时语音素材
20    ///# 微信接口
21    /// https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={}&type=voice
22    pub fn upload_temp_voice(&mut self,file_name:&str,file_bytes:Vec<u8>)->Result<serde_json::Value, reqwest::Error>{
23        self.agent.fresh_token();
24        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={}&type=voice",self.agent.get_access_token());
25        tools::upload(url,String::from(file_name),file_bytes)
26    }
27
28    ///上传临时视频素材
29    ///# 微信接口
30    /// https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={}&type=video
31    pub fn upload_temp_video(&mut self,file_name:&str,file_bytes:Vec<u8>)->Result<serde_json::Value, reqwest::Error>{
32        self.agent.fresh_token();
33        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={}&type=video",self.agent.get_access_token());
34        tools::upload(url,String::from(file_name),file_bytes)
35    }
36
37    ///上传临时文件素材
38    ///# 微信接口
39    /// https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={}&type=file
40    pub fn upload_temp_file(&mut self,file_name:&str,file_bytes:Vec<u8>)->Result<serde_json::Value, reqwest::Error>{
41        self.agent.fresh_token();
42        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={}&type=file",self.agent.get_access_token());
43        tools::upload(url,String::from(file_name),file_bytes)
44    }
45
46    ///上传图片
47    ///# 微信接口
48    /// https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg
49    pub fn upload_image(&mut self,file_name:&str,file_bytes:Vec<u8>)->Result<serde_json::Value, reqwest::Error>{
50        self.agent.fresh_token();
51        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg?access_token={}",self.agent.get_access_token());
52        tools::upload(url,String::from(file_name),file_bytes)
53    }
54
55
56}