Sharon Machlis
Contributing Writer

How to send text messages from R

how-to
Jan 10, 20204 mins

See how easily you can send texts directly from an R script with Twilio and the twilio R package

Do More With R [video teaser/video series] - R Programming Guide - Tips & Tricks
Credit: Thinkstock

Did you know that you can send text messages directly from R? Itโ€™s easy . . . and if youโ€™re wondering why youโ€™d want to, do you really need a reason beyond โ€œbecause I canโ€?

But seriously, scripted texting can be useful beyond simple fun. Wouldnโ€™t you like to receive a text when a lengthy script finishes or throws an error? Or if an automated script returns a value you didnโ€™t expect, or even to send texts to a list of phone numbers?

There are a few ways to generate texts in R. One of the easiest is to use the Twilio service and theย twilio R package.

First, you will need a Twilio account. Go to Twilio.com and sign up for a free account. Once you enter your info, youโ€™ll need to verify your phone number โ€” either by having them text or call you with a code.

After you sign up, you should see a dashboard that looks something like the screenshot below.

Twilio dashboard Screenshot by Sharon Machlis, IDG

Twilio dashboard for a new user.ย 

Youโ€™ll need to take note of your ACCOUNT SID and AUTH TOKEN. Also, get a trial number as suggested by that red button.

Messages cost less than a penny each, and the trial has $15 in credits โ€” enough to play with. The more important limit is that you can only send messages to phone numbers that youโ€™ve verified and added to your account. You can verify more numbers from the Twilio dashboard (or get a paid account).

After setting up your Twilio account, install the twilio R package from CRAN with install.packages("twilio") and then load it the usual way withย ย library(twilio). Save your account SID and TOKEN to the specific R environment variables that the package expects: TWILIO_SID and TWILIO_TOKEN. You can do that at the start of each session, using code like the lines below.

Sys.setenv(TWILIO_SID = "Your SID")
Sys.setenv(TWILIO_TOKEN = "Your Token")

Alternatively, you can save these variables once to your .Renviron file, which is easily accessible with usethis::edit_r_environ(). Note that you will need the usethis package installed for that.

Finally, weโ€™re ready to text.

The sending and receiving phone numbers should be in a format such as +15088970700. That is, start with a plus sign before the country code followed by numbers only โ€” no parentheses, dashes, or dots.ย 

The function to send an SMS is tw_send_message() with the syntax tw_send_message(the_receiving_number, my_sending_number, my_message_body) and an optional fourth argument for media URL. Thatโ€™s it! A simple example might look like this:

tw_send_message(
  to = "+16035551212",
  from = "+15088970700",
  body = paste("I am sending this message from an R script!")
)

If you store the results in a variable, youโ€™ll have a list with more than a dozen values:

my_message <- tw_send_message(
  to = Sys.getenv("to_number"),
  from = Sys.getenv("from_number"),
  body = paste("I am sending this message from an R script!")
)
names(my_message)
 [1] "sid"           "date_created" 
 [3] "date_updated"  "date_sent"    
 [5] "to"            "from"         
 [7] "body"          "status"       
 [9] "num_segments"  "num_media"    
[11] "direction"     "api_version"  
[13] "price"         "price_unit"   
[15] "error_code"    "error_message

If you print the message body, youโ€™ll see that trial accounts add โ€œSent from your Twilio trial account.โ€

> my_message$body
[1] "Sent from your Twilio trial account -I am sending this message from an R script!"

Once you set up a Twilio account and your SID and token variables, the rest is easy.

Want to send email or a Slack message from R instead? Weโ€™ve got you covered! Email: How to send email from R and Gmail. Slack: How to Slack from R.

For more R tips, head to the Do More With R page at https://bit.ly/domorewithR or the Do More With R playlist on the IDG TECHtalk YouTube channel.