Code Monkey home page Code Monkey logo

twilio-rs's Introduction

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.

Using rustls

If you want to replace the use of native TLS (usually OpenSSL) with rustls you can enable the rustls feature like so:

twilio = { version = "1.0", default-features = false, features = ["rustls"] }

twilio-rs's People

Contributors

ebkalderon avatar lancecarlson avatar neil-lobracco avatar 9at8 avatar benhansen-io avatar tie-rack avatar danbruder avatar kecolus avatar marcopolo avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.