Since we use Twilio in a lot of our R packages while running analysis for our clients, we have built a wrapper for contacting the API. You can see and install it here. We have also built API wrappers for Stripe, Sendwithus, and Mandrill. Using these helps prevent code bloat and makes it cleaner. A lot of this post is going to be very similiar to our old blog post about Twilio and Shiny.

As Shiny Server gets more and more production ready, developers are adding new features that typically aren't easy to build in—examples include automated phone notifications through Twilio. Twilio allows your application to send phone calls and text messages to users of your application when a triggering event occurs. For example, we use it to call our clients of potential flaws, taints, and contamination's in their products in real time.

This tutorial will teach you to integrate Twilio with a basic Shiny application which places a sample call to your users. This tutorial will leave you with a working application which you can build into your own needs. This tutorial will require the following:

  • R
  • Shiny

This tutorial focuses on Shiny Server, but most of the code could easily be converted to other R based web frameworks or batch scripts. The code for the Shiny Version is located on GitHub.

Our current situation provides is we have a button and field in shiny through which our users can enter their phone number and hit the button to test. Here's how we did it in R. The first thing we needed was a simple input and button in our Ui.R file which was the following code:

 1 div(class="message_test",
 2     div(class="center",
 3         textInput("phone_number", label="", value="")),
 4     div(class="center",
 5         HTML("<div class='center'>
 6             <button id='send_test_call' class='action-button btn btn-default '>
 7             Send test Call</button>
 8             </div>")
 9         )
10     )

Twilio works by sending an API call with the data necessary to work including the url with XML code. The RTwilio package allows you to either host your own xml file or use the echo Twimlet to have a "stateless" call. The phone number and account information is sent through the API. Attached to the API call is a URL that Twilio will go to in order to grab the XML or TwiML. This is the data specifying what the text-to-speech says, as well as other configurations. Check out the TwiML information here.

We no longer store our XML files on Amazon S3 as we did in the past since starting to use the echo Twimlet. You can still use our RS3 package to upload to S3 if you need to with linux.

Now that the frontend is built, let's build the backend. In our server.R file, we have an observe function which will check for the button being clicked input$send_test_call and that the phone number isn't null.

 1 require(RTwilio)
 3 observe({
 4     if(!is.null(input$send_test_call)) {
 5         if(!is.null(isolate(input$phone_number)) &&
 6              isolate(input$phone_number) != "") {
 7              time <- now()
 9 ### We make the message
10             message <- paste("Greetings, it is currently, ",time," . This message can be made to say anything.")
11             twilio_stateless_call("<Your-Twilio-account-SID>",
12                                   "<Your-Twilio-Account-Auth-Token>",
13                                   message=message,
14                                   From="<Your-Twilio-phone-number>",
15                                   To=("+1",isolate(input$phone_number)))
16         }
17     }
18 })

Basically, you wait until the button is pressed and you make sure that a phone number has been entered.It is very easy with the package, and you can also easily send a SMS message. If doing this in pure R without Shiny, the only things you need are the following:

 1 require(RTwilio
 2 time <- now()
 4 ### Phone number
 5 call_from <- "+17248675309"
 7 ### We make the message
 8 message <- paste("Greetings, it is currently, ",time," . This message can be made to say anything.")
10 twilio_stateless_call("<Your-Twilio-account-SID>",
11                       "<Your-Twilio-Account-Auth-Token>",
12                       message=message,
13                       From="<Your-Twilio-phone-number>",
14                       To=call_from)

And that's it! That's how we got Twilio to work in R and in Shiny. We are going to expand a lot on this simple version to alert our users in a custom manner. Adapt this to your needs. Just pull the code off the GitHub repo (GitHub) and run the code with your info added in.

Evan Farrell

Lead Engineer

Evan studied Information Sciences and Technology at Penn State, and continues to learn everything he can about technology. Before joining AFS, Evan started learning everything to do with beer and beer production while homebrewing. When not coding, you will find him climbing 14ers, hiking, and doing anything else in the outdoors.

comments powered by Disqus