python twilio flask:索引超出范围

Posted

技术标签:

【中文标题】python twilio flask:索引超出范围【英文标题】:python twilio flask: index out of range 【发布时间】:2019-11-29 00:26:55 【问题描述】:

我正在设置一个 twillo SMS 机器人,并希望将收到的烧瓶响应拆分到一个列表中,并检查响应是否有 addlistremove

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)

【问题讨论】:

您可以在这里发布您的 GET/POST 请求吗?没有它,我认为任何人都无法解决问题。我认为request.args.get('body','') 不存在。 侧边栏,但您可以使用 pyngrok 从 Python 应用程序轻松地以编程方式管理 ngrok。烧瓶示例here. 【参考方案1】:

这里是 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 Query 参数,通过 GET 请求从 URL 中获取参数

request.valuesargsform 的组合。 更多信息是here in the Flask docs.

【讨论】:

以上是关于python twilio flask:索引超出范围的主要内容,如果未能解决你的问题,请参考以下文章

Twilio Flask 错误 - 不响应短信

Flask Socket-IO 服务器客户端不通信(使用 tweepy 和 twilio)

python中用find_all查找网页的属性b【0】结果显示超出范围是为啥

将 Flask 和 Ngrok 用于 http://127.0.0.1:5000 时找不到 404

Python - 索引错误 - 列表索引超出范围

在 Python 中跟踪消息的传递状态