See how easily you can send texts directly from an R script with Twilio and the twilio R package
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.
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.


