Send and receive SMS messages
2017-02-06

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. Zang 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.

Zang Setup

We created a free trial account with Zang.io, 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 Zang 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 Zang is that inbound messages are FREE (as of this writing), only requiring a Zang phone number to receive them. Let's start with the script at DragToCode, then show how to configure Zang 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 Zang 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 Zang 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 Zang'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 Zang to reference your script. It utilizes the Zang API to set the URL for your phone.

Wait. How does this even work?

When Zang receives a text, it calls the URL you set with some extra information like the sending phone ("from" variable), time ("date_sent"), 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.

A Complex Example

Because Zang does not have a mechanism for keeping state between requests, you can use Redis to store where you are in the script:


If the key (the phone number) isn't set, then we text "what is your name?" and set the key to 1.
If the key is set to 1, we see if the entire message body is "arthur." If it is, send "what is your quest?" and set the key to 2. If the response is not "arthur," then text "who?"
If the key is set to 2, we check anywhere in the response text for "grail." If it's there, send "what is the flight speed of a fully laden swallow?" and delete the Redis key. If it's not, we send "who?" and keep the key intact.
If nothing at all matches, we send back "no idea what's going on."

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!