0COMMENTS0RECOMMENDS

Twilio Extensions to STEM Labs - How to SMS and MMS to your phone and interact

31
POSTED IN: Building Bridges from R to IBM Watson

STEM - Add Twilio?  Kids - shall we make the phone go "ping"?

 

My kids LOVE the movie Inside Out.  We thought we'd have a little dad and daughters coding time - seeing if the Tone Analyzer could produce the signals big enough to signal the main emotions (who are, conveniently) the characters from inside out.

Do do this we needed

  • Installed version of R, R Studio and Packages below
  • IBM Bluemix Account (free!) with the "Tone" service endpoint (Also free to test)
  • R COde (below) equipped with updated credentails

 

 

 

IMAGES: the images below belong to Pixar Animation Studios and Walt Disney Pictures - and are presented here to make the educational subject matter more interesting.  Images should NOT be used in anything commercial unless you receive permission from the media owner(s)

 

 

 

 

The Code

######################################################
### Experimental Code.  Experimental R Interface for IBM Watson Services -
### Focus: IBM Watson Tone Analyzer is a service that helps people comprehend, consume, and revise the language tone of their writing for more efficient communications
### Focus: TWILIO - https://www.twilio.com/ - apps to communicate with the world
### https://github.com/rustyoldrake/R_Scripts_for_Watson - Ryan Anderson  - this is my test code.  Use at your own peril!  Representations do not necessarily represent the views of my employer
######################################################

library(RCurl) # the big one
library(rjson)
library(jsonlite)
library(XML)
library(httr)
library(rjson)

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

######### Housekeeping And Authentication
setwd("/Users/ryan/Documents/Service - Tone Analyzer")
getwd()
source("keys.R") # this files is where you put your Access Credentials from Bluemix (username and password)

## check we got FOUR ITEMS: authentication and phone numbers
AccountSID_TWILIO # confirm SID is here from Keys. R file (you need to sign up to Twilio and get keys)
AuthToken_TWILIO # Token
NUMBER_FROM # check it's looking ok
NUMBER_TO # check it's looking ok

######## STEP #1 - Confirm Credentails work and API is responding happily
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

ABC_authenticate_twilio <- paste("https://",AccountSID_TWILIO,":",AuthToken_TWILIO,"@api.twilio.com/2010-04-01/Accounts",sep="")
authenticate_response <- getURL(ABC_authenticate_twilio)
print(authenticate_response)  ## if this work, you should get a long string that contains something like "<FriendlyName>ryan###@gmail.com's Account

getURL(paste("https://api.twilio.com/2010-04-01/Accounts/",AccountSID_TWILIO,"/Messages.JSON",sep=""),userpwd = paste(AccountSID_TWILIO,":",AuthToken_TWILIO,sep=""))

GET(ABC_authenticate_twilio) # this work too? 200 yes?

######## STEP #2 - Let's send an SMS Text
ABC_post_twilio <- paste("https://",AccountSID_TWILIO,":",AuthToken_TWILIO,"@api.twilio.com/2010-04-01/Accounts/",AccountSID_TWILIO,"/Messages.XML",sep="")
ABC_post_twilio # look good?

########## THIS FUNCTION Receives query/message and also emotion and pushes out to SMS/MMS via twilio

send_SMS_results <- function(message,emotion)
{
  switch(emotion,
         "joy" = image_link <- "https://dreamtolearn.com/internal/doc-asset/251BS61FR6H557318H0CS0YLS/joy.jpg",
         "sadness" = image_link <- "https://dreamtolearn.com/internal/doc-asset/251BS61FR6H557318H0CS0YLS/sad.jpg",
         "anger" = image_link <- "https://dreamtolearn.com/internal/doc-asset/251BS61FR6H557318H0CS0YLS/anger.jpg",
         "disgust" = image_link <- "https://dreamtolearn.com/internal/doc-asset/251BS61FR6H557318H0CS0YLS/disgust.jpg",
         "fear" = image_link <- "https://dreamtolearn.com/internal/doc-asset/251BS61FR6H557318H0CS0YLS/fear.jpg"
        )  
POST(ABC_post_twilio,
     body = list(
       From = NUMBER_FROM,
       To = NUMBER_TO,
       Body = paste("MESSAGE ANALYZED: ",message,sep=""),
       MediaUrl = image_link
      ))
}

 

 

 

 

http://copyright.gov/fair-use/ "Educational multimedia projects created under these guidelines incorporate students' or educators' original material, such as course notes or commentary, together with various copyrighted media formats including but not limited to, motion media, music, text material, graphics, illustrations, photographs and digital software which are combined into an integrated presentation. "

https://en.wikipedia.org/wiki/Fair_use

Information here is my own personal blog, and does not necessarily reflect the views of my employer.

 

 

 

Before you can comment, you need to sign-up or login

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

This is an informal blog that explores tools, code and tricks that group members have developed to engage IBM Watson cognitive computing services - from the R Programming Language. Packages include RCURL to access Watson APIs - for services that include Natural Language Classifier and Speech to Text. THIS IS MY PERSONAL BLOG - it does not represent the views of my employer. Code is presented as 'use at your own risk' (it has lots of bugs)

Created: September 13, 2015

English

Up Next