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.


1COMMENTS0RECOMMENDS

Using TWILIO API with R Programming Language & rCurl

76
POSTED IN: Data Analytics & Visualization Blog

 

 

Start with Twilio XML/CURL instructions

https://www.twilio.com/docs/api/rest/sending-messages

Super Basic R Code - rCurl

######## STEP #0

#Install these packages and load libraries. RCurl is really imporant here.
library(rjson)
library(jsonlite)
library(XML)
library(httr)
library(rjson)
library(RCurl) # the big one

## This next 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")))

######## STEP #1

#- SIGN UP FOR A FREE TWILO ACCOUNT - https://www.twilio.com - and send yourself a test text.  Then get your SID/AUTH TOKENS
#TWILIO PRIVATE: ACCOUNT SID ACda5eedceab2a9aae6472320XXXXXXX (you need to get your own :)
#TWILIO PRIVATE: AUTH TOKEN 96ea43a811585a0f959675d1XXXXXX
#TW +1240837XXXX first programmable Twilio number: (240) 837-XXXX

######## STEP #2

#- HAVE A HERO MOMENT!  Hit the API with a getURL
# FORMAT: https://{error: no user with username 'AccountSid}:{AuthToken' was found}@api.twilio.com/2010-04-01/Accounts   NO BRACKETS IN THE CALL (no {})
authenticate_twilio <- ("https://ACda5eedceab2a9aae6472320bdc54XXXX:96ea43a811585a0f959675d1f951XXXX@api.twilio.com/2010-04-01/Accounts")
# here is action 
authenticate_response <- getURL(authenticate_twilio)
print(authenticate_response)  ## if this work, you should get a long string that contains something like "<FriendlyName>ryan###@gmail.com's Account
GET(authenticate_twilio) # this work too? yes

postForm("https://ACda5eedceab2a9aae6472320bdc54XXXX:96ea43a811585a0f959675d1f95XXXX@api.twilio.com/2010-04-01/Accounts/ACda5eedceab2a9aae6472320bdc54XXXX/Messages.XML",
         .params = c(From = "+1240837XXXX", To = "+1415672XXXX",Body = "TWILIO TEST using R PROGRAMMING LANGUAGE - rCurl / postForm")
) ## Returns "Error: UNAUTHORIZED" but works! Sends SMS

postForm("https://ACda5eedceab2a9aae6472320bdc54xxxx:96ea43a811585a0f959675d1f951xxxx@api.twilio.com/2010-04-01/Accounts/ACda5eedceab2a9aae6472320bdc54XXXX/Messages.XML",
         .params = c(From = "+1240837XXXX", To = "+1510418XXXX",Body = "Wife- you are an excellent wife!")
) ## Returns "Error: UNAUTHORIZED" but works! Sends SMS

Success!

####### STEP #3

#- Pat self on back.  You are now a full stack programmer.  (Well, no, not really. But good job anyway)
# back <- "pat pat"

####### A few extra pieces
## THIS WORKS
getURL("https://api.twilio.com/2010-04-01/Accounts/ACda5eedceab2a9aae6472320bdc54XXXX/Messages.JSON",    
       userpwd = "ACda5eedceab2a9aae6472320bdc54XXXX:96ea43a811585a0f959675d1f951XXXX") ## YAY! Looks like this works

getURL("https://api.twilio.com/2010-04-01")  # working? - get's a response - generic/public
httpGET("https://api.twilio.com/2010-04-01")  # working?
GET("https://api.twilio.com/2010-04-01")  # working? yes - 200 response

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