samuelli97
samuelli97

Reputation: 71

Twilio MessagingResponse() after Flask redirect

As in the Twilio Docs, I can make a MessagingResponse() object for sending a response text to my phone. However, what if I want to first redirect to an internal page? I am not able to simply make a MessagingResponse() in the redirected function, like I want to:

@app.route("/news", methods=['GET', 'POST'])
def news():
    ....
    resp = MessagingResponse()
    resp.message(msg)
    return ""

@app.route("/", methods=['GET', 'POST'])
def hello():
    return redirect("news")

I am not getting a text back if I use the above code. However, if I manually create a Client from twilio.rest as follows:

@app.route("/news", methods=['GET', 'POST'])
def news():
    ....
    client = Client(account_sid, auth_token)
    message = client.messages.create(to=request.values.get("From"), from_=request.values.get("To"),
                                         body=msg)

I successfully do what I want to do. I don't understand why MessagingResponse() doesn't work when the request object is the same.

Upvotes: 1

Views: 1249

Answers (1)

Alex Baban
Alex Baban

Reputation: 11702

Your first snippet is how you respond to incoming text messages with XML or TwiML (one thing)
from twilio.twiml.messaging_response import MessagingResponse

while your second snippet is how you send text messages via REST API (another thing)
from twilio.rest import Client

Looking at your first snippet, if you're responding with an empty string to incoming text messages, you will not get a text back (Twilio will not do anything), as such:

instead of

return ""

try

return str(resp)

and make sure msg is not empty.

Upvotes: 1

Related Questions