Using Twilio from SmartThings

In our prior post, we covered using Twilio for our vacation rental.  It gives us a great combination of low cost and programmability.  In this post, we're going to extend the use of our Twilio number to automatically text guests when they first unlock the door.

Why

Guests appreciate an attentive, but not overly snoopy, host.  I always like it when a host reaches out to me soon after checkin and otherwise leaves me alone.  However, as a host, I don't want to babysit the property, so this will do it for me.

SmartThings classic supports sending SMS by simply calling sendSms().  It's easy and it works well.  However, it comes from a short code number, so it's a little strange for guests, plus they can't reply.  Using Twilio, the guest only interacts with you and your number.  If they reply, you will see it.

How

The groovy environment used by SmartThings is limited.  We use the somewhat poorly documented and named httpPost() call to call the Twilio API.  SmartThings does not mention anywhere that this call works with HTTPS, but it does.  I have no idea if it does any certificate validation or anything.  I introduced a new twilio_sms() function as follows:

def twilio_sms(sms, msg) {
if (sms) {
String charset = "UTF-8"
String url = String.format("https://%s:%s@api.twilio.com/2010-04-01/Accounts/%s/Messages.json",
URLEncoder.encode(twsid, charset),
URLEncoder.encode(twtok, charset),
URLEncoder.encode(twacct, charset))
String query = String.format("To=%s&Body=%s&From=%s",
URLEncoder.encode(sms, charset),
URLEncoder.encode(msg, charset),
twphone)

try {
httpPost(url, query) { resp ->
log.debug "response data: ${resp.data}"
}
} catch (e) {
notify(ownersms, "Problem sending twilio sms to $sms: $e")
}
}
}

The code ends up being pretty short.  It took me a bit to figure out that it supported the username:password in the URL shortcut, instead of separating the params and base64 encoding myself.  It also took me a bit to figure out that the From number is required.  Should I also be encoding that?  Yes, probably.  Will fix after confirming the encoded "+" in numbers doesn't cause issues.

How to use with VacationLockManager

Assuming you already have a Twilio account and number, get the VacationLockManager code.  You'll need to install it as a SmartApp in your SmartThings classic app.  See the original post about it if you need help.

You'll need to get the following information to configure the app:

  1. Your Twilio account ID.  This will look like ACXXXXXXXXX and can be found on your Twilio console.
  2. A Twilio API key and token.  Go to Programmable Voice -> Settings -> API Keys.  Click the red + to make a new one.  The SID and TOKEN are what you need for the next two fields in the SmartApp.
  3. Your Twilio number to send the SMS messages from.

Your guests will now get a welcome text automatically when they first unlock the door.  Any problems sending to Twilio will be sent to you via SMS from SmartThings, if you configure the owner phone number.

What next?

I may change the code to wait a bit after unlocking before sending the welcome SMS.  Sending immediately is just the easiest thing to do.

This could also be extended to send a text after they check out or midway through their stay.