Dream to Learn is shutting down...

We are very sorry to say that Dream to Learn will be shutting down as of December 28th, 2019. If you have content that you wish to keep, you should make a copy of it before that date.


0COMMENTS0RECOMMENDS

USING TWILIO API WITH R PROGRAMMING LANGUAGE: RCURL & HTTR (Final Post 3 of 3)

78
POSTED IN: Data Analytics & Visualization Blog

Twilio REST API - SMS Text with RCURL & HTTR:

OK- great news!  After some back and forth with the super helpful Alex Chan from Twilio support (thanks Alex!) - we now have BOTH HTTR and RCURL behaving well.  The final key he provided was "httpauth=AUTH_BASIC" below - which helped RCURL handle the response well - and not error out.

So net result:  We can now hit Twilio REST API and send SMS messages to cell phones from R.  I put a few dollars on my Twilio account - so not sure if this works on the 'free' version.  I encourage you to try.

See also: https://github.com/rustyoldrake/twilio_rcurl_httr for code.

Happy Twilio texting!  Ryan

 

##########################################################
## Using R Programming Language with Twilio REST API
## Basic Test Code - using HTTR and RCURL to send SMS Message from active (paid) account 
## Ryan Anderson - December 2014
##########################################################

library(httr)
library(RCurl)

## The SID, TOKEN and From/To Numbers (you need to insert your own)
Twilio_Account_SID <- "ACda5eedceab2a9aae6472320bdc5XXXX"
Twilio_Account_TOKEN <- "96ea43a811585a0f959675d1f95XXXX"
Number_From <- "+124083XXXX"
Number_To <- "+1415672XXXX"

## WORKS -> HTTR Package using "POST"
the_url <- paste("https://api.twilio.com/2010-04-01/Accounts/",Twilio_Account_SID,"/Messages.XML",sep="")
POST(the_url,
     body = list(
       From = Number_From,
       To = Number_To,
       Body = "TWILIO TEST using R PROGRAMMING LANGUAGE - HTTR Package - Test #55"
     ),
     config=authenticate(Twilio_Account_SID,Twilio_Account_TOKEN,type="basic")
)


##########################################################

## WORKS - RCURL Package using PoSTFORM

## To begin: this line sets CERT Global to make a CA Cert go away - http://stackoverflow.com/questions/15347233/ssl-certificate-failed-for-twitter-in-r
options(RCurlOptions = list(
  cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"),
  httpauth=AUTH_BASIC
)
)
# NOTE - the "httpauth=AUTH_BASIC" piece gets rid of the "Error: UNAUTHORIZED" message (Thanks Twilio Alex)

the_url <- paste("https://api.twilio.com/2010-04-01/Accounts/",Twilio_Account_SID,"/Messages.XML",sep="")
postForm(the_url,
         .opts = list(
           userpwd = paste(Twilio_Account_SID,":",Twilio_Account_TOKEN,sep=""),
           useragent = "RCurl",
           verbose = TRUE
         ),
         .params = c(From = Number_From, 
                     To = Number_To, 
                     Body = "TWILIO TEST using R PROGRAMMING LANGUAGE - RCURL Package: POSTFORM Test #56" 
         )
)

Interested in more content by this author?

About the Author

Ryan Anderson

Ryan Anderson

Hi! I like to play with data, analytics and hack around with robots and gadgets in my garage. Lately I've been learning about machine learning.

About this blog

Description is...<br/>Data Analytics & Visualization Blog - Generating insights from Data since 2013

Created: July 25, 2014

Englishfrançais

Up Next