Home Power PlatformsPower Automate Manage Twilio subscriptions using Power Automate

Manage Twilio subscriptions using Power Automate

by Mo Faheem
Manage Twilio subscriptions using Power Automate

With one word “Stop” or “Cancel”, the SMS message recipient can unsubscribe from receiving SMSs from a Twilio phone number. Also, the same recipient is one word away from resubscribing back. However, all the subscription is managed on Twilio and not transferred back to Dynamics 365.

Subscription is one of the actions we can process the incoming messages alongside other services that can useful. The same variable we will be discussing in this article can be used to fulfil these purposes.

Receive Twilio SMSs to Power Automate easily

In a previous post, we discussed configuring Twilio and Power Automate to receive SMS coming to a Twilio number. Please refer to this article before proceeding to this article.

The previous article ended with us passing the Twilio Webhook to a JSON variable. In the following steps, we will discuss the variable and how will we handle those variables.

Parse JSON to Select action

The Parse JSON account produces a JSON file with keys and values that I generally find difficult to deal with. Therefore, I use a Select action to reformat the JSON file so that I can easily handle it.

Select action

The schema generated by the Select action will follow the code below.

{
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "ToCountry": {
                "type": "string"
            },
            "ToState": {
                "type": "string"
            },
            "SmsMessageSid": {
                "type": "string"
            },
            "NumMedia": {
                "type": "string"
            },
            "ToCity": {
                "type": "string"
            },
            "FromZip": {
                "type": "string"
            },
            "SmsSid": {
                "type": "string"
            },
            "FromState": {
                "type": "string"
            },
            "SmsStatus": {
                "type": "string"
            },
            "FromCity": {
                "type": "string"
            },
            "Body": {
                "type": "string"
            },
            "FromCountry": {
                "type": "string"
            },
            "To": {
                "type": "string"
            },
            "ToZip": {
                "type": "string"
            },
            "NumSegments": {
                "type": "string"
            },
            "ReferralNumMedia": {
                "type": "string"
            },
            "MessageSid": {
                "type": "string"
            },
            "AccountSid": {
                "type": "string"
            },
            "From": {
                "type": "string"
            },
            "ApiVersion": {
                "type": "string"
            }
        },
        "required": []
    }
}

Now that the Select actions sort the JSON in a manageable array. You can use the below codes to access the variables of the Twilio incoming webhook.

The country where receipent mobile number is registered@{body(‘Select’)?[0]?[‘ToCountry’]}
The State where receipent mobile number is registered@{body(‘Select’)?[1]?[‘ToState’]}
The unique message ID created by Twilio’s API@{body(‘Select’)?[2]?[‘SmsMessageSid’]}
A boolean value to indicate if the SMS or MMS
NumMedia = 0 meaning SMS (no media)
NumMedia = 1 meaning MMS (media)
@{body(‘Select’)?[3]?[‘NumMedia’]}
The city where receipent the mobile number is registered@{body(‘Select’)?[4]?[‘ToCity’]}
The Zip code where receipent the mobile number is registered@{body(‘Select’)?[5]?[‘ToZip’]}
The unique message ID created by Twilio’s API @{body(‘Select’)?[6]?[‘SmsSid’]}
The State where sender mobile number is registered@{body(‘Select’)?[7]?[‘FromState’]}
The status of the the SMS (in our case, generally will indicate
that the SMS is received)
@{body(‘Select’)?[8]?[‘SmsStatus’]}
The city where the mobile number is registered@{body(‘Select’)?[9]?[‘FromCity’]}
The text of the SMS (String)@{body(‘Select’)?[10]?[‘Body’]}
The country where sender mobile number is registered@{body(‘Select’)?[11]?[‘FromCountry’]}
The recipient phone number@{body(‘Select’)?[12]?[‘To’]}
The Zip code where sender the mobile number is registered@{body(‘Select’)?[13]?[‘ToZip’]}
The number of segments that make up the complete message@{body(‘Select’)?[14]?[‘NumSegments’]}
The number of media files in the case of MMSs@{body(‘Select’)?[15]?[‘ReferralNumMedia’]}
The unique message ID created by Twilio’s API@{body(‘Select’)?[16]?[‘MessageSid’]}
The Twilio account @{body(‘Select’)?[17]?[‘AccountSid’]}
The sender phone number@{body(‘Select’)?[18]?[‘From’]}
The version of the Twilio API used@{body(‘Select’)?[19]?[‘ApiVersion’]}

To understand the different parameters generated when a message is received, please refer to Twilio help.

Managing the parameters

Generally, you can use the codes above to work with each Twilio SMS parameter. But I am rather passing these parameters to Power Automate variables. It helps manage those variables, and it is easier to call them in later actions.

parameters

Twilio subscriptions using Power Automate conditions

The subscription and unsbscription happen when the user sends a message containing a word that indicate his intention. The set of words are, as follows.

Subscription keywords

  1. Start
  2. Start

Unsubscription keywords

  1. Stop
  2. Stopall
  3. Unsubscribe
  4. Cancel
  5. End
  6. Quit
Twilio subscriptions using Power Automate conditions
Subscription keywords and Power Automate condition
Twilio unsubscriptions using Power Automate conditions
Subscription keywords and Power Automate condition

Using those words in a conditions, as shown below can enable us to direct the flow into related D365 Marketing contact records to make our changes.

It is important to know that the Twilio text message is not case sensitive and any combination of capital letter and small letter will opt-in or opt-out the sender. On the other hand, Power Automate condition is not case sensitive, therefore, I changed the text body to lower case, as in the code below.

@{toLower(body('Select')?[10]?['Body'])}

Related Articles

Leave a Comment