twilio_async/
message.rs

1use super::{
2    encode_pairs, url_encode, Execute, Twilio, TwilioErr, TwilioJson, TwilioRequest, TwilioResp,
3};
4use async_trait::async_trait;
5use hyper::{self, Method};
6use serde::Deserialize;
7
8#[derive(Default, Debug)]
9pub struct Msg<'a> {
10    from: &'a str,
11    to: &'a str,
12    body: &'a str,
13    media_url: Option<&'a str>,
14}
15
16impl<'a> Msg<'a> {
17    pub fn new(from: &'a str, to: &'a str, body: &'a str) -> Msg<'a> {
18        Msg {
19            from,
20            to,
21            body,
22            ..Msg::default()
23        }
24    }
25}
26
27impl<'a> ToString for Msg<'a> {
28    fn to_string(&self) -> String {
29        match self.media_url {
30            Some(m_url) => encode_pairs(&[
31                ("To", self.to),
32                ("From", self.from),
33                ("Body", self.body),
34                ("MediaUrl", m_url),
35            ])
36            .unwrap(),
37            None => {
38                encode_pairs(&[("To", self.to), ("From", self.from), ("Body", self.body)]).unwrap()
39            }
40        }
41    }
42}
43
44#[derive(Debug, Deserialize)]
45#[allow(non_camel_case_types)]
46pub enum MsgStatus {
47    queued,
48    sending,
49    sent,
50    failed,
51    delivered,
52    undelivered,
53    receiving,
54    received,
55}
56
57#[derive(Debug, Deserialize)]
58pub struct MsgResp {
59    pub from: String,
60    pub to: String,
61    pub body: String,
62    pub sid: String,
63    pub status: MsgStatus,
64    pub media_url: Option<String>,
65    pub price: Option<String>,
66    pub price_unit: String,
67    pub uri: String,
68    pub date_created: String,
69    pub date_sent: Option<String>,
70    pub date_updated: String,
71}
72
73// for outbound sms
74#[derive(Debug)]
75pub struct SendMsg<'a> {
76    pub msg: Msg<'a>,
77    pub client: &'a Twilio,
78}
79
80impl<'a> SendMsg<'a> {
81    pub fn media(mut self, media_url: &'a str) -> SendMsg<'a> {
82        self.msg.media_url = Some(media_url);
83        self
84    }
85}
86
87execute!(SendMsg);
88
89#[async_trait]
90impl<'a> TwilioRequest for SendMsg<'a> {
91    type Resp = MsgResp;
92
93    async fn run(&self) -> TwilioResp<TwilioJson<Self::Resp>> {
94        let msg = self.msg.to_string();
95        self.execute(Method::POST, "Messages.json", Some(msg)).await
96    }
97}
98
99#[derive(Debug)]
100pub struct GetMessage<'a> {
101    pub message_sid: &'a str,
102    pub client: &'a Twilio,
103}
104
105execute!(GetMessage);
106
107#[async_trait]
108impl<'a> TwilioRequest for GetMessage<'a> {
109    type Resp = MsgResp;
110
111    async fn run(&self) -> TwilioResp<TwilioJson<Self::Resp>> {
112        let msg_sid = format!("Messages/{}.json", self.message_sid);
113        self.execute(Method::GET, msg_sid, None).await
114    }
115}
116
117impl<'a> GetMessage<'a> {
118    pub async fn redact(&self) -> TwilioResp<TwilioJson<MsgResp>> {
119        let msg_sid = format!("Messages/{}.json", self.message_sid);
120        self.execute(Method::POST, msg_sid, Some("Body=".into()))
121            .await
122    }
123
124    pub async fn delete(&self) -> TwilioResp<TwilioJson<MsgResp>> {
125        let msg_sid = format!("Messages/{}.json", self.message_sid);
126        self.execute(Method::DELETE, msg_sid, None).await
127    }
128
129    pub async fn media(&self) -> TwilioResp<TwilioJson<MediaResp>> {
130        let msg_sid = format!("Messages/{}/Media.json", self.message_sid);
131        self.execute(Method::GET, msg_sid, None).await
132    }
133}
134
135#[derive(Debug, Deserialize)]
136pub struct MediaResp {
137    pub media_list: Vec<MediaItem>,
138    pub num_pages: i32,
139    pub page: i32,
140    pub page_size: i32,
141    pub start: i32,
142    pub total: i32,
143    pub uri: String,
144    pub account_sid: String,
145    pub message_sid: String,
146}
147
148#[derive(Debug, Deserialize)]
149pub struct MediaItem {
150    pub account_sid: String,
151    pub content_type: String,
152    pub sid: String,
153    pub uri: String,
154    pub message_sid: String,
155    pub date_created: String,
156    pub date_update: String,
157}
158
159#[derive(Debug)]
160pub struct Messages<'a> {
161    pub client: &'a Twilio,
162}
163
164impl<'a> Messages<'a> {
165    pub fn between(self, from: &'a str, to: &'a str) -> MessagesDetails<'a> {
166        MessagesDetails {
167            client: self.client,
168            from: Some(from),
169            to: Some(to),
170            date_sent: None,
171        }
172    }
173
174    pub fn on(self, date_sent: &'a str) -> MessagesDetails<'a> {
175        MessagesDetails {
176            client: self.client,
177            from: None,
178            to: None,
179            date_sent: Some(date_sent),
180        }
181    }
182}
183
184execute!(Messages);
185
186#[async_trait]
187impl<'a> TwilioRequest for Messages<'a> {
188    type Resp = ListAllMsgs;
189
190    async fn run(&self) -> TwilioResp<TwilioJson<Self::Resp>> {
191        self.execute(Method::GET, "Messages.json", None).await
192    }
193}
194
195#[derive(Debug)]
196pub struct MessagesDetails<'a> {
197    pub client: &'a Twilio,
198    pub from: Option<&'a str>,
199    pub to: Option<&'a str>,
200    pub date_sent: Option<&'a str>,
201}
202
203impl<'a> ToString for MessagesDetails<'a> {
204    fn to_string(&self) -> String {
205        let mut pairs = Vec::new();
206        pair!(self, from, "From", pairs);
207        pair!(self, to, "To", pairs);
208        pair!(self, date_sent, "DateSent", pairs);
209        // does this have to be different? will the encode_pairs work here?
210        url_encode(pairs)
211    }
212}
213
214execute!(MessagesDetails);
215
216#[async_trait]
217impl<'a> TwilioRequest for MessagesDetails<'a> {
218    type Resp = ListAllMsgs;
219
220    async fn run(&self) -> TwilioResp<TwilioJson<Self::Resp>> {
221        let url = format!("Messages.json?{}", self.to_string());
222        self.execute(Method::GET, url, None).await
223    }
224}
225
226#[derive(Debug, Deserialize)]
227pub struct ListAllMsgs {
228    pub messages: Vec<MsgResp>,
229    pub page: usize,
230    pub page_size: usize,
231    pub uri: String,
232    pub next_page_uri: Option<String>,
233    pub previous_page_uri: Option<String>,
234}