chimes_utils/wechat/
message.rs

1//! copyright © ecdata.cn 2021 - present
2//! 获取客服消息内的临时素材。即下载临时的多媒体文件。目前小程序仅支持下载图片文件。
3//! created by shaipe 20210228
4//!
5//! customerServiceMessage.getTempMedia
6//! DOC https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-message/customerServiceMessage.getTempMedia.html
7//!
8//! 发送客服消息给用户。
9//! customerServiceMessage.send
10//! DOC https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/customer-message/customerServiceMessage.send.html
11//! 下发客服当前输入状态给用户。
12//! customerServiceMessage.setTyping
13//! 把媒体文件上传到微信服务器。目前仅支持图片。用于发送客服消息或被动回复用户消息。
14//! customerServiceMessage.uploadTempMedia
15//!
16//! 下发小程序和公众号统一的服务消息
17//! uniformMessage.send
18//!
19//! 创建被分享动态消息或私密消息的 activity_id。
20//! updatableMessage.createActivityId
21//!
22//! 修改被分享的动态消息。
23//! updatableMessage.setUpdatableMsg
24//!
25//!
26//!
27//!
28//!
29//!
30//!
31//!
32//!
33//!
34//!
35//!
36use serde::Serialize;
37use serde_json::{json, Value};
38
39use crate::ChimesClient;
40
41#[derive(Debug, Eq, PartialEq, Clone, Serialize)]
42pub struct WeappTemplateMessage {
43    template_id: String,
44    page: String,
45    form_id: String,
46    emphasis_keyword: String,
47    data: Value,
48}
49
50impl WeappTemplateMessage {
51    pub fn new(template_id: &str, page: &str, form_id: &str, emph: &str, data: Value) -> Self {
52        Self {
53            template_id: template_id.to_owned(),
54            page: page.to_owned(),
55            form_id: form_id.to_owned(),
56            emphasis_keyword: emph.to_owned(),
57            data: data.clone(),
58        }
59    }
60}
61
62#[derive(Debug, Eq, PartialEq, Clone, Serialize)]
63pub struct MPTemplateMessage {
64    appid: String,
65    template_id: String,
66    url: String,
67    miniprogram: Value,
68    data: Value,
69}
70
71impl MPTemplateMessage {
72    pub fn new(
73        app_id: &str,
74        template_id: &str,
75        url: &str,
76        miniprogram: &str,
77        pagepath: &str,
78        data: &Value,
79    ) -> Self {
80        Self {
81            template_id: template_id.to_owned(),
82            appid: app_id.to_owned(),
83            url: url.to_owned(),
84            miniprogram: json!({"appid": miniprogram.to_owned(), "pagepath": pagepath.to_owned()}),
85            data: data.clone(),
86        }
87    }
88}
89
90#[derive(Debug, Eq, PartialEq, Clone, Serialize)]
91pub struct UniformMessage {
92    touser: String,
93    weapp_template_msg: Option<WeappTemplateMessage>,
94    mp_template_msg: MPTemplateMessage,
95}
96
97impl UniformMessage {
98    pub fn new(
99        to_user: &str,
100        weapp: &Option<WeappTemplateMessage>,
101        mp: &MPTemplateMessage,
102    ) -> Self {
103        Self {
104            touser: to_user.to_owned(),
105            weapp_template_msg: weapp.clone(),
106            mp_template_msg: mp.clone(),
107        }
108    }
109
110    #[inline]
111    pub async fn send(&self, access_token: &String) -> bool {
112        let api_url = format!(
113        "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token={}",
114        access_token
115    );
116        // let mut params = HashMap::new();
117        // params.insert("touser".to_string(), to_user.as_ref().to_string());
118        // params.insert("msgtype".to_string(), msg_type.as_ref().to_string());
119        // params.insert(msg_type.as_ref().to_string(), content.as_ref().to_string());
120        let params = json!(self);
121        // let params=format!(r#"{{
122        //     "touser":"{}",
123        //     "msgtype":"{}",
124        //     "{}":{{"content":"{}"}}
125        // }}"#,to_user.as_ref().to_string(),
126        // msg_type.as_ref().to_string(),
127        // msg_type.as_ref().to_string(),
128        // content.as_ref().to_string());
129
130        println!(
131            "send Unionform Message url:: {} content :: {:?}",
132            api_url, params
133        );
134
135        match ChimesClient::new().post(&api_url, &params).await {
136            Ok(v) => {
137                println!("success {:?}", v);
138                !v.contains("access_token expired")
139            }
140            Err(e) => {
141                println!("error {:?}", e);
142                false
143            }
144        }
145    }
146}