pythonflasktwiliopythonanywhere

Twilio invalid StatusCallback URL using python and flask


I'm trying to send an sms message to the caller in Twilio using a python and flask webhook. My python webhook is hosted on pythonanywhere.com.

A snippet of the code is shown below. I haven't been able to find any details on the internet on why I keep getting an invalid StatusCallback URL error. All of the other routes seem to work fine.

def twiml(resp: VoiceResponse) -> flask.Response:
    resp = flask.Response(resp)
    resp.headers['Content-Type'] = 'text/xml'
    return resp


@app.route('/send-message', methods=['POST'])
def send_message():
    response = VoiceResponse()

    msg = 'a text message'

    response.sms(msg,
                 status_callback=url_for('sms_callback', source=request.endpoint),
                 status_callback_method='POST',
                 to=request.values.get('From'),
                 from_=request.values.get('To')
                 )

    return twiml(response)


@app.route('/sms-callback', methods=['POST'])
def sms_callback():
    response = VoiceResponse()

    response.say('message callback')

    return twiml(response)



Solution

  • Of course I found out the answer after I post the question. Hopefully this helps someone else!

    The status_callback parameter must be the full URL link and not just the relative link, since it is being passed to the Twilio servers. They don't know how to handle the relative link, so it's invalid.

    I passed the full link as shown below and it works.

        response.sms(msg,
                 status_callback='https://myaccount.pythonanywhere.com/sms-callback',
                 status_callback_method='POST',
                 to=request.values.get('From'),
                 from_=request.values.get('To')
                 )