Skip to content

Instantly share code, notes, and snippets.

@devinrader
Last active November 22, 2018 18:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devinrader/ba796aa318174958c14c to your computer and use it in GitHub Desktop.
Save devinrader/ba796aa318174958c14c to your computer and use it in GitHub Desktop.
Receive and Set Session Values using Flask and KVSession
from flask import Flask, request, make_response, session
from flaskext.kvsession import KVSessionExtension
from datetime import datetime, timedelta
from twilio import twiml
import json
import redis
from simplekv.memory.redisstore import RedisStore
SECRET_KEY = 'a secret key'
#Because this is new code, lets use the preferred RedisStrict implementation
store = RedisStore(redis.StrictRedis(host = '127.0.0.1', port = 6379, db = 0))
app = Flask(__name__)
app.config.from_object(__name__)
KVSessionExtension(store, app)
@app.route("/sms")
def converse():
messagecount = int(session.get('messagecount',0))
messagecount += 1
username = session.get('username', locateUsername( request.args.get('From') ) )
session['messagecount'] = str(messagecount)
session['username'] = username
twml = twiml.Response()
twml.sms("Hello " + username + ". You've sent " + str(messagecount) + " messages in this conversation so far")
resp = make_response(str(twml))
return resp
if __name__ == "__main__":
app.debug = True
app.run()
@joshlsullivan
Copy link

I don't see how you stored the session in the database. I'm interested in use SQLAlchemy. Would this follow the same steps?

@BinarySo1o
Copy link

I couldn't get this to work until I changed "from flaskext.kvsession" to "from flask.ext.kvsession", just a note. Probably something that has changed since this was published originally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment