Send and receive FileMaker data with Twilio
If you’re using SMS with a FileMaker solution, you’re probably doing more sending than receiving. Maybe you’re using a service that converts email to SMS. Or maybe you signed up with an SMS provider like Twilio, and are sending texts from your database with httpspost. That’s fine, but what about sending an SMS /to/ a FileMaker solution? I’m not talking about just saving inbound messages as records. I mean doing a find request based on the sender’s cell number and message content, and responding with record data.
Before we dive into that, this is /not/ a post about how to set up a Twilio account. I’m assuming you’ve done that, and defined your
Request URL, and copied the Twilio framework to your webserver. This post is about using the FileMaker PHP API to respond to inbound messages. Ok. Let’s go.
First things first…
Now let’s capture the sender’s cell number and message content. Take note of Twilio’s formatting. You’ll need to have some kind of “phoneCalc” field in your FileMaker database that matches this format. Make sure it’s on the layout you’re querying.
You can do /a ton/ of stuff with these two strings. Let’s check $body for a keyword.
default: first, so we can respond if FileMaker doesn’t find a match or understand the sender’s message.
Now look back at our
switch. If an artist texts the word ‘profile’, we want to
sendArtistURL(). We have the sender’s cell number already, in
$from, so let’s pass that into a find request and see if there’s a matching database record. If you’re thinking, “Hey, that’s like doing a find request in CWP!”, you’re right.
If there’s no match, we’ll build a
$response and tell our sender we couldn’t find anything. If we get back a
$record, we can respond with field data. Neat.
$url is a calc field. Each artist in my database has an auto-enter uuid, and I’m using that to build this simple url:
"http://my.server/directory/?uuid=" & Artist::uuid
Basically, this replies with a link that takes the user directly to a mobile-friendly form and fetches their record with a
$_GET. I used an artist uuid, but it could have been a uuid for an invoice record, a store product, an event page, etc.
. . .
That was an easy example. My production file has a dozen+ switch cases, and functions that check for volunteer teams, etc. Users can even send texts like “5 hours” to log their time. Think about that for a minute. If you have a $record, you have a primary key. You can turn around and use that on a related layout with
newAddCommand() and set the foreign key. Très cool.