chimes_utils/wechat/
message.rs1use 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 params = json!(self);
121 println!(
131 "send Unionform Message url:: {} content :: {:?}",
132 api_url, params
133 );
134
135 match ChimesClient::new().post(&api_url, ¶ms).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}