More for you to like:
Closed Captioning Closed captioning available on our YouTube channel
You Might Like

How to send SMS text messages from R

InfoWorld | Jan 9, 2020

See how to send a text message directly from an R script with Twilio and the Twilio R package.

Copyright © 2020 IDG Communications, Inc.

Similar
Hi. I’m Sharon Machlis at IDG, here with episode 40 of Do More With R: Send SMS text messages right from R.

Why would you want to send a text from R? Do you really need a reason? It’s totally cool! But it could be useful, too: to send notices when a script stops running, or throws an error, or sees an unexpected value. You could also use it to send texts to list of numbers. All kinds of things!

There are a few ways to do this. One of the easiest is to use a platform called Twilio, and the twilio R package.

Step 1: You need a Twilio account. Go to Twilio.com that’s t.w.i.l.i.o dot 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 you or call you with a code.

Let me open a screen shot of what you’ll see next. See where it says “ACCOUNT SID” and “AUTH TOKEN”? Well, I hid my account SID, but yours should show up there. You’ll need both of those. 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 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.

OK, now for some R.

Install the twilio package from CRAN with install.packages() and then load it the usual way with library. Save your account SID and TOKEN to these specific R environment variables, that the package expects: TWILIO_SID and TWILIO_TOKEN in all caps. You can do that at the start of each session, using code like I have here. Or, you can save it once to your R environment file.

Finally, we’re all set to text! We want the from and to numbers to be in this format: plus sign, country code, area or other regional code, and number. Start with a plus sign before the country code! After that just digits – no parentheses, dashes, or dots. I saved my numbers for this video as environmental variables too so my Google Voice number doesn’t show on screen.

The function to send an SMS is tw_send_message(). It takes the to phone number as the first argument, from phone number second, and message body third. There’s also an optional fourth argument for media URL. That’s it! You can see the result is a list and we can see some of the contents. If I check my Google Voice account there should be a new message. And there we have it: SMS text messageing from R!

That’s it for this episode, thanks for watching! For more R tips, head to the Do More With R page at bit-dot-ly slash do more with R, all lowercase except for the R. You can also find the Do More With R playlist on the YouTube IDG Tech Talk channel -- where you can subscribe so you never miss an episode. Hope to see you next time!
Popular
Featured videos from IDG.tv