0

I am using Dialogflow to receive user input and the received values are passed to the flask program using a webhook call. Session variables are set for this action on webhook call. For another google action, I am trying to retrieve the session variables. But, I am not able to access them. Let me know where I am going wrong. Thanks in advance.

DialogFlow Intent - setclass Receives the parameter 'class' (grade) from the User and invokes the Action setclass, which sets the session variables class and grade through webhook call.

Dialogflow Intent - getclass Action getclass gets the values set in the session through webhook call.

Flask program to store and receive values from session:

from flask import Flask, request, session, jsonify

app = Flask(__name__)
app.secret_key = "abc"

@app.route("/sessiontrial",methods=['POST','GET'])
def sessiontrial():
    req = request.get_json(silent=True, force=True)
    value = req.get('queryResult')
    params = value.get('parameters') 
    action = value.get('action')
    
    print("Inside sessiontrial")

    if (action == "setclass"):
        classno = params.get('class')
        print (classno)
        session['class'] = classno
        session['grade'] = str(classno) + " Grade"
        print (session['grade'])
        res = {"fulfillmentText": classno}
        return(jsonify(res))

        
    if (action == "getclass"):      
        if 'class' in session:  
            classs = session['class']
            print("Class is: ", classs)
            gradee = session['grade']
            print("Grade is: ", gradee)
            res = {"fulfillmentText": gradee}
            return(jsonify(res))
        else:
            print("Class not found in Session")    
            res = {"fulfillmentText": "Class not found in Session"}
            return(jsonify(res))

if __name__ == '__main__':
    app.run()
Ananya A
  • 1
  • 1

1 Answers1

1

You can use Redis Session Store to solve your problem. Since you've created the session in the webbook without allowing end-users to interact with the application, this could be the culprit.

You can do the following.

  1. Go to the following link: https://github.com/aws-samples/amazon-elasticache-samples/blob/master/session-store/example-4.py

  2. Download the class example-4.py from the session store section

  3. Setup in your flask app.

  4. If you are using Heroku, you can create Heroku Redis by executing the following command:

heroku addons:create heroku-redis:hobby-dev -a your-app-name
  1. Redis Session store requires two input values: TOKEN and REDIS URL.

  2. You can get the REDIS_URL configuration variable in the settings section of Heroku app after you setup the Redis as described in the step 4.

  3. You must create a unique token for each user that remains active for as long as you want by specifying it in the session store. It is known as TTL (remaining time of key expiry in seconds). If you're using the dialogflow-fullfilment package, you can create this in the dialogflow and get it easily by using agent.session. In the example below, I've set the expiration time to one hour.

ttl=3600 
  1. Then do the following in your code snippets.
store = SessionStore(token, REDIS_URL)

if (action == "setclass"):
        classno = params.get('class')
        print (classno)
        store.set('class', classno)
        #session['class'] = classno
        store.set('grade', str(classno) + " Grade")
        #session['grade'] = str(classno) + " Grade"
        print (store.get('grade'))
        res = {"fulfillmentText": classno}
        return(jsonify(res))

        
    if (action == "getclass"):      
        if 'class' in session:  
            classs = session['class']
            print("Class is: ", classs)
            gradee = store.get('grade')
            #gradee = session['grade']
            print("Grade is: ", gradee)
            res = {"fulfillmentText": gradee}
            return(jsonify(res))
  1. If you get the data in byte format, then you can easily convert this into integers or whatever format you like. Here is an example: How could I convert a bytes to a whole hex string?
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83