Send and receive SMS messages
2016-11-22

Love them or hate them, text messages are a part of life, and your implementation may need to contact customers, vendors, or staff at any time of the day or night. Twilio provides a frictionless way to send your messages at some of the lowest cost in the industry. You can both send and receive messages in DragToCode with little effort, and we had fun connecting them.

Here's how we did it.

Twilio Setup

We created a free trial account with Twilio, but then we went ahead and spent the $10 for credit in order to buy a phone number to play with. The location of the phone number isn't important, but we were able to score a local virtual number.

The last step before we leave the Twilio site is to copy the account SID and token.

Script Setup

Now that we picked up a phone number, account SID, and token, we can enter it in a DragToCode connector.
A direct route is https://www.dragtocode.com/connectors_view?action=SMS

Here's the super-simple, one-block way to send a text message anywhere:

CAVEAT SENDER

The one and only caveat for sending text messages is in formatting the phone number: format it for an international call. In the United States, this means preface your number with "+1" (like "+14152341234") and in the UK "+44" (like "+441143520352").

Receiving Texts

A nice selling point for Twilio is that inbound messages are FREE (as of this writing), only requiring a Twilio phone number to receive them. Let's start with the script at DragToCode, then show how to configure Twilio to receive it.

This is a simple play on the Abbott and Costello "Who's On First" bit.

  • if we receive "who", we tell them "he's on first"
  • if we receive "what", we tell them "he's on second"
  • if we receive anything else, we tell them "Abbott and Cosello baseball"
We've set up a script, but our phone number doesn't point at it. Let's fix that next.

Configuring Twilio For Incoming SMS

When we purchased the phone number, we skipped the part about configuring it for incoming. There are two ways to do that.

Manually
The Twilio site has a spot for setting the URL of where you want notifications to go. We want to receive them in our newly-created script, so we'd enter:
https://www.dragtocode.com/script/voice?token=[YOUR_TOKEN_HERE]
Into Twilio's interface:

Be sure to click save when you're done.
Breathlessly panic! It says voice in the URL! The reason why it says voice is that the voice and SMS interfaces are identical, so we re-use the URL.

Automatically
A second, easier method is part of the connector interface we didn't mention is the "Incoming Script" at the bottom. There, you can select your script by name from the list and click "Set Hook" to set the URL in Twilio to reference your script. It utilizes the Twilio API to set the URL for your phone.

Wait. How does this even work?

When Twilio receives a text, it calls the URL you set with some extra information like the sending phone ("from" variable), time ("date_created"), identification number ("sid"), and the text of the message ("body"). DragToCode receives this like a input parameters from any internet form, and makes them available to the script.

Maintaining State

DragToCode sends every variable in your script as part of the cookie submission. What that means to you is that you only need to set a regular DragToCode variable to get it back when the participant responds.

Conclusion

Customers prefer messages to almost any other form of communication. We're all texters, so why not interact with your customers that way? Give it a try and tell us about the great things you build!