在Python中为会话漫游器路由Twilio SMS响应

时间:2018-06-24 21:37:59

标签: python python-3.x flask sms twilio

我是一名相对较不熟悉Python编程的大学生。我一直在尝试构建一个对话式机器人,该机器人使用户可以通过Python中的SMS安排约会。

我一直在网上关注许多Python Twilio SMS教程,例如official Twilio指南。我无法弄清楚如何将Twilio SMS响应路由到相应的路由。例如,假设用户以SCHEDULERESCHEDULECHECKDROP的文本形式发送消息,而应用程序则应将它们路由到相应的路由逻辑来处理请求(并用进一步的SMS消息回复)。如果他们没有输入四个指定单词之一,则应用程序应该将用户重新路由到/inbound路由,以要求他们再次说明他们正在选择四个可接受的选项之一。在过去的几天里,我确实一直在努力工作,对routes和是否使用message = client.messages.createresponse = MessagingResponse() Twilio API感到困惑。我正在使用Flask处理Web服务器功能。

此外,您是否会偶然知道如何通过redirect路由传递参数,以便我可以在下一条路由中使用该数据并将其保存在数据库中?

非常感谢。我非常感谢我能获得的所有帮助。

from twilio.twiml.messaging_response import Body, Message, Redirect, MessagingResponse
from flask import Flask, request, session

app = Flask(__name__)


# Main function triggered by a POST request by ngrok. When an SMS is received, Twilio will send the POST.
@app.route('/inbound', methods=['POST'])
def inbound():
    """ Use Twilio API to reply to texts """
    sender = request.form['From']
    message = request.form['Body']
    recipient = request.form['To']              # Recipient Phone Number
    message_id = request.form['MessageSid']     # Twilio message id
    response = MessagingResponse()              # init a Twilio response
    formatted = message.lower().strip()         # formulate answer to message
    if "schedule" == formatted:
        reply = 'Hi, my name is Cletus. I\'d like to help you schedule your appointment today. What is your name? ' \
                'Please format \"First Last\" (ex: John Smith).'
        response.message(reply)
        response.redirect('/schedule')
        # write to db
    elif "reschedule" == formatted:
        reply = 'Hi, my name is Cletus. I\'d like to help you reschedule your appointment today. What is your name? ' \
                'Please format \"First Last\" (ex: John Smith).'
        response.message(reply)
        response.redirect('/reschedule')
    elif "appointments" == formatted:
        reply = 'Hi, my name is Cletus. I\'d like to help you check your upcoming appointment. What is your name? ' \
                'Please format \"First Last\" (ex: John Smith).'
        response.message(reply)
        response.redirect('/check')
    elif "drop" == formatted:
        reply = 'Hi, my name is Cletus. I\'d like to help you cancel your appointment. What is your name? ' \
                'Please format \"First Last\" (ex: John Smith).'
        response.message(reply)
        response.redirect('/drop')
    else:
        reply = 'Hi, my name is Cletus. How can I help you? You can text RESCHEDULE to reschedule you appointment, ' \
                'DROP to cancel your appointment, and APPOINTMENTS to receive a list of your currently scheduled ' \
                'appointments. Have a great day!'
        response.message(reply)
        response.redirect('/inbound')
    return str(response)


@app.route('/schedule', methods=['POST'])
def schedule():
    print('Finally routing to schedule')
    sender = request.form['From']
    message = request.form['Body']
    recipient = request.form['To']  # Recipient Phone Number
    message_id = request.form['MessageSid']  # Twilio message id
    response = MessagingResponse()  # init a Twilio response
    formatted = message.lower().strip()


@app.route('/reschedule', methods=['POST'])
def reschedule():
    sender = request.form['From']
    message = request.form['Body']
    recipient = request.form['To']  # Recipient Phone Number
    message_id = request.form['MessageSid']  # Twilio message id
    response = MessagingResponse()  # init a Twilio response
    formatted = message.lower().strip()


@app.route('/check', methods=['POST'])
def check():
    sender = request.form['From']
    message = request.form['Body']
    recipient = request.form['To']  # Recipient Phone Number
    message_id = request.form['MessageSid']  # Twilio message id
    response = MessagingResponse()  # init a Twilio response
    formatted = message.lower().strip()


@app.route('/drop', methods=['POST'])
def cancel():
    sender = request.form['From']
    message = request.form['Body']
    recipient = request.form['To']  # Recipient Phone Number
    message_id = request.form['MessageSid']  # Twilio message id
    response = MessagingResponse()  # init a Twilio response
    formatted = message.lower().strip()


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

0 个答案:

没有答案