9 releases (5 stable)

1.1.0 Mar 25, 2024
1.0.3 Aug 16, 2023
1.0.2 Jan 29, 2023
1.0.0 Sep 8, 2020
0.1.0 Apr 20, 2015

#927 in Network programming

Download history 198/week @ 2024-08-22 184/week @ 2024-08-29 264/week @ 2024-09-05 248/week @ 2024-09-12 292/week @ 2024-09-19 235/week @ 2024-09-26 191/week @ 2024-10-03 307/week @ 2024-10-10 234/week @ 2024-10-17 167/week @ 2024-10-24 232/week @ 2024-10-31 230/week @ 2024-11-07 200/week @ 2024-11-14 358/week @ 2024-11-21 230/week @ 2024-11-28 138/week @ 2024-12-05

986 downloads per month
Used in 3 crates

MIT license

25KB
653 lines

twilio-rs

twilio-rs is a Rust library for integrating with Twilio. It tries to present an idiomatic Rust interface for making requests to the Twilio API, and validating, parsing, and replying to webhooks that Twilio makes to your server.

First, you'll need to create a Twilio client:

let client = twilio::Client::new(ACCOUNT_ID, AUTH_TOKEN);

Now, you can use that client to make or receive Twilio requests. For example, to send a message:

client.send_message(OutboundMessage::new(from, to, "Hello, World!")).await;

Or to make a call:

client.make_call(OutboundCall::new(from, to, callback_url)).await;

Of course, much of our interaction with Twilio is by defining resources that respond to Twilio webhooks. To respond to every SMS with a customized reply, in your server's handler method:

use hyper::{Body, Request, Response};
use std::convert::Infallible;

async fn handle_request(req: Request<Body>) -> Result<Response<Body>, Infallible> {
    let client = ...;
    let response = client.respond_to_webhook(req, |msg: Message| {
        let mut t = Twiml::new();
        t.add(&twiml::Message {
            txt: format!("You told me: '{}'",
            msg.body.unwrap()),
        });
        t
    })
    .await;
    Ok(response)
}

Alternatively, to respond to a voice callback with a message:

use hyper::{Body, Request, Response};
use std::convert::Infallible;

async fn handle_request(req: Request<Body>) -> Result<Response<Body>, Infallible> {
    let client = ...;
    let response = client.respond_to_webhook(req, |msg: Call| {
        let mut t = Twiml::new();
        t.add(&twitml::Say {
            txt: "Thanks for using twilio-rs. Bye!".to_string(),
            voice: Voice::Woman,
            language: "en".to_string(),
        });
        t
    })
    .await;
    Ok(response)
}

Using the respond_to_webhook method will first authenticate that the request came from Twilio, using your AuthToken. If that fails, an error will be sent to the client. Next, the call or message will be parsed from the parameters passed in. If a required field is missing, an error will be sent to the client. Finally, the parsed object will be passed to your handler method, which must return a Twiml that will be used to respond to the webhook.

The respond_to_webhook method is designed to work on Hyper Requests and Responses. Hyper is also used internally to make requests to Twilio's API.

Dependencies

~10–22MB
~322K SLoC