I have a messenger bot that asks questions in a sequential way. To keep track of the questions I'm using a counter which is a session variable.But when I increment the session[counter'] it doesn't change.
Here's the logic. Even if this code works, I'm not sure why when I implement the same logic for my bot, it doesn't work.
question_dict = {
'Locq0':[{'question':"Some question 0",'options':(["Continue","@continue"])}],
'Locq1':[{'question':"Some question 1",'options':(["Continue","@continue"])}],
'Locq2':[{'question':"Some question 2",'options':(["Continue","@continue"])}],
}
def main():
session['counter'] = 0
if user_payload == "@continue":
increment()
def increment():
if session['counter'] <= len(question_dict):
session['counter'] += 1
quiz(session['counter'])
def quiz(counter):
question = question_dict['Locq{}'.format(counter)][0]['question']
option = question_dict['Locq{}'.format(counter)][0]['options']
# display the question and option
print(question)
print(option)
Back to the bot. When the user uses the bot for the first time, a session variable is created session['counter']
and it's initialized to 0. Then the bot starts asking questions, to move to the next question it should increment session['counter'] +=1
. But that doesn't happen, session['counter']
is always 0, even though the increment() function is called.
Am I doing something wrong? Or is there a proper way to do it? This app is served using Flask.
I think it's because when the main method is called, it is throwing an error. try initializing the dictionary first and then increment it.
the following code throws an error
def main():
session['counter'] = 0
if user_payload == "@continue":
increment()
try the following code
def main():
session = {'counter':0}
if user_payload == "@continue":
increment()