Marketo 2 Way SMS using Telnyx and Zapier

This post will walk you through how to build an inbound SMS responder using the Zapier automation tool and the Telnyx Zapier app to support the outbound SMS that you send from Marketo.

Achieving 2 way SMS with Marketo will help you stay in compliance with SMS regulations by appropriately responding to customer requests to be unsubscribed or requests seeking help. As you have probably seen in the SMS you have received from companies, this is done using keywords of “STOP” and “HELP” to automatically send appropriate responses.

This post will walk you through how to build an inbound SMS responder using the Zapier automation tool and the Telnyx app that will respond to these keywords and support the outbound SMS that you send from Marketo.

N.B. If you are looking for an introduction to the Telnyx CPaas platform and why they should be your SMS provider take a look at the Marketo SMS Marketing with Telnyx post where I explain the pricing and features of Telnyx SMS. Also, if you want a sneak peek at the cool things you can do once you have set up 2 way SMS in Marketo then check out the Marketo SMS Integration: Webinar Reminders post.

Before starting with the steps below make sure you have followed the Telnyx SMS Quickstart Portal Setup Guide to do the prerequisites below.

N.B. Make sure to sign up using this referral link to get a sweet, sweet $20 in free credit.

  1. Create a free Telnyx portal account
  2. Buy an SMS capable phone number
  3. Create a messaging profile
  4. Associate your SMS number with this messaging profile
  5. Obtain your API V2 authorization key

Sending Outbound SMS

Sending outbound SMS with Marketo can be achieved by using a webhook to call the Telnyx SMS API. This webhook can be called from triggered Marketo smart campaigns to build SMS into your automation workflows e.g. for webinar SMS reminders.

Responding to Inbound SMS

Once you have sent outbound SMS from within Marketo you can then use the Zapier automation tool to define the logic to automatically send follow-up SMS in response to inbound queries.

Marketo 2 way SMS using the Telnyx App in Zapier to respond to Inbound SMS
Responding to Inbound SMS using Telnyx in Zapier

1. Inbound SMS to Messaging Profile Trigger

Follow the steps below when creating your new Zap to set up a trigger for inbound SMS to one of your Telnyx messaging profiles.

  1. Select the “Telnyx” app
  2. Select the “Receive a Message” event
  3. Click “Continue”
  4. Click on the “Choose an account” dropdown
  5. Select “Connect a new account”
  6. Enter your “v2 API Key” and “v2 profile id” in the window that pops up
    1. See the Telnyx SMS Quickstart Portal Setup Guide to get these 2 parameters
  7. Click “Yes, Continue”

Now any inbound SMS to any of the numbers associated with this messaging profile will trigger this Zap to run.

2. Parse Inbound SMS and Choose Outbound Response

In order to parse the inbound SMS for certain keywords and determine the corresponding response, the Python code below will be used in the “Code by Zapier” action.

It is not necessary to specify a message for the “STOP” response in the Python code because Telnyx automatically has an auto-response for opt-out keywords including “STOP”. The SMS below is sent to the person if their SMS includes the word “STOP” and Telnyx protects this person from receiving any more SMS from the number that sent the SMS.

“You have successfully been unsubscribed, you will not receive any more messages from this number. Reply START to re-subscribe.”

Telnyx “STOP” auto-response

If someone wants to re-subscribe to SMS then the auto-response below will be sent when they reply with “START”.

“You have successfully been re-subscribed to this number. Reply STOP to unsubscribe. Msg&Data Rates May Apply.

Telnyx “START” auto response
subscription = True

if 'stop' in input['inbound_text'].lower():
    keyword = 'stop'
    subscription = False
elif 'start' in input['inbound_text'].lower():
    keyword = 'start'
elif 'help' in input['inbound_text'].lower():
    message = 'This is Telnyx Marketing. Reply STOP to unsubscribe. Msg&data rates may apply.'
    keyword = 'help'
    message = 'Thanks for reaching out. If you require assistance please visit'

return{'outbound_message':message, 'keyword':keyword, 'subscription':subscription}
"Parse Inbound SMS and Choose Outbound Response" Zapier Action
“Parse Inbound SMS and Choose Outbound Response” Zapier Action

3. Send Response using Telnyx SMS API

Selecting the “Code by Zapier” action will allow you to use Python code to send the outbound response from Step 2 to the number that texted into the messaging profile. The Telnyx app does have a “Send SMS” action (see Step 5 below), however, the “Code by Zapier” action was used so that conditional logic could be added to ensure that an SMS will not send when the keyword is “stop” or “start” since Telnyx automatically responds to these keywords (see Step 2 above).

import requests
import json

if 'stop' not in input['keyword'] and 'start' not in input['keyword']:
    url = ""
    payload = json.dumps({'from': input['from_number'] ,'to': input['to_number'],'text': input['message']})

    headers = {
      'Authorization': 'Bearer '+input['api_key'],
      'Content-Type': 'application/json'

    response = requests.request("POST", url, headers=headers, data=payload)

    return {'response':response.text}

return {'response':'skipped send'}
Marketo 2 way SMS using the "Code by Zapier" action and the Telnyx SMS API to send an SMS
Sending an SMS using the “Code by Zapier” action and the Telnyx SMS API

Inbound SMS Alerts via Email & SMS

Marketo 2 way SMS Inbound SMS alerts
Inbound SMS Alerts via Email & SMS

If you want to get Slack notifications for inbound SMS then take a look at the Forward SMS to Slack, Email, & Another Number post to see how you can implement this.

4. Email Alert for Inbound SMS

If you want to get alerted whenever there is an inbound SMS to a number on your messaging profile you can use the “Email by Zapier” action to forward details of the SMS to your email inbox. You can also include the response sent in reply to the inbound SMS (if a response is sent from Step 3) and a link to the history of the Zap for easy follow-up and debugging if necessary.

Marketo 2 way SMS email alert for inbound SMS
Email Alert for Inbound SMS

5. SMS Alert for Inbound SMS

Additionally, if you also want SMS alerts for any inbound SMS then you can use the “Send SMS” action from the Telnyx app using the account you created in Step 1. Unlike, the email alert I would not recommend including the response from Step 3 since the response is very long. There is also no need to include the “To Phone Number” since the number you will receive the alert from will be the same number that the inbound SMS was sent to.

Although using Zapier on mobile will not be the most friendly user experience, you can also include the link to the Zap’s history for quick access.

Marketo 2 way SMS forwarding inbound SMS to phone number
SMS Alert for Inbound SMS

Setting Subscription after “STOP” or “START” Response

After the appropriate SMS has been sent to the customer it may then be necessary to unsubscribe them from or resubscribe them to SMS notifications. As mentioned previously, if someone replies with “STOP” then Telnyx will automatically prevent this person from receiving further SMS, and conversely, when someone replies “START” it will re-enable SMS to this person.

While this is the case, you should still have a field in Marketo e.g. “SMS Subscription – Webinar”, for a person’s SMS subscription and turn this to

  • False for the “stop” keyword so that you are not unnecessarily sending SMS from Marketo that will be blocked by Telnyx
  • True for the “start” keyword so Marketo knows it can start sending outbound SMS to this person again
Setting Subscription in Marketo after a "stop" or "start" response
Setting Subscription in Marketo after a “stop” or “start” response

6. Only Continue if Keyword is Stop or Start

This is a simple filter that will only allow the Zap to continue if the keyword variable from Step 2 contains the word “stop” or “start”.

Zapier filter to only allow progression if the keyword field contains stop or start
Zapier keyword filter

7. Get Marketo Access Token

A “Code by Zapier” action is used with the Python code below in order to get the access token necessary to do the lookup in Step 8. If you need help finding your Marketo REST API authentication credentials then look at the authentication section in the Marketo REST API docs.

import requests
import re

url = input['base_url'] + '/identity/oauth/token?grant_type=client_credentials&client_id='+ input['client_id'] +'&client_secret='+ input['client_secret']

response = requests.get(url)

token ='access_token":"(.*)","token_type"', response.text).group(1)

return {'token': token}

8. Lookup Email using Phone Number

The “Create or Update Lead in Marketo” action used in Step 9. can only use the person’s email address to do the lookup. Consequently, that means we need to match the phone number of the person who replied to their email address in Marketo.

The Marketo REST API offers the option to query leads using fields and their values as filters. Note that it is necessary to URL encode the lookup phone number for the request to go through successfully. Additionally, in the case that multiple leads share the same phone number the findall function is used to get the returned information for all matching leads and by default the email of the first matching lead will be returned by this code.

import requests
import json
import re
import urllib.parse

url = input['base_url'] + '/rest/v1/leads.json?filterType='+input['field_api_name']+'&filterValues='+urllib.parse.quote(input['lookup_value'])

headers = {
  'Authorization': 'Bearer '+input['token']

response = requests.request("GET", url, headers=headers)

result ='"result":\[(.*)\]',response.text).group(1)
ids = re.findall('{.*?}',result)
id0 = json.loads(ids[0])
email = id0['email']

Zapier action to lookup a lead's email using their phone number
“Lookup Email using Phone Number” Zapier Action

Transferring Phone Field Value to a String Field

One thing to note though is that fields with the type of “Phone” cannot be used in the lookup request. Therefore, a workaround needs to be implemented to transfer the value from the phone type field to a string type field that can then be used in the lookup request.

This is achieved using a Marketo smart campaign that triggers anytime the phone field (“Form – Marketing Phone” in this case) changes and then transfers the value of this phone field to a string field (“Form – Marketing Phone Lookup”) using the lead token. This string field is then the one that should be referenced in the lookup request above.

Transfer Phone Number Smart List
Transfer Phone Number Smart List
Transfer Phone Number Flow Action
Transfer Phone Number Flow Action

N.B. Notice how the default “Phone” field, which maps to the “Phone” field on the lead and contact in Salesforce, is not being used here. The reason for creating a separate phone field is to ensure that only marketing will have access to this phone number and that sales members cannot reach out to leads using this phone.

9. Create or Update Lead in Marketo

The email address obtained from Step 8. can now be used in the “Create or Update Lead in Marketo” action to identify the person and change their SMS subscription field e.g. “SMS Subscription – Webinar”, to true/false using the subscription value returned from Step 2.

Using 2 Way SMS with Marketo

Now that your 2 way SMS mechanism for Marketo is firing on all cylinders take a look at how you can use SMS communications for webinar reminders in the Marketo SMS Integration: Webinar Reminders post.

I encourage you to make the zap above your own. The exact same logic used in Step 2 above can be used to build auto-responders for any marketing campaign. For example:

  • If SMS contains “Yes” then send X
  • If SMS contains “No” then send Y
  • Else send Z

So go wild!

Automating with APIs, Python, Zapier, and Google Scripts 

Former engineer turned marketing operations lead who loves to get down and dirty with the Marketo API, Python, Zapier, and Google Scripts.

Post a comment