line_bot_messaging_api/entity/message/
template.rs

1use crate::action::LineActionObject;
2use crate::message::LineMessageObject;
3use serde_derive::{Deserialize, Serialize};
4use serde_json::{json, Value};
5
6#[derive(Debug, Deserialize, Serialize, Clone)]
7pub struct LineMessageTemplate {
8    #[serde(rename = "type")]
9    message_type: String,
10    #[serde(rename = "altText")]
11    alt_text: String,
12    template: Value,
13}
14
15impl LineMessageTemplate {
16    pub fn new<T: ToString>(alt_text: T, template: impl LineMessageTemplateObject) -> Self {
17        Self {
18            message_type: "template".to_string(),
19            alt_text: alt_text.to_string(),
20            template: template.build(),
21        }
22    }
23}
24impl LineMessageObject for LineMessageTemplate {
25    fn build(&self) -> Value {
26        json!(self)
27    }
28}
29
30pub trait LineMessageTemplateObject {
31    fn build(&self) -> Value;
32}
33
34#[derive(Debug, Deserialize, Serialize, Clone)]
35pub struct LineMessageTemplateButton {
36    #[serde(rename = "type")]
37    message_type: String,
38    #[serde(rename = "thumbnailImageUrl")]
39    #[serde(skip_serializing_if = "Option::is_none")]
40    thumbnail_image_url: Option<String>,
41    #[serde(rename = "imageAspectRatio")]
42    #[serde(skip_serializing_if = "Option::is_none")]
43    image_aspect_ratio: Option<String>,
44    #[serde(rename = "imageSize")]
45    #[serde(skip_serializing_if = "Option::is_none")]
46    image_size: Option<String>,
47    #[serde(rename = "imageBackgroundColor")]
48    #[serde(skip_serializing_if = "Option::is_none")]
49    image_background_color: Option<String>,
50    #[serde(rename = "title")]
51    #[serde(skip_serializing_if = "Option::is_none")]
52    title: Option<String>,
53    #[serde(rename = "text")]
54    text: String,
55    #[serde(skip_serializing_if = "Option::is_none")]
56    #[serde(rename = "defaultAction")]
57    default_action: Option<Value>,
58    #[serde(rename = "actions")]
59    actions: Vec<Value>,
60}
61
62impl LineMessageTemplateButton {
63    pub fn new<T: ToString>(text: T) -> Self {
64        Self {
65            message_type: "buttons".to_string(),
66            thumbnail_image_url: None,
67            image_aspect_ratio: None,
68            image_size: None,
69            image_background_color: None,
70            title: None,
71            text: text.to_string(),
72            default_action: None,
73            actions: vec![],
74        }
75    }
76    pub fn set_default_action(&mut self, action: impl LineActionObject) {
77        self.default_action = Some(action.build());
78    }
79    pub fn append_action(&mut self, action: impl LineActionObject) {
80        self.actions.push(action.build());
81    }
82    pub fn set_thumbnail_image_url<T: ToString>(&mut self, thumbnail_image_url: T) {
83        self.thumbnail_image_url = Some(thumbnail_image_url.to_string());
84    }
85    pub fn set_image_aspect_ratio<T: ToString>(&mut self, image_aspect_ratio: T) {
86        self.image_aspect_ratio = Some(image_aspect_ratio.to_string());
87    }
88    pub fn set_image_size<T: ToString>(&mut self, image_size: T) {
89        self.image_size = Some(image_size.to_string());
90    }
91    pub fn set_image_background_color<T: ToString>(&mut self, image_background_color: T) {
92        self.image_background_color = Some(image_background_color.to_string());
93    }
94    pub fn set_title<T: ToString>(&mut self, title: T) {
95        self.title = Some(title.to_string());
96    }
97}
98
99impl LineMessageTemplateObject for LineMessageTemplateButton {
100    fn build(&self) -> Value {
101        json!(self)
102    }
103}
104
105#[derive(Debug, Deserialize, Serialize, Clone)]
106pub struct LineMessageTemplateConfirm {
107    #[serde(rename = "type")]
108    message_type: String,
109    #[serde(rename = "text")]
110    text: String,
111    #[serde(rename = "actions")]
112    actions: Vec<Value>,
113}
114
115impl LineMessageTemplateConfirm {
116    pub fn new<T: ToString>(text: T) -> Self {
117        Self {
118            message_type: "confirm".to_string(),
119            text: text.to_string(),
120            actions: vec![],
121        }
122    }
123    pub fn append_action(&mut self, action: impl LineActionObject) {
124        self.actions.push(action.build());
125    }
126}
127impl LineMessageTemplateObject for LineMessageTemplateConfirm {
128    fn build(&self) -> Value {
129        json!(self)
130    }
131}
132
133#[derive(Debug, Deserialize, Serialize, Clone)]
134pub struct LineMessageTemplateCarousel {
135    #[serde(rename = "type")]
136    message_type: String,
137    #[serde(rename = "columns")]
138    columns: Vec<LineMessageTemplateCarouselColumn>,
139    #[serde(rename = "imageAspectRatio")]
140    #[serde(skip_serializing_if = "Option::is_none")]
141    image_aspect_ratio: Option<String>,
142    #[serde(rename = "imageSize")]
143    #[serde(skip_serializing_if = "Option::is_none")]
144    image_size: Option<String>,
145}
146impl LineMessageTemplateCarousel {
147    pub fn new(columns: Vec<LineMessageTemplateCarouselColumn>) -> Self {
148        Self {
149            message_type: "carousel".to_string(),
150            columns,
151            image_aspect_ratio: None,
152            image_size: None,
153        }
154    }
155    pub fn append_column(&mut self, column: LineMessageTemplateCarouselColumn) {
156        self.columns.push(column);
157    }
158    pub fn set_image_aspect_ratio<T: ToString>(&mut self, image_aspect_ratio: T) {
159        self.image_aspect_ratio = Some(image_aspect_ratio.to_string());
160    }
161    pub fn set_image_size<T: ToString>(&mut self, image_size: T) {
162        self.image_size = Some(image_size.to_string());
163    }
164}
165impl LineMessageTemplateObject for LineMessageTemplateCarousel {
166    fn build(&self) -> Value {
167        json!(self)
168    }
169}
170#[derive(Debug, Deserialize, Serialize, Clone)]
171pub struct LineMessageTemplateCarouselColumn {
172    #[serde(rename = "thumbnailImageUrl")]
173    #[serde(skip_serializing_if = "Option::is_none")]
174    thumbnail_image_url: Option<String>,
175    #[serde(rename = "imageBackgroundColor")]
176    #[serde(skip_serializing_if = "Option::is_none")]
177    image_background_color: Option<String>,
178    #[serde(rename = "title")]
179    #[serde(skip_serializing_if = "Option::is_none")]
180    title: Option<String>,
181    #[serde(rename = "text")]
182    text: String,
183    #[serde(skip_serializing_if = "Option::is_none")]
184    #[serde(rename = "defaultAction")]
185    default_action: Option<Value>,
186    #[serde(rename = "actions")]
187    actions: Vec<Value>,
188}
189
190impl LineMessageTemplateCarouselColumn {
191    pub fn new<T: ToString>(text: T) -> Self {
192        Self {
193            thumbnail_image_url: None,
194            image_background_color: None,
195            title: None,
196            text: text.to_string(),
197            default_action: None,
198            actions: vec![],
199        }
200    }
201    pub fn set_default_action(&mut self, action: impl LineActionObject) {
202        self.default_action = Some(action.build());
203    }
204    pub fn append_action(&mut self, action: impl LineActionObject) {
205        self.actions.push(action.build());
206    }
207    pub fn set_thumbnail_image_url<T: ToString>(&mut self, thumbnail_image_url: T) {
208        self.thumbnail_image_url = Some(thumbnail_image_url.to_string());
209    }
210    pub fn set_image_background_color<T: ToString>(&mut self, image_background_color: T) {
211        self.image_background_color = Some(image_background_color.to_string());
212    }
213    pub fn set_title<T: ToString>(&mut self, title: T) {
214        self.title = Some(title.to_string());
215    }
216}
217
218#[derive(Debug, Deserialize, Serialize, Clone)]
219pub struct LineMessageTemplateCarouselImage {
220    #[serde(rename = "type")]
221    message_type: String,
222    #[serde(rename = "columns")]
223    columns: Vec<LineMessageTemplateCarouselImageColumn>,
224}
225
226impl LineMessageTemplateCarouselImage {
227    pub fn new(columns: Vec<LineMessageTemplateCarouselImageColumn>) -> Self {
228        Self {
229            message_type: "image_carousel".to_string(),
230            columns,
231        }
232    }
233    pub fn append_column(&mut self, column: LineMessageTemplateCarouselImageColumn) {
234        self.columns.push(column);
235    }
236}
237impl LineMessageTemplateObject for LineMessageTemplateCarouselImage {
238    fn build(&self) -> Value {
239        json!(self)
240    }
241}
242#[derive(Debug, Deserialize, Serialize, Clone)]
243pub struct LineMessageTemplateCarouselImageColumn {
244    #[serde(rename = "imageUrl")]
245    image_url: String,
246    #[serde(rename = "action")]
247    action: Value,
248}
249impl LineMessageTemplateCarouselImageColumn {
250    pub fn new<T: ToString>(image_url: T, action: impl LineActionObject) -> Self {
251        Self {
252            image_url: image_url.to_string(),
253            action: action.build(),
254        }
255    }
256}