Last active
November 22, 2018 18:05
-
-
Save devinrader/ba796aa318174958c14c to your computer and use it in GitHub Desktop.
Receive and Set Session Values using Flask and KVSession
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
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
I don't see how you stored the session in the database. I'm interested in use SQLAlchemy. Would this follow the same steps?