2015-08-14 78 views
0

我有一個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) 

回答

0

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時使用它。讓我知道這是否能解決您的問題。

相關問題