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
use serde::{Deserialize, Serialize};

use super::Template;

const WHATSAPP: &str = "whatsapp";
const TEXT: &str = "text";
const TEMPLATE: &str = "template";

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Message {
    pub to: String,
    pub messaging_product: String,
    pub recipient_type: Option<String>,
    #[serde(rename = "type")]
    pub message_type: String,
    pub text: Option<Text>,
    pub template: Option<Template>,
}

impl Message {
    pub fn from_text(to: &str, text: Text) -> Self {
        Self {
            to: to.into(),
            messaging_product: WHATSAPP.into(),
            recipient_type: None,
            message_type: TEXT.into(),
            text: Some(text),
            template: None,
        }
    }

    pub fn from_template(to: &str, template: Template) -> Self {
        Self {
            messaging_product: WHATSAPP.into(),
            recipient_type: None,
            message_type: TEMPLATE.into(),
            to: to.into(),
            text: None,
            template: Some(template),
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Text {
    pub body: String,
    pub preview_url: Option<bool>,
}

impl Text {
    pub fn new(body: &str) -> Text {
        Self {
            body: body.into(),
            preview_url: None,
        }
    }

    pub fn with_preview_url(body: &str, preview_url: bool) -> Self {
        Self {
            body: body.into(),
            preview_url: Some(preview_url),
        }
    }
}