0

I have an android app that sends an sms message to my twilio number with content in json. I have a twiml flask python app that is supposed to read the users previous sms from the android app and its supposed to return a customized reply message stating a repeat of what the user said in the text.

So if the user said "San Jose". The twiml app(hosted on heroku server) would reply with "Do you want to go to" <--Destination--> "?". So in this case it would reply with "Do you want to go to San Jose?"

What would I do to be able to read the users message and reply based on his message?

P.S. I'm somewhat new to python and flask and twilio, so any answers with code would be helpful.

Here is my code:

from flask import Flask, request, redirect
from twilio.rest import TwilioRestClient
import twilio.twiml
import requests
import json
import httplib

app = Flask(__name__)

# Try adding your own number to this list!
callers = {
    "+14151234556": "The Hunter",
    "+14158675310": "The Best",
    "+14158675311": "Virgil",
}

@app.route("/", methods=['GET', 'POST'])
def hello_monkey():

ACCOUNT_SID = "askdfjhaksjdfklajsdlfjaslkdfjals"
    AUTH_TOKEN = "kasjdkjaSDKjaskdjkasJDkasjdkljAS"

    """Respond and greet the caller by name."""

    from_number = request.values.get('From', None)
    if from_number in callers:
        message = callers[from_number] + ", follow these directions to get to your destination: "
    else:
        message = "User, thanks for the message!"

    resp = twilio.twiml.Response()
    resp.message(message)

    return str(resp)

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

1 Answers1

0

Twilio Developer Evangelist here. You seem to be on the right path, and have it almost right.

All you need to do is change the following code snippet from:

from_number = request.values.get('From', None)
if from_number in callers:
    message = callers[from_number] + ", follow these directions to get to your destination: "
else:
    message = "User, thanks for the message!"

To:

from_number = request.values.get('From', None)
if from_number in callers:
    message = callers[from_number] + ", follow these directions to get to your destination: " + request.values.get('Body', None)
else:
    message = "User, thanks for the message!"

Notice how I've added request.values.get('Body', None) to it, so it picks up that variable and uses it when generating the TwiML. Let me know if that solves your problem.

Marcos Placona
  • 21,468
  • 11
  • 68
  • 93