Skip to main content

wechat_oa_sdk/models/
template.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4/// Template message data item.
5#[derive(Debug, Clone, Serialize)]
6pub struct TemplateDataItem {
7    pub value: String,
8    #[serde(skip_serializing_if = "Option::is_none")]
9    pub color: Option<String>,
10}
11
12impl TemplateDataItem {
13    pub fn new(value: impl Into<String>) -> Self {
14        Self {
15            value: value.into(),
16            color: None,
17        }
18    }
19
20    pub fn with_color(mut self, color: impl Into<String>) -> Self {
21        self.color = Some(color.into());
22        self
23    }
24}
25
26/// Mini program link for template message.
27#[derive(Debug, Clone, Serialize)]
28pub struct TemplateMiniProgram {
29    pub appid: String,
30    pub pagepath: Option<String>,
31}
32
33/// Template message request.
34#[derive(Debug, Clone, Serialize)]
35pub struct TemplateMessage {
36    /// Receiver's OpenID
37    pub touser: String,
38    /// Template ID
39    pub template_id: String,
40    /// Click URL (optional)
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub url: Option<String>,
43    /// Mini program link (optional, takes priority over url)
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub miniprogram: Option<TemplateMiniProgram>,
46    /// Template data
47    pub data: HashMap<String, TemplateDataItem>,
48    /// Client message ID (for deduplication)
49    #[serde(skip_serializing_if = "Option::is_none")]
50    pub client_msg_id: Option<String>,
51}
52
53impl TemplateMessage {
54    pub fn new(
55        to_user: impl Into<String>,
56        template_id: impl Into<String>,
57        data: HashMap<String, TemplateDataItem>,
58    ) -> Self {
59        Self {
60            touser: to_user.into(),
61            template_id: template_id.into(),
62            url: None,
63            miniprogram: None,
64            data,
65            client_msg_id: None,
66        }
67    }
68
69    pub fn with_url(mut self, url: impl Into<String>) -> Self {
70        self.url = Some(url.into());
71        self
72    }
73
74    pub fn with_miniprogram(mut self, appid: impl Into<String>, pagepath: Option<String>) -> Self {
75        self.miniprogram = Some(TemplateMiniProgram {
76            appid: appid.into(),
77            pagepath,
78        });
79        self
80    }
81
82    pub fn with_client_msg_id(mut self, id: impl Into<String>) -> Self {
83        self.client_msg_id = Some(id.into());
84        self
85    }
86}
87
88/// Response from sending template message.
89#[derive(Debug, Clone, Deserialize)]
90pub struct SendTemplateResponse {
91    pub errcode: i64,
92    pub errmsg: String,
93    pub msgid: Option<i64>,
94}
95
96/// Template item from template list.
97#[derive(Debug, Clone, Deserialize)]
98pub struct TemplateItem {
99    pub template_id: String,
100    pub title: String,
101    pub primary_industry: String,
102    pub deputy_industry: String,
103    pub content: String,
104    pub example: String,
105}
106
107/// Response from getting template list.
108#[derive(Debug, Clone, Deserialize)]
109pub struct TemplateListResponse {
110    pub template_list: Vec<TemplateItem>,
111}
112
113/// Industry information.
114#[derive(Debug, Clone, Deserialize)]
115pub struct Industry {
116    pub first_class: String,
117    pub second_class: String,
118}
119
120/// Response from getting industry.
121#[derive(Debug, Clone, Deserialize)]
122pub struct GetIndustryResponse {
123    pub primary_industry: Industry,
124    pub secondary_industry: Industry,
125}