How to send text messages from R

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

How to send text messages from R
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.