Twilio SMS And Calls
Simple SMS Inbound
You first need to setup a Twilio account. Go to https://www.twilio.com and create an account. Go to your Twilio console - Phone Numbers - Buy a number. You can buy a local number for as little as $1 per month.
In ThinkAutomation, create a Message Source. Select Twilio as the message source type and click Next.
The webhook URL will be displayed. Click Copy to copy it to the clipboard.
Back in Twilio - select your phone number. In The Messaging section, select Webhook from the A Message Comes In selector and paste the URL shown in ThinkAutomation.
When you save the ThinkAutomation Message Source a new Automation will be created automatically. This Automation will contain Extract Field actions to extract data (from number, message text etc) from the incoming Twilio webhook.
You can then add additional functionality to your Automation.
In the example below, we connect to a local database and lookup from the Customers table using the senders phone number. If a customer does not exist we send back a message and end processing. Otherwise we send back a response based on the message text.
SMS Automation
// Automation Actions Follow
// Extract Twilio SMS Data
from=Extract FieldFrom%msg_body%Json Path"from"
fromCountry=Extract FieldFrom%msg_body%Json Path"fromCountry"
fromCity=Extract FieldFrom%msg_body%Json Path"fromCity"
fromState=Extract FieldFrom%msg_body%Json Path"fromState"
fromZip=Extract FieldFrom%msg_body%Json Path"fromZip"
to=Extract FieldFrom%msg_body%Json Path"to"
toCountry=Extract FieldFrom%msg_body%Json Path"toCountry"
toCity=Extract FieldFrom%msg_body%Json Path"toCity"
toState=Extract FieldFrom%msg_body%Json Path"toState"
toZip=Extract FieldFrom%msg_body%Json Path"toZip"
smsStatus=Extract FieldFrom%msg_body%Json Path"smsStatus"
body=Extract FieldFrom%msg_body%Json Path"body"
numMedia=Extract FieldFrom%msg_body%Json Path"numMedia"
numSegments=Extract FieldFrom%msg_body%Json Path"numSegments"
messageSid=Extract FieldFrom%msg_body%Json Path"messageSid"
accountSid=Extract FieldFrom%msg_body%Json Path"accountSid"
apiVersion=Extract FieldFrom%msg_body%Json Path"apiVersion"
// Find customer in database with this number
Lookup From A DatabaseSQL ServerOn%Connection%SELECT Id,Balance,LastOrder FROM Customers WHERE Phone=@phone%CustomerId%=Id,%Balance%=Balance,%LastOrder%=LastOrder
If%CustomerId%Is BlankThen
Twilio Send SMS MessageTo%from%From%MyNumber%"This phone number is not registered"
bodylower=To Lower Case(%body%)
Twilio Send SMS MessageTo%from%From%MyNumber%"You last order number was: %LastOrder%"
Twilio Send SMS MessageTo%from%From%MyNumber%"Your balance is: %Balance%"
Twilio Send SMS MessageTo%from%From%MyNumber%"Unknown command. Send 'Order' or 'Balance'"
If you want to use the Twilio Send SMS Message action to send outgoing SMS messages you first need to enter your Twilio Account details in the ThinkAutomation Server Settings - Twilio Integration.
The From number for outgoing SMS messages must be one of your Twilio phone numbers. In the above example we created a Solution Constant called %MyNumber% containing our Twilio phone number.
Note: When sending SMS messages the 'To' phone number must always be the full international format (eg: +4477991234567). When you receive an SMS the senders phone number will already be in this format. You can use the Normalize Phone Number action to convert a local number to its international format if required.
SMS Inbound With Wait For Reply
This example expands on the previous. The customer sends 'Info' to our incoming Twilio number. The Lookup From A Database action is used to find the customer using their phone number. If the customer exists it sends back an SMS message asking which product they would like information about.
The Twilio Wait For SMS Reply action is then used to wait for a reply.
Based on the reply it sends an email to the customer with the information (the email address was found in the previous database lookup).
SMS Info Request Automation
// Automation Actions Follow
// Extract Twilio SMS Data
from=Extract FieldFrom%msg_body%Json Path"from"(Database SMS.FromNumber)
Country=Extract FieldFrom%msg_body%Json Path"fromCountry"(Database SMS.Country)
fromCity=Extract FieldFrom%msg_body%Json Path"fromCity"
fromState=Extract FieldFrom%msg_body%Json Path"fromState"
fromZip=Extract FieldFrom%msg_body%Json Path"fromZip"
to=Extract FieldFrom%msg_body%Json Path"to"
toCountry=Extract FieldFrom%msg_body%Json Path"toCountry"
toCity=Extract FieldFrom%msg_body%Json Path"toCity"
toState=Extract FieldFrom%msg_body%Json Path"toState"
toZip=Extract FieldFrom%msg_body%Json Path"toZip"
smsStatus=Extract FieldFrom%msg_body%Json Path"smsStatus"
body=Extract FieldFrom%msg_body%Json Path"body"(Database SMS.message)
numMedia=Extract FieldFrom%msg_body%Json Path"numMedia"
numSegments=Extract FieldFrom%msg_body%Json Path"numSegments"
messageSid=Extract FieldFrom%msg_body%Json Path"messageSid"
accountSid=Extract FieldFrom%msg_body%Json Path"accountSid"
apiVersion=Extract FieldFrom%msg_body%Json Path"apiVersion"
Command=To Lower Case(%body%)
Command=Extract First Word(%Command%)
If%Command%Not EqualinfoThen
// Customer must send 'info' to start the process.
ReturnInvalid command from %from%
// Info request SMS from %from%(Show Notification)(Log)
// Add to SMS received log database
Update A DatabaseSQL ServerOn%SMSLogConnectionString%
// Check if customer has already registered and get the customers name & email
Lookup From A DatabaseSQL ServerOn%ConnectionString%SELECT * FROM Registrations WHERE FromNumber = @FromNumber%CustomerName%=CustomerName,%Email%=Email
If%CustomerName%Is BlankThen
Twilio Send SMS MessageTo%from%From%MyNumber%"Sorry we do not recognize your number. Please use the Register command to register your details."
ReturnNo customer record for %from%
FirstName=Extract First Word(%CustomerName%)
Status(Assign Status)=Twilio Send SMS MessageTo%from%From%MyNumber%"Hello %FirstName%. Thank you for requesting information your product license.
Which product?
1 = ThinkAutomation
2 = WhosOn
Please reply with '1' or '2'."
If%Status%Not EqualdeliveredThen
// SMS could not be delivered to %from%(Log)
ReturnSMS to %from% failed with %Status%
Reply1(Assign Message Text)=Twilio Wait For SMS ReplyFromLast SentWait For120 Seconds
// Set the product depending on the response
Reply1=Extract First Word(%Reply1%)
Send EmailTo%Email%"Parker Software ThinkAutomation License Details "
Send EmailTo%Email%"Parker Software WhosOn License Details"
Twilio Send SMS MessageTo%from%From%MyNumber%"Invalid reply. Please start over."
ReturnInvalid reply from %from%
Twilio Send SMS MessageTo%from%From%MyNumber%"Thank you %FirstName%. We have sent your license information for %Product% to %Email%"
ReturnLicense details for %Product% sent to %Email% from SMS request %from%
Call Me
This sample responds to an SMS with 'call' as the body. It asks the customer which department they need and then places a call with the required department. Once the department call is answered it connects the call to the customers phone. When the call is complete it sends an email to the customer with a recording of the call.
SMS Call Me Automation
// Automation Actions Follow
// Extract Twilio SMS Data
from=Extract FieldFrom%msg_body%Json Path"from"(Database SMS.FromNumber)
fromCountry=Extract FieldFrom%msg_body%Json Path"fromCountry"(Database SMS.Country)
fromCity=Extract FieldFrom%msg_body%Json Path"fromCity"
fromState=Extract FieldFrom%msg_body%Json Path"fromState"
fromZip=Extract FieldFrom%msg_body%Json Path"fromZip"
to=Extract FieldFrom%msg_body%Json Path"to"
toCountry=Extract FieldFrom%msg_body%Json Path"toCountry"
toCity=Extract FieldFrom%msg_body%Json Path"toCity"
toState=Extract FieldFrom%msg_body%Json Path"toState"
toZip=Extract FieldFrom%msg_body%Json Path"toZip"
smsStatus=Extract FieldFrom%msg_body%Json Path"smsStatus"
body=Extract FieldFrom%msg_body%Json Path"body"(Database SMS.Message)
numMedia=Extract FieldFrom%msg_body%Json Path"numMedia"
numSegments=Extract FieldFrom%msg_body%Json Path"numSegments"
messageSid=Extract FieldFrom%msg_body%Json Path"messageSid"
accountSid=Extract FieldFrom%msg_body%Json Path"accountSid"
apiVersion=Extract FieldFrom%msg_body%Json Path"apiVersion"
Command=To Lower Case(%body%)
Command=Extract First Word(%Command%)
If%Command%Not EqualcallThen
ReturnInvalid command from %from%
// Call me back SMS from %from% %(Show Notification)(Log)
// Add to SMS received log database
Update A DatabaseSQL ServerOn%SMSLogConnectionString%
// Check if customer has already registered and get the customer name & email
Lookup From A DatabaseSQL ServerOn%ConnectionString%SELECT * FROM Registrations WHERE PhoneNumber = @FromNumber%CustomerName%=CustomerName,%Email%=Email
If%CustomerName%Is BlankThen
Twilio Send SMS MessageTo%from%From%MyNumber%"Sorry we do not recognize your number. Please use the Register command to register your details."
ReturnNo customer record for %from%
Status(Assign Status)=Twilio Send SMS MessageTo%from%From%MyNumber%"Hello %CustomerName%. Would you like to speak to:
1 = Sales
2 = Support
Please reply with '1' or '2'"
If%Status%Not EqualdeliveredThen
// SMS could not be delivered to %from%(Log)
ReturnSMS to %from% failed with %Status%
Reply1(Assign Message Text)=Twilio Wait For SMS ReplyFromLast SentWait For120 Seconds
Reply1=Extract First Word(%Reply1%)
Twilio Send SMS MessageTo%from%From%MyNumber%"Invalid response. Please start over."
ReturnInvalid reply from %from%
Twilio Send SMS MessageTo%from%From%MyNumber%"Thank you for requesting a call with %Department%. I am connecting you now."
CallStatus(Assign Status)=Twilio Make A Telephone CallTo%from%Using Caller ID%MyNumber%And Say "Hello %CustomerName%. Thank you for requesting a call from Parker Software %Department%. One moment please."Then Connect Call To%ConnectTo%
If%CallStatus%Equal TocompletedThen
Twilio Send SMS MessageTo%from%From%MyNumber%"Thank you for you call. We have emailed a recording of this call to %Email%."
Send EmailTo%Email%"Call With Parker Software %Department%"
Twilio Send SMS MessageTo%from%From%MyNumber%"We could not connect the call with %Department% at this time. An email has been sent to %Department% so that they can call you back."
Send EmailTomissedcalls@parkersoftware.com"Missed call from %CustomerName% for %Department%. Please call back."
ReturnCallback request from %from% for %Department%. Status: %CallStatus%
Run Different Automations Based On The SMS Received Message
When receiving incoming SMS messages you can either have different Twilio numbers and then separate ThinkAutomation Message Sources, or you can have a single Twilio Number with a single ThinkAutomation Message Source. The Automation that is called from this message source can then examine the SMS text and execute different blocks (based on the Select Case action) or call different Automations.
The example below uses the Call action to call different Automations based on the message text received.
SMS Select Automation
// Automation Actions Follow
// Extract Twilio SMS Data
from=Extract FieldFrom%msg_body%Json Path"from"
fromCountry=Extract FieldFrom%msg_body%Json Path"fromCountry"
fromCity=Extract FieldFrom%msg_body%Json Path"fromCity"
fromState=Extract FieldFrom%msg_body%Json Path"fromState"
fromZip=Extract FieldFrom%msg_body%Json Path"fromZip"
to=Extract FieldFrom%msg_body%Json Path"to"
toCountry=Extract FieldFrom%msg_body%Json Path"toCountry"
toCity=Extract FieldFrom%msg_body%Json Path"toCity"
toState=Extract FieldFrom%msg_body%Json Path"toState"
toZip=Extract FieldFrom%msg_body%Json Path"toZip"
smsStatus=Extract FieldFrom%msg_body%Json Path"smsStatus"
body=Extract FieldFrom%msg_body%Json Path"body"
numMedia=Extract FieldFrom%msg_body%Json Path"numMedia"
numSegments=Extract FieldFrom%msg_body%Json Path"numSegments"
messageSid=Extract FieldFrom%msg_body%Json Path"messageSid"
accountSid=Extract FieldFrom%msg_body%Json Path"accountSid"
apiVersion=Extract FieldFrom%msg_body%Json Path"apiVersion"
Command=To Lower Case(%body%)
Command=Extract First Word(%Command%)
// Call Automation based on the command sent
Result=CallSMS Info Request Automation(%Msg_Body%)
Result=CallSMS Call Me Automation(%Msg_Body%)
Twilio Send SMS MessageTo%from%From%MyNumber%"Invalid command. Please send either:
'info' to request your license details
'call' to request a call back."