twilio sms status callback - not in json

Viewed 241

I made a django app that integrates with Twilio SMS and I am trying to set-up a way to get the status of an sms I send.

I have been following twilio guide on how to track delivery status on python so when I send a text I send it with the following parameters (where sms_obj is an instance in my db to keep track of the text I am sending).

def send_sms(sms_obj, to):
      client = Client(account_sid, auth_token)
      message = client.messages \
            .create(
                 body=sms_obj.body,
                 from_= phone_number,
                 to=to,
                 status_callback='https://www.ateam.productions/comunications/twilio/sms/status/{sms_id}'.format(sms_id=sms_obj.id)
             )

      return

So basically I should receive a callback with delivery statuses on this url https://www.ateam.productions/comunications/twilio/sms/status/{sms_id}, (which I do)

However, the data I get from Twilio aren't in a nice Json structure but rather a long string like the one below.

Before I start coding some weird regex to process such string, I was wondering if anyone had advices on how to handle the process.

SmsSid=SM3bceec02449e4a75a3c308e2befa6136&SmsStatus=sent&MessageStatus=sent&To=%2B19174003826&MessageSid=SM3bceec02449e4a75a3c308e2befa6136&AccountSid=////////&From=%2B19179244405&ApiVersion=2010-04-01
1 Answers

Twilio developer evangelist here.

That isn't a long string which you'd need a weird regex to process. That string is in the format application/x-www-form-urlencoded. It's the same format used to submit a form on an HTML page.

Django can already parse this type of request body, you should be able to read the contents as a dict-like object in request.POST.

For example, you should be able to get the status by calling request.POST.get('MessageStatus').

Related