Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 798
user8273233 Avatar asked Jan 30 '23 01:01

user8273233


2 Answers

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)
like image 156
philnash Avatar answered Feb 02 '23 10:02

philnash


This code is been using Flask for sending message

For installing use:

* 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)
like image 44
Mahendra S. Chouhan Avatar answered Feb 02 '23 09:02

Mahendra S. Chouhan