Skip to content

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
LastOrder=
Balance=
CustomerId=
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"
End Processing
End If
 
bodylower=To Lower Case(%body%)
Select Case%bodylower%
Case=order
Twilio Send SMS MessageTo%from%From%MyNumber%"You last order number was: %LastOrder%"
Case=balance
Twilio Send SMS MessageTo%from%From%MyNumber%"Your balance is: %Balance%"
Case Else
Twilio Send SMS MessageTo%from%From%MyNumber%"Unknown command. Send 'Order' or 'Balance'"
End Select

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%
End If
 
// Info request SMS from %from%(Show Notification)(Log)
Status=
Reply1=
CustomerName=
FirstName=
Email=
Product=
 
// 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%
End If
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%
End If
 
// Wait for the reply
Reply1(Assign Message Text)=Twilio Wait For SMS ReplyFromLast SentWait For120 Seconds
 
// Set the product depending on the response
Reply1=Extract First Word(%Reply1%)
Select Case%Reply1%
Case=1
Product=ThinkAutomation
Send EmailTo%Email%"Parker Software ThinkAutomation License Details "
Case=2
Product=WhosOn
Send EmailTo%Email%"Parker Software WhosOn License Details"
Case Else
Twilio Send SMS MessageTo%from%From%MyNumber%"Invalid reply. Please start over."
ReturnInvalid reply from %from%
End Select
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
Department=
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%
End If
 
// Call me back SMS from %from% %(Show Notification)(Log)
Status=
Duration=
HasSupport=
CustomerName=
Email=
CallStatus=
CallRecordingURL=
Reply1=
ConnectTo=
 
// 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
// Start If Block
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%
End If
 
// Ask for department
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%
End If
 
// Wait for reply
Reply1(Assign Message Text)=Twilio Wait For SMS ReplyFromLast SentWait For120 Seconds
Reply1=Extract First Word(%Reply1%)
Select Case%Reply1%
Case=1
Department=Sales
ConnectTo=+447476976405
Case=2
Department=Support
ConnectTo=+447476976406
Case Else
Twilio Send SMS MessageTo%from%From%MyNumber%"Invalid response. Please start over."
ReturnInvalid reply from %from%
End Select
 
// Connect call
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%
 
// Call completed
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%"
Else
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."
End If
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
Result=
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
Select Case%Command%
Case=info
Result=CallSMS Info Request Automation(%Msg_Body%)
Case=call
Result=CallSMS Call Me Automation(%Msg_Body%)
Case Else
Twilio Send SMS MessageTo%from%From%MyNumber%"Invalid command. Please send either: 'info' to request your license details 'call' to request a call back."
Result=Invalid command
End Select
 
Return%Result%