twilio_async/
call.rs

1use super::{encode_pairs, Execute, Twilio, TwilioErr, TwilioJson, TwilioRequest, TwilioResp};
2use async_trait::async_trait;
3use hyper::{self, Method};
4use serde::Deserialize;
5
6#[derive(Debug, Default)]
7pub struct Call<'a> {
8    from: &'a str,
9    to: &'a str,
10    url: &'a str,
11    sid: Option<&'a str>,
12    callerid: Option<&'a str>,
13    machine_detection: Option<bool>,
14    record: Option<bool>,
15    send_digits: Option<&'a str>,
16    status_callback: Option<&'a str>,
17    callback_event: Option<CallbackEvent>,
18    timeout: Option<&'a str>,
19}
20
21#[derive(Debug)]
22pub enum CallbackEvent {
23    Initiated,
24    Ringing,
25    Answered,
26    Completed,
27}
28
29use self::CallbackEvent::*;
30
31impl<'a> Call<'a> {
32    pub fn new(from: &'a str, to: &'a str, url: &'a str) -> Call<'a> {
33        Call {
34            from,
35            to,
36            url,
37            ..Call::default()
38        }
39    }
40}
41
42impl<'a> ToString for Call<'a> {
43    fn to_string(&self) -> String {
44        let mut pairs = vec![("To", self.to), ("From", self.from), ("Url", self.url)];
45        pair!(self, sid, "ApplicationSid", pairs);
46        pair!(self, callerid, "CallerId", pairs);
47        if let Some(detection) = self.machine_detection {
48            if detection {
49                pairs.push(("MachineDetection", "Enable"));
50            }
51        }
52        if let Some(record) = self.record {
53            if record {
54                pairs.push(("Record", "true"));
55            }
56        }
57        if let Some(ref cb) = self.callback_event {
58            let event = match *cb {
59                Initiated => "initiated",
60                Ringing => "ringing",
61                Answered => "answered",
62                Completed => "completed",
63            };
64            pairs.push(("StatusCallbackEvent", event));
65        }
66        pair!(self, timeout, "Timeout", pairs);
67        pair!(self, send_digits, "SendDigits", pairs);
68        pair!(self, status_callback, "StatusCallback", pairs);
69
70        encode_pairs(pairs).unwrap()
71    }
72}
73
74#[derive(Debug, Deserialize)]
75#[allow(non_camel_case_types)]
76pub enum CallStatus {
77    queued,
78    ringing,
79    #[serde(rename = "in-progress")]
80    inprogress,
81    canceled,
82    completed,
83    failed,
84    busy,
85    #[serde(rename = "no-answer")]
86    noanswer,
87}
88
89#[derive(Deserialize, Debug)]
90pub struct CallResp {
91    pub from: String,
92    pub to: String,
93    pub sid: String,
94    pub start_time: Option<String>,
95    pub status: CallStatus,
96    pub account_sid: String,
97    pub caller_name: Option<String>,
98    pub duration: Option<i32>,
99    pub price: Option<String>,
100    pub price_unit: String,
101    pub uri: String,
102    pub date_created: Option<String>,
103    pub end_time: Option<String>,
104    pub phone_number_sid: String,
105    pub direction: Direction,
106}
107
108#[derive(Debug, Deserialize)]
109#[allow(non_camel_case_types)]
110pub enum Direction {
111    inbound,
112    #[serde(rename = "outbound-api")]
113    outbound_api,
114    #[serde(rename = "outbound-dial")]
115    outbound_dial,
116    #[serde(rename = "trunking-terminating")]
117    trunking_terminating,
118    #[serde(rename = "trunking-originating")]
119    trunking_originating,
120}
121
122#[derive(Debug)]
123pub struct SendCall<'a> {
124    pub call: Call<'a>,
125    pub client: &'a Twilio,
126}
127
128execute!(SendCall);
129
130#[async_trait]
131impl<'a> TwilioRequest for SendCall<'a> {
132    type Resp = CallResp;
133
134    async fn run(&self) -> TwilioResp<TwilioJson<Self::Resp>> {
135        let call = self.call.to_string();
136        self.execute(Method::POST, "Calls.json", Some(call)).await
137    }
138}
139
140impl<'a> SendCall<'a> {
141    pub fn sid(mut self, sid: &'a str) -> SendCall<'a> {
142        self.call.sid = Some(sid);
143        self
144    }
145
146    pub fn callerid(mut self, callerid: &'a str) -> SendCall<'a> {
147        self.call.callerid = Some(callerid);
148        self
149    }
150
151    pub fn machine_detection(mut self, machine_detection: bool) -> SendCall<'a> {
152        self.call.machine_detection = Some(machine_detection);
153        self
154    }
155
156    pub fn record(mut self, record: bool) -> SendCall<'a> {
157        self.call.record = Some(record);
158        self
159    }
160
161    pub fn send_digits(mut self, send_digits: &'a str) -> SendCall<'a> {
162        self.call.send_digits = Some(send_digits);
163        self
164    }
165
166    pub fn status_callback(mut self, callback: &'a str) -> SendCall<'a> {
167        self.call.status_callback = Some(callback);
168        self
169    }
170
171    pub fn callback_event(mut self, event: CallbackEvent) -> SendCall<'a> {
172        self.call.callback_event = Some(event);
173        self
174    }
175
176    pub fn timeout(mut self, timeout: &'a str) -> SendCall<'a> {
177        self.call.timeout = Some(timeout);
178        self
179    }
180}