Reputation: 1131
I am receiving a 11200 HTTPS retrieval error from the code below. Can someone please explain me how I can resolve this? (I am hosting this application on a local server and using the ngrok https 5000 URL for the twilio API)
from flask import Flask, Response, request
from twilio import twiml
import os
from twilio.http.http_client import TwilioHttpClient
import requests
from twilio.rest import Client
app = Flask(__name__)
port = int(os.environ.get('PORT', 5000))
account_sid = "xxx"
auth_token = "xxx"
# proxy_client = TwilioHttpClient(proxy={'http': os.environ['http_proxy'], 'https': os.environ['https_proxy']})
client = Client(account_sid, auth_token)
@app.route("/")
def check_app():
# returns a simple string stating the app is working
return Response("It works!"), 200
@app.route("/twilio", methods=["POST"])
def inbound_sms():
response = twiml.Response()
# we get the SMS message from the request. we could also get the
# "To" and the "From" phone number as well
inbound_message = request.form.get("Body")
print(inbound_message)
# we can now use the incoming message text in our Python application
if inbound_message == "Hello":
response.message("Hello back to you!")
else:
response.message("Hi! Not quite sure what you meant, but okay.")
# we return back the mimetype because Twilio needs an XML response
return Response(str(response), mimetype="application/xml"), 200
if __name__ == "__main__":
app.run(debug=True)
Upvotes: 0
Views: 326
Reputation: 4837
You mixed up your Flask response and Twilio response, your code is actually raising an AttributeError
with the Twilio Python Helper Library version 6.45.1 under Python 3.6.9 because Twilio doesn't have a twiml.Response
attribute.
Change your code to the following, please note the usage of MessagingResponse
:
@app.route("/twilio", methods=["POST"])
def inbound_sms():
response = MessagingResponse()
inbound_message = request.form.get("Body")
if inbound_message == "Hello":
response.message("Hello back to you!")
else:
response.message("Hi! Not quite sure what you meant, but okay.")
return Response(str(response), mimetype="application/xml"), 200
Don't forget to add from twilio.twiml.messaging_response import MessagingResponse
to your imports. See also the example in the Twilio documentation.
I've only tested it locally but then your /twilio
endpoint returns correct TwiML when hitting it with HTTPie:
$ http --form POST http://127.0.0.1:5000/twilio Body=Hello
HTTP/1.0 200 OK
Content-Length: 96
Content-Type: application/xml; charset=utf-8
Date: Fri, 26 Feb 2021 12:15:30 GMT
Server: Werkzeug/1.0.1 Python/3.6.9
<?xml version="1.0" encoding="UTF-8"?><Response><Message>Hello back to you!</Message></Response>
Upvotes: 1