1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use crate::action::LineActionObject;
use serde_derive::{Deserialize, Serialize};
use serde_json::{json, Value};
use crate::message::LineMessageObject;

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct LineMessageTemplate {
    #[serde(rename = "type")]
    message_type: String,
    #[serde(rename = "altText")]
    alt_text: String,
    template: Value,
}

impl LineMessageTemplate {
    pub fn new<T: ToString>(alt_text: T, template: impl LineMessageTemplateObject) -> Self {
        Self {
            message_type: "template".to_string(),
            alt_text: alt_text.to_string(),
            template: template.build(),
        }
    }
}
impl LineMessageObject for LineMessageTemplate{
    fn build(&self) -> Value {
        json!(self)
    }
}

pub trait LineMessageTemplateObject {
    fn build(&self) -> Value;
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct LineMessageTemplateButton {
    #[serde(rename = "type")]
    message_type: String,
    #[serde(rename = "thumbnailImageUrl")]
    #[serde(skip_serializing_if = "Option::is_none")]
    thumbnail_image_url: Option<String>,
    #[serde(rename = "imageAspectRatio")]
    #[serde(skip_serializing_if = "Option::is_none")]
    image_aspect_ratio: Option<String>,
    #[serde(rename = "imageSize")]
    #[serde(skip_serializing_if = "Option::is_none")]
    image_size: Option<String>,
    #[serde(rename = "imageBackgroundColor")]
    #[serde(skip_serializing_if = "Option::is_none")]
    image_background_color: Option<String>,
    #[serde(rename = "title")]
    #[serde(skip_serializing_if = "Option::is_none")]
    title: Option<String>,
    #[serde(rename = "text")]
    text: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "defaultAction")]
    default_action: Option<Value>,
    #[serde(rename = "actions")]
    actions: Vec<Value>,
}

impl LineMessageTemplateButton {
    pub fn new<T: ToString>(text: T) -> Self {
        Self {
            message_type: "buttons".to_string(),
            thumbnail_image_url: None,
            image_aspect_ratio: None,
            image_size: None,
            image_background_color: None,
            title: None,
            text: text.to_string(),
            default_action: None,
            actions: vec![],
        }
    }
    pub fn set_default_action(&mut self,action: impl LineActionObject){
        self.default_action = Some(action.build());
    }
    pub fn append_action(&mut self,action: impl LineActionObject){
        self.actions.push(action.build());
    }
    pub fn set_thumbnail_image_url<T: ToString>(&mut self, thumbnail_image_url: T) {
        self.thumbnail_image_url = Some(thumbnail_image_url.to_string());
    }
    pub fn set_image_aspect_ratio<T: ToString>(&mut self, image_aspect_ratio: T) {
        self.image_aspect_ratio = Some(image_aspect_ratio.to_string());
    }
    pub fn set_image_size<T: ToString>(&mut self, image_size: T) {
        self.image_size = Some(image_size.to_string());
    }
    pub fn set_image_background_color<T: ToString>(&mut self, image_background_color: T) {
        self.image_background_color = Some(image_background_color.to_string());
    }
    pub fn set_title<T: ToString>(&mut self, title: T) {
        self.title = Some(title.to_string());
    }
}

impl LineMessageTemplateObject for LineMessageTemplateButton {
    fn build(&self) -> Value {
        json!(self)
    }
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct LineMessageTemplateConfirm {
    #[serde(rename = "type")]
    message_type: String,
    #[serde(rename = "text")]
    text: String,
    #[serde(rename = "actions")]
    actions: Vec<Value>,
}

impl LineMessageTemplateConfirm {
    pub fn new<T: ToString>(text: T) -> Self {
        Self {
            message_type: "confirm".to_string(),
            text: text.to_string(),
            actions: vec![],
        }
    }
    pub fn append_action(&mut self,action: impl LineActionObject){
        self.actions.push(action.build());
    }

}
impl LineMessageTemplateObject for LineMessageTemplateConfirm {
    fn build(&self) -> Value {
        json!(self)
    }
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct LineMessageTemplateCarousel {
    #[serde(rename = "type")]
    message_type: String,
    #[serde(rename = "columns")]
    columns: Vec<LineMessageTemplateCarouselColumn>,
    #[serde(rename = "imageAspectRatio")]
    #[serde(skip_serializing_if = "Option::is_none")]
    image_aspect_ratio: Option<String>,
    #[serde(rename = "imageSize")]
    #[serde(skip_serializing_if = "Option::is_none")]
    image_size: Option<String>,
}
impl LineMessageTemplateCarousel {
    pub fn new(columns: Vec<LineMessageTemplateCarouselColumn>) -> Self {
        Self {
            message_type: "carousel".to_string(),
            columns,
            image_aspect_ratio: None,
            image_size: None,
        }
    }
    pub fn append_column(&mut self,column: LineMessageTemplateCarouselColumn){
        self.columns.push(column);
    }
    pub fn set_image_aspect_ratio<T: ToString>(&mut self, image_aspect_ratio: T) {
        self.image_aspect_ratio = Some(image_aspect_ratio.to_string());
    }
    pub fn set_image_size<T: ToString>(&mut self, image_size: T) {
        self.image_size = Some(image_size.to_string());
    }
}
impl LineMessageTemplateObject for LineMessageTemplateCarousel {
    fn build(&self) -> Value {
        json!(self)
    }
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct LineMessageTemplateCarouselColumn {
    #[serde(rename = "thumbnailImageUrl")]
    #[serde(skip_serializing_if = "Option::is_none")]
    thumbnail_image_url: Option<String>,
    #[serde(rename = "imageBackgroundColor")]
    #[serde(skip_serializing_if = "Option::is_none")]
    image_background_color: Option<String>,
    #[serde(rename = "title")]
    #[serde(skip_serializing_if = "Option::is_none")]
    title: Option<String>,
    #[serde(rename = "text")]
    text: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "defaultAction")]
    default_action: Option<Value>,
    #[serde(rename = "actions")]
    actions: Vec<Value>,
}

impl LineMessageTemplateCarouselColumn{

    pub fn new<T: ToString>(text: T) -> Self {
        Self {
            thumbnail_image_url: None,
            image_background_color: None,
            title: None,
            text: text.to_string(),
            default_action: None,
            actions: vec![],
        }
    }
    pub fn set_default_action(&mut self,action: impl LineActionObject){
        self.default_action = Some(action.build());
    }
    pub fn append_action(&mut self,action: impl LineActionObject){
        self.actions.push(action.build());
    }
    pub fn set_thumbnail_image_url<T: ToString>(&mut self, thumbnail_image_url: T) {
        self.thumbnail_image_url = Some(thumbnail_image_url.to_string());
    }
    pub fn set_image_background_color<T: ToString>(&mut self, image_background_color: T) {
        self.image_background_color = Some(image_background_color.to_string());
    }
    pub fn set_title<T: ToString>(&mut self, title: T) {
        self.title = Some(title.to_string());
    }
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct LineMessageTemplateCarouselImage {
    #[serde(rename = "type")]
    message_type: String,
    #[serde(rename = "columns")]
    columns: Vec<LineMessageTemplateCarouselImageColumn>,
}

impl LineMessageTemplateCarouselImage{
    pub fn new(columns: Vec<LineMessageTemplateCarouselImageColumn>) -> Self {
        Self {
            message_type: "image_carousel".to_string(),
            columns,
        }
    }
    pub fn append_column(&mut self,column: LineMessageTemplateCarouselImageColumn){
        self.columns.push(column);
    }
}
impl LineMessageTemplateObject for LineMessageTemplateCarouselImage {
    fn build(&self) -> Value {
        json!(self)
    }
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct LineMessageTemplateCarouselImageColumn {
    #[serde(rename = "imageUrl")]
    image_url: String,
    #[serde(rename = "action")]
    action: Value,
}
impl LineMessageTemplateCarouselImageColumn{
    pub fn new<T: ToString>(image_url: T,action: impl LineActionObject) -> Self {
        Self {
            image_url: image_url.to_string(),
            action: action.build(),
        }
    }
}