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
use crate::{
    models::{MediaResponse, Message, MessageResponse, MessageStatus, MessageStatusResponse},
    WhatsappError,
};

const FACEBOOK_GRAPH_API_BASE_URL: &str = "https://graph.facebook.com/v17.0";

pub struct WhatasppClient {
    access_token: String,
    phone_number_id: String,
}

impl WhatasppClient {
    pub fn new(access_token: &str, phone_number_id: &str) -> Self {
        Self {
            access_token: access_token.into(),
            phone_number_id: phone_number_id.into(),
        }
    }

    pub async fn send_message(&self, message: &Message) -> Result<MessageResponse, WhatsappError> {
        http_client::post(&self.messages_api_url(), &self.access_token, message).await
    }

    pub async fn mark_message_as_read(
        &self,
        message_id: &str,
    ) -> Result<MessageStatusResponse, WhatsappError> {
        let message_status = MessageStatus::for_read(message_id);
        http_client::post(
            &self.messages_api_url(),
            &self.access_token,
            &message_status,
        )
        .await
    }

    pub async fn get_media(&self, media_id: &str) -> Result<MediaResponse, WhatsappError> {
        http_client::get(&self.media_api_url(media_id), &self.access_token).await
    }

    fn messages_api_url(&self) -> String {
        format!(
            "{FACEBOOK_GRAPH_API_BASE_URL}/{}/messages",
            self.phone_number_id
        )
    }

    fn media_api_url(&self, media_id: &str) -> String {
        format!("{FACEBOOK_GRAPH_API_BASE_URL}/{media_id}")
    }
}

mod http_client {
    use reqwest::StatusCode;
    use serde::{de::DeserializeOwned, Serialize};

    use crate::WhatsappError;

    pub async fn get<U>(url: &str, bearer_token: &str) -> Result<U, WhatsappError>
    where
        U: DeserializeOwned,
    {
        let client = reqwest::Client::new();
        let resp = client.get(url).bearer_auth(bearer_token).send().await?;

        match resp.status() {
            StatusCode::OK => {
                let json = resp.json::<U>().await?;
                Ok(json)
            }
            _ => {
                log::warn!("{:?}", &resp);
                let error_text = &resp.text().await?;
                log::warn!("{:?}", &error_text);
                Err(WhatsappError::UnexpectedError(error_text.to_string()))
            }
        }
    }

    pub async fn post<T, U>(url: &str, bearer_token: &str, data: &T) -> Result<U, WhatsappError>
    where
        T: Serialize + ?Sized,
        U: DeserializeOwned,
    {
        let client = reqwest::Client::new();
        let resp = client
            .post(url)
            .bearer_auth(bearer_token)
            .json(&data)
            .send()
            .await?;

        match resp.status() {
            StatusCode::OK | StatusCode::CREATED => {
                let json = resp.json::<U>().await?;
                Ok(json)
            }
            _ => {
                log::warn!("{:?}", &resp);
                let error_text = &resp.text().await?;
                log::warn!("{:?}", &error_text);
                Err(WhatsappError::UnexpectedError(error_text.to_string()))
            }
        }
    }
}