0

The definitive guide to Twilio conversation tracking uses outdated code as twiml no longer has a method Response(). Is it still possible to track conversations with cookies?

from flask import Flask, request, make_response
from datetime import datetime, timedelta
from twilio import twiml

app = Flask(__name__)

@app.route("/sms")
def sms():

    #get the cookie value, or default to zero
    messagecount = int(request.cookies.get('messagecount',0))
    messagecount += 1

    twml = twiml.Response()
    twml.sms("You've sent " + str(messagecount) + " messages in this conversation so far")

    resp = make_response(str(twml))

    expires=datetime.utcnow() + timedelta(hours=4)
    resp.set_cookie('messagecount',value=str(messagecount),expires=expires.strftime('%a, %d %b %Y %H:%M:%S GMT'))

    return resp

if __name__ == "__main__":
    app.debug = True
    app.run()
kszl
  • 1,203
  • 1
  • 11
  • 18
Max D.
  • 11
  • 1
  • 4

2 Answers2

0

I got the error AttributeError: module 'twilio.twiml' has no attribute 'Response' when I try to run the code supplied by the Twilio tutorial on their blog post twilio.com/blog/2014/07/…. The solution (haven't tried, but will probably work) is to use the code example here: twilio.com/docs/guides/… – Max D. 1 min ago

Max D.
  • 11
  • 1
  • 4
0

Twilio developer evangelist here.

The blog post you are referring to there was written in 2014 and the Python library has been updated since then. It shouldn't take much to update the code from the post though, and as long as Flask's cookie handling hasn't changed either, it will all still work.

The issue is that instead of twiml.Response we now have separate twiml.MessagingResponse and twiml.VoiceResponse. Your app is for messaging, so we'll include the MessagingResponse. Then we need to update how you generate the message from:

twml = twiml.Response()
twml.sms("You've sent " + str(messagecount) + " messages in this conversation so far")

to:

twml = MessagingResponse()
twml.message("You've sent " + str(messagecount) + " messages in this conversation so far")

All together that looks like:

from flask import Flask, request, make_response
from datetime import datetime, timedelta
from twilio.twiml.messaging_response import MessagingResponse

app = Flask(__name__)

@app.route("/sms")
def sms():

    #get the cookie value, or default to zero
    messagecount = int(request.cookies.get('messagecount',0))
    messagecount += 1

    twml = MessagingResponse()
    twml.message("You've sent " + str(messagecount) + " messages in this conversation so far")

    resp = make_response(str(twml))

    expires=datetime.utcnow() + timedelta(hours=4)
    resp.set_cookie('messagecount',value=str(messagecount),expires=expires.strftime('%a, %d %b %Y %H:%M:%S GMT'))

    return resp

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

As you've pointed out, the guide to SMS conversations with cookies in the documentation is more up to date (and will be kept up to date).

Let me know if this helps.

philnash
  • 70,667
  • 10
  • 60
  • 88