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
use crate::{
models::{Message, MessageResponse},
WhatsappError,
};
const WHATSAPP_API_URL: &str = "https://graph.facebook.com/v13.0/105940028793862/messages";
pub struct WhatasppClient {
access_token: String,
}
impl WhatasppClient {
pub fn new(access_token: &str) -> Self {
Self {
access_token: access_token.into(),
}
}
pub async fn send_message(&self, message: &Message) -> Result<MessageResponse, WhatsappError> {
http_client::post(WHATSAPP_API_URL, &self.access_token, message).await
}
}
mod http_client {
use reqwest::StatusCode;
use serde::{de::DeserializeOwned, Serialize};
use crate::WhatsappError;
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()))
}
}
}
}