Why do I get the error AttributeError: 'module' object has no attribute 'Response' in my SMS app that interfaces with Twilio?

Why do I get the error AttributeError: 'module' object has no attribute 'Response' in my SMS app that interfaces with Twilio?

同样在 ngrok 中,尝试使用 twilio 发出 post 请求时出现内部服务器错误 500。

这是我觉得有问题的代码部分:

from flask import Flask, request
from twilio import twiml
import wolframalpha
import wikipedia

app = Flask(__name__)

wolf = wolframalpha.Client(wolfram_app_id)


@app.route('/', methods=['POST'])
def sms():

    message_body = request.form['Body']
    resp = twiml.Response()

    replyText = getReply(message_body) 
    resp.message('Hi\n\n' + replyText )
    return str(resp)

我已经更新了所有最新版本的 ngrok、python、twilio 和 Flask。 我也按照所有步骤激活了virtualenv。

此处为 Twilio 开发人员布道师。

如果您使用的是最新版本的Twilio Python module,则没有Response方法。相反,由于您正在回复消息,因此您需要改用 MessagingResponse

尝试以下操作:

from flask import Flask, request
from twilio.twiml.messaging_response import Message, MessagingResponse
import wolframalpha
import wikipedia

app = Flask(__name__)

wolf = wolframalpha.Client(wolfram_app_id)


@app.route('/', methods=['POST'])
def sms():

    message_body = request.form['Body']
    resp = MessagingResponse()

    replyText = getReply(message_body) 
    resp.message('Hi\n\n' + replyText )
    return str(resp)

此代码一直在使用 Flask 发送消息

安装使用:

* pip install flask
* pip install twilio

from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse

app = Flask(__name__)

@app.route("/sms", methods =['POST'])
def sms():
    number = request.form['From']
    message_body = request.form['Body']

    resp = MessagingResponse()
    response_message = 'Hello {}, You said:{}'.format(number, message_body)
    resp.message(response_message)

    return str(resp)

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