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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
use actions::ImagemapAction;
use templates::TemplateComponent;
use flex_message::containers::FlexContainer;

#[derive(Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct BaseSize {
    pub height: u64,
    pub width : u64,
}

#[derive(Serialize, Deserialize, Clone)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum LineMessageType {
    Text     { text: String },
    Image {
        #[serde(default, rename = "originalContentUrl")]     
        original_content_url: String,
        #[serde(default, rename = "previewImageUrl")]
        preview_image_url   : String,
    },
    Video {
        #[serde(default, rename = "originalContentUrl")]        
        original_content_url: String,
        #[serde(default, rename = "previewImageUrl")]
        preview_image_url   : String,
    },
    Audio {
        #[serde(default, rename = "originalContentUrl")]        
        original_content_url: String,
        #[serde(default)]
        duration            : u64,
    },
    Location {
        #[serde(default)]
        title    : String,
        address  : String,
        latitude : f64,
        longitude: f64
    },
    File {
        #[serde(rename = "fileName")]
        file_name: String,
        #[serde(rename = "fileSize")]
        file_size: u64,
    },
    Sticker {
        #[serde(rename = "packageId")]
        package_id: String,
        #[serde(rename = "stickerId")]
        sticker_id: String
    },
    Imagemap { 
        #[serde(rename = "baseUrl")]
        base_url: String,
        #[serde(rename = "altText")]
        alt_text: String,
        #[serde(rename = "baseSize")]
        base_size: BaseSize,
        actions: Vec<ImagemapAction>
    },
    Template {
        #[serde(default, rename = "altText")]             
        alt_text: String,
        template: TemplateComponent
    },
    Flex {
        #[serde(default, rename = "altText")]             
        alt_text: String,
        contents: FlexContainer,
    }
}

#[derive(Serialize, Deserialize, Clone)]
pub struct LineMessage {
    #[serde(flatten, rename = "type")]
    pub kind: LineMessageType,
    pub id:   String,
}

//builder and getter
impl LineMessage {
    pub fn new(id: &str, kind: LineMessageType) -> LineMessage {
        LineMessage { id: String::from(id), kind }
    }

    pub fn get_id(&self) -> String {
        self.id.clone()
    }
    
    pub fn get_text(&self) -> Option<String> {
        match self.kind.clone() {
            LineMessageType::Text { text } => Some(text),
            _ => None
        }
    }

    pub fn get_alt_text(&self) -> Option<String> {
        match self.kind.clone() {
            LineMessageType::Template { alt_text, template }                     => Some(alt_text),
            LineMessageType::Imagemap { base_url, alt_text, base_size, actions } => Some(base_url),    
            _ => None
        }
    }

    pub fn get_tempate(&self) -> Option<TemplateComponent> {
        match self.kind.clone() {
            LineMessageType::Template { alt_text, template } => Some(template),
            _ => None
        }
    }

    pub fn get_address(&self) -> Option<String> {
        match self.kind.clone() {
            LineMessageType::Location { title, address, latitude, longitude } => Some(address),
            _ => None
        }
    }

    pub fn get_latitude(&self) -> Option<f64> {
        match self.kind.clone() {
            LineMessageType::Location { title, address, latitude, longitude } => Some(latitude),
            _ => None
        }
    }

    pub fn get_longitude(&self) -> Option<f64> {
        match self.kind.clone() {
            LineMessageType::Location { title, address, latitude, longitude } => Some(longitude),
            _ => None
        }
    }

    pub fn get_base_url(&self) -> Option<String> {
        match self.kind.clone() {
            LineMessageType::Imagemap { base_url, alt_text, base_size, actions } => Some(base_url),
            _ => None
        }
    }

    pub fn get_base_size(&self) -> Option<BaseSize> {
        match self.kind.clone() {
            LineMessageType::Imagemap { base_url, alt_text, base_size, actions } => Some(base_size),
            _ => None
        }
    }
    
    pub fn get_base_size_height(&self) -> Option<u64> {
        match self.kind.clone() {
            LineMessageType::Imagemap { base_url, alt_text, base_size, actions } => Some(base_size.height),
            _ => None
        }
    }

    pub fn get_base_size_width(&self) -> Option<u64> {
        match self.kind.clone() {
            LineMessageType::Imagemap { base_url, alt_text, base_size, actions } => Some(base_size.width),
            _ => None
        }
    }

    pub fn get_action(&self) -> Option<Vec<ImagemapAction>> {
        match self.kind.clone() {
            LineMessageType::Imagemap { base_url, alt_text, base_size, actions } => Some(actions),
            _ => None
        }
    }

    pub fn get_file_name(&self) -> Option<String> {
        match self.kind.clone() {
            LineMessageType::File { file_name, file_size } => Some(file_name),
            _ => None
        }
    }

    pub fn get_file_size(&self) -> Option<u64> {
        match self.kind.clone() {
            LineMessageType::File { file_name, file_size } => Some(file_size),
            _ => None
        }
    }

    pub fn get_package_id(&self) -> Option<String> {
        match self.kind.clone() {
            LineMessageType::Sticker { package_id, sticker_id } => Some(package_id),
            _ => None
        }
    }

    pub fn get_sticker_id(&self) -> Option<String> {
        match self.kind.clone() {
            LineMessageType::Sticker { package_id, sticker_id } => Some(sticker_id),
            _ => None
        }
    }
}

//crete message
impl LineMessage {
    pub fn create_image(id: &str, original_content_url: &str, preview_image_url: &str) -> LineMessage {
        LineMessage {
            id: String::from(id),
            kind: LineMessageType::Image {
                original_content_url: String::from(original_content_url),
                preview_image_url   : String::from(preview_image_url),
            }
        }
    }

    pub fn create_video(id: &str, original_content_url: &str, preview_image_url: &str) -> LineMessage {
        LineMessage {
            id: String::from(id),
            kind: LineMessageType::Video {
                original_content_url: String::from(original_content_url),
                preview_image_url   : String::from(preview_image_url),
            }
        }
    }

    pub fn create_audio(id: &str, original_content_url: &str, duration: u64) -> LineMessage {
        LineMessage {
            id: String::from(id),
            kind: LineMessageType::Audio {
                original_content_url: String::from(original_content_url),
                duration,
            }
        }
    }

    pub fn create_text(id: &str, text: &str) -> LineMessage {
        LineMessage { 
            id: String::from(id),
            kind: LineMessageType::Text {
                text: String::from(text)
            }
        }
    }

    pub fn create_location(id: &str, title: &str, address: &str, latitude: f64, longitude: f64) -> LineMessage {
        LineMessage {
            id: String::from(id),
            kind: LineMessageType::Location {
                title: String::from(title),
                address: String::from(address),
                latitude,
                longitude,
            }
        }
    }

    pub fn create_imagemap(id: &str, base_url: &str, alt_text: &str, height: u64, width: u64, actions: Vec<ImagemapAction>) -> LineMessage {
        LineMessage {
            id: String::from(id),
            kind: LineMessageType::Imagemap {
                base_url: String::from(base_url),
                alt_text: String::from(alt_text),
                base_size: BaseSize { height, width },
                actions,
            }
        }
    }

    pub fn create_file(id: &str, file_name: &str, file_size: u64) -> LineMessage {
        LineMessage {
            id: String::from(id),
            kind: LineMessageType::File {
                file_name: String::from(file_name),
                file_size
            }
        }
    }

    pub fn create_sticker(id: &str, package_id: &str, sticker_id: &str) -> LineMessage {
        LineMessage {
            id: String::from(id),
            kind: LineMessageType::Sticker {
                package_id: String::from(package_id),
                sticker_id: String::from(sticker_id),
            }
        }
    }

    pub fn create_template(id: &str, alt_text: &str, template: TemplateComponent) -> LineMessage {
        LineMessage {
            id: String::from(id),
            kind: LineMessageType::Template {
                alt_text: String::from(alt_text),
                template,
            }
        }
    }

    pub fn create_flex(id: &str, alt_text: &str, contents: FlexContainer) -> LineMessage {
        LineMessage {
            id: String::from(id),
            kind: LineMessageType::Flex {
                alt_text: String::from(alt_text),
                contents,
            }
        }
    }
}