Skip to main content
Inspiring
March 13, 2019
Answered

Text to Structure

  • March 13, 2019
  • 4 replies
  • 1358 views

Kind of a newbie question.... We started using Twilio for SMS. Inbound texts are sent via an HTTP post using webhooks.  I'm a bit lost on how to convert the string I get to a structure I can work with. When I use: <cfset requestBody = toString( getHttpRequestData().content ) /> and dump requestbody to a text file I get:

ToCountry=US&ToState=MA&SmsMessageSid=SMcaba8a187004c59cf83f1b97d3ff091c&NumMedia=0&ToCity=ROXBURY&FromZip=01760  ...etc

Looks like & is the delimiter.

Thanks in advance for any suggestions.

Gary

    This topic has been closed for replies.
    Correct answer Scott_PALADEM

    So since I was going to be working with twilio soon anyways I decided to set up a quick webhook and try it. I set up a webhook that emails me dumps of the CGI, URL, and Form scopes whenever the webhook gets a request and the values from Twilio are all in the Form Scope. The following is a list of the struct keys that were in the form scope ( I removed the values because they contain my personal phone number and stuff)

    FORM - struct
    ACCOUNTSID
    APIVERSION
    BODY
    FIELDNAMES
    FROM
    FROMCITY
    FROMCOUNTRY
    FROMSTATE
    FROMZIP
    MESSAGESID
    MESSAGINGSERVICESID
    NUMMEDIA
    NUMSEGMENTS
    SMSMESSAGESID
    SMSSID
    SMSSTATUS
    TO
    TOCITY
    TOCOUNTRY
    TOSTATE
    TOZIP

    4 replies

    Scott_PALADEM
    Scott_PALADEMCorrect answer
    Participating Frequently
    March 13, 2019

    So since I was going to be working with twilio soon anyways I decided to set up a quick webhook and try it. I set up a webhook that emails me dumps of the CGI, URL, and Form scopes whenever the webhook gets a request and the values from Twilio are all in the Form Scope. The following is a list of the struct keys that were in the form scope ( I removed the values because they contain my personal phone number and stuff)

    FORM - struct
    ACCOUNTSID
    APIVERSION
    BODY
    FIELDNAMES
    FROM
    FROMCITY
    FROMCOUNTRY
    FROMSTATE
    FROMZIP
    MESSAGESID
    MESSAGINGSERVICESID
    NUMMEDIA
    NUMSEGMENTS
    SMSMESSAGESID
    SMSSID
    SMSSTATUS
    TO
    TOCITY
    TOCOUNTRY
    TOSTATE
    TOZIP
    ghanna1Author
    Inspiring
    March 13, 2019

    Thanks, Scott. I've only used webhooks with JSON. Never thought it would be a simple as form scope.

    Scott_PALADEM
    Participating Frequently
    March 13, 2019

    no problem.... twilio is a remarkably easy to use API!

    In less than an hour I sent a message to my phone  from CF and recieved a reply from my phone via a webhook to a cfm file.

    I have integrated CF code with MANY APIs over the years, and this one of the least painful.

    Scott_PALADEM
    Participating Frequently
    March 13, 2019

    Out of curiosity... have you tried just dumping out the form scope? <cfdump var="#form#>

    Based on the documentation, and what you are seeing in the request body, you may just need to access the data like #Form.ToCountry# #form.SmsMessageSid#, etc

    WolfShade
    Legend
    March 13, 2019

    If the message has been converted to use ASCII characters in place of special characters, Scott's suggestion will work.  However, if it is not converted AND if the user who sent the message places an ampersand (&) in the message, using & as the delimiter will fail.

    So, before implementing Scott's suggestion, make sure that the message the user sends is converted.

    V/r,

    ^ _ ^

    Scott_PALADEM
    Participating Frequently
    March 13, 2019

    Yes... The little bit of the string you showed appears to be URL encoded which is why I suggested that function. If it isn't for some reason, then  you will need to write a function to parse it properly based on whatever format they are using. Looking at the Twilio documentation, it seems like it should be in the url or form scope of the request, but i have not tried setting up a webhook yet... coincidentally I will be working on a Twilio integration soon though so this would be good to know...TwiML™ for Programmable SMS - Twilio

    Scott_PALADEM
    Participating Frequently
    March 13, 2019

    if it's in a query string format you could use

    https://cflib.org/udf/convertQueryStringToStruct