pythontwiliotwilio-apitwilio-twimltwiliosms

Twilio MessagingResponse not sending SMS inbound message


I'm currently following this tutorial from on how to send inbound messages (starts in minute 2). I've set up a webhook to call a /sms route on my Flask app, and I'm using ngrok to make the application accessible on the Internet. When I send an SMS to my Twilio number, I can successfully print the incoming request.form, including the message SID and other details. So far, the first part of the code is functioning correctly.

However, I'm facing an issue with the code towards the end, which is supposed to instantiate a MessagingResponse to send a confirmation that the message has been received. Unfortunately, this part is not working as expected. I'm not receiving any SMS message on my phone, and when I check the SMS logs page on Twilio.com while logged into my project, I don't see any Outgoing API record for the sent message. Strangely, I can see the test message I sent to our number as an Incoming message, but the confirmation response is not going through.

This is my code:

from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse

app = Flask(__name__)


@app.route('/sms', methods=["GET","POST"])
def sms():
    print(request.form)

    resp = MessagingResponse()
    resp.message(body="Subscribe")
    
    return str(resp)

if __name__ == "__main__":
    app.run(debug=True)

There is no error or exception so I can't even debug. I'm not super advanced so maybe it could be something that's right in front of me.

I would greatly appreciate any insights or help in resolving this problem and making the messaging response work as intended.

Other things I've tried so far:


Solution

  • Just in case someone in the future wants to know what fixed the problem. I was getting a XML error from twilio console debugger and the following fixed the issue:

    from flask import Flask, request, Response 
    
    return Response(str(response), mimetype="application/xml") 
    

    Source: Warning - 12200 Schema validation warning The provided XML does not conform to the Twilio Markup XML schema