我正在设置一个 twillo SMS 机器人,想要将 Flask 的传入响应拆分到一个列表中,并检查响应是否有 add
、 list
, 删除
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
app = Flask(__name__)
@app.route("/sms", methods=['GET', 'POST'])
#define method to split the input
def sms_ahoy_reply():
"""Respond to incoming messages with a friendly SMS."""
# Start our response
resp = MessagingResponse()
country = request.values.get('FromCountry')
toDoList = []
splitext = request.args.get('body','').split()
msgSplit = list(splitext)
if msgSplit[0] == 'add':
toDoList.append(msgSplit[1:])
elif msgSplit[0] == 'list':
resp.message(msgSplit[1:])
elif msgSplit[0] == 'remove':
deL = msgSplit[1]
if deL!=0:
del msgSplit[deL-1]
else:
del msgSplit[deL]
# Add a message
resp.message("Hi! It looks like your phone number was born in %s" %country)
return str(resp)
if __name__ == "__main__":
app.run(debug=True)
最佳答案
这里是 Twilio 开发者布道者。
您应该将 splitext = request.args.get('body','').split()
更改为 splitext = request.values.get('body','' ).split()
.
request.args
是 URL 查询字符串中的键/值对(即 http://website.com/index?arg1=hello&arg2=world )。用于 URL 查询参数,通过 GET 请求从 URL 获取参数
request.values
是 args
和 form
的组合。
更多信息为here in the Flask docs.
关于 python twilio flask : index out of range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57122119/