Saturday, July 23, 2022

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

Issue

Also in ngrok, there appears an internal server error 500 when attempting to make a post request using twilio.

Here is the section of my code where I feel there is a problem with:

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)

I have updated all latest versions of ngrok, python, twilio and Flask. I also followed all the steps to activate the virtualenv.


Solution

Twilio developer evangelist here.

If you are using the latest version of the Twilio Python module then there is no Response method. Instead, since you are replying to a message, you need to use the MessagingResponse instead.

Try the following:

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)


Answered By - philnash
Answer Checked By - Dawn Plyler (WPSolving Volunteer)