Twilio:如何根据用户以前的消息在python中用自定义消息回复用户消息?
问题描述:
我有一个android应用程序发送短信到我的twilio号码与json的内容。我有一个twiml烧瓶python应用程序,应该从android应用程序读取用户以前的短信,并且应该返回一个自定义的回复消息,指出用户在文本中所说的内容。Twilio:如何根据用户以前的消息在python中用自定义消息回复用户消息?
所以,如果用户说“圣何塞”。 twiml应用程序(托管在heroku服务器上)将回复“你想要去”< - 目标 - >“?”。所以在这种情况下,它会回答“你想去圣何塞吗?”
我能做些什么来读取用户消息并根据他的消息进行回复?
P.S.我对python和flask和twilio有点新,所以任何有关代码的答案都会有所帮助。
这里是我的代码:
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)
答
Twilio开发者在这里传播者。你似乎走在正确的道路上,而且几乎是正确的。
所有你需要做的是更改从下面的代码片段:
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!"
要:
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!"
通知我如何添加request.values.get('Body', None)
它,所以它拿起变量和在生成TwiML时使用它。让我知道这是否能解决您的问题。