azure_functions/bindings/
twilio_sms_message.rs

1use crate::{
2    rpc::{typed_data::Data, TypedData},
3    FromVec,
4};
5use serde::{Deserialize, Serialize};
6use serde_json::{to_string, to_value, Value};
7
8/// Represents the Twilio SMS message output binding.
9///
10/// The following binding attributes are supported:
11///
12/// | Name          | Description                                                                                                                                        |
13/// |---------------|----------------------------------------------------------------------------------------------------------------------------------------------------|
14/// | `name`        | The name of the parameter being bound.                                                                                                             |
15/// | `account_sid` | The name of an app setting that holds your Twilio Account SID. If not set, the default app setting name is "AzureWebJobsTwilioAccountSid".         |
16/// | `auth_token`  | The name of an app setting that holds your Twilio authentication token. If not set, the default app setting name is "AzureWebJobsTwilioAuthToken". |
17/// | `from`        | The default phone number that the SMS text is sent from.                                                                                           |
18/// | `body`        | The default SMS message body to use.                                                                                                               |
19///
20/// # Examples
21///
22/// An example HTTP-triggered function that outputs a Twilio SMS message:
23///
24/// ```rust
25/// use azure_functions::{
26///     bindings::{HttpRequest, HttpResponse, TwilioSmsMessage},
27///     func,
28/// };
29/// use std::borrow::ToOwned;
30///
31/// #[func]
32/// #[binding(name = "output1", from = "+15555555555")]
33/// pub fn send_sms(req: HttpRequest) -> (HttpResponse, TwilioSmsMessage) {
34///     let params = req.query_params();
35///
36///     (
37///         "Text message sent.".into(),
38///         TwilioSmsMessage {
39///             to: params.get("to").unwrap().to_owned(),
40///             body: params.get("body").map(ToOwned::to_owned),
41///             ..Default::default()
42///         },
43///     )
44/// }
45/// ```
46#[derive(Debug, Default, Clone, Serialize, Deserialize)]
47#[serde(rename_all = "camelCase")]
48pub struct TwilioSmsMessage {
49    /// The phone number to send the SMS message to.
50    pub to: String,
51    /// The optional phone number to send the SMS message from. If None, the `from` binding attribute is used.
52    pub from: Option<String>,
53    /// The optional SMS message body. If None, the `body` binding attribute is used.
54    pub body: Option<String>,
55}
56
57#[doc(hidden)]
58impl Into<TypedData> for TwilioSmsMessage {
59    fn into(self) -> TypedData {
60        TypedData {
61            data: Some(Data::Json(
62                to_string(&self).expect("failed to convert Twilio SMS message to JSON string"),
63            )),
64        }
65    }
66}
67
68#[doc(hidden)]
69impl FromVec<TwilioSmsMessage> for TypedData {
70    fn from_vec(vec: Vec<TwilioSmsMessage>) -> Self {
71        TypedData {
72            data: Some(Data::Json(
73                Value::Array(vec.into_iter().map(|m| to_value(m).unwrap()).collect()).to_string(),
74            )),
75        }
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82
83    #[test]
84    fn it_serializes_to_json() {
85        let json = to_string(&TwilioSmsMessage {
86            to: "foo".to_owned(),
87            from: Some("bar".to_owned()),
88            body: Some("baz".to_owned()),
89        })
90        .unwrap();
91
92        assert_eq!(json, r#"{"to":"foo","from":"bar","body":"baz"}"#);
93    }
94
95    #[test]
96    fn it_converts_to_typed_data() {
97        let message = TwilioSmsMessage {
98            to: "foo".to_owned(),
99            from: Some("bar".to_owned()),
100            body: Some("baz".to_owned()),
101        };
102
103        let data: TypedData = message.into();
104        assert_eq!(
105            data.data,
106            Some(Data::Json(
107                r#"{"to":"foo","from":"bar","body":"baz"}"#.to_string()
108            ))
109        );
110    }
111}