Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session Handling #125

Closed
kkhzz1004 opened this issue Dec 18, 2018 · 3 comments
Closed

Session Handling #125

kkhzz1004 opened this issue Dec 18, 2018 · 3 comments
Assignees

Comments

@kkhzz1004
Copy link

kkhzz1004 commented Dec 18, 2018

🚨 The issue tracker is not for questions 🚨

If you have a question, please ask it on https://stackoverflow.com/questions/tagged/botframework

async def messages(req: web.web_request) -> web.Response:
        
    session = await get_session(req) 
    User='None'
    if 'User_code' in session:
        User = session['User_code'] 
    else:
        random_id = ''.join([random.choice(string.ascii_letters 
            + string.digits) for n in range(16)]) 
        session['User_code'] = random_id
        User = session['User_code'] 
          text = 'User_code: {}'.format(User)
    print(text)
    body = await req.json()
    activity = Activity().deserialize(body)
    auth_header = req.headers['Authorization'] if 'Authorization' in req.headers else ''
    try:
        return await ADAPTER.process_activity(activity, auth_header, request_handler)
    except Exception as e:
        raise e

def make_app():
    app = web.Application()
    # app = web.Application(middlewares=[session_middleware_factory,])
    # secret_key must be 32 url-safe base64-encoded bytes
    fernet_key = fernet.Fernet.generate_key()
    secret_key = base64.urlsafe_b64decode(fernet_key)
    setup(app, EncryptedCookieStorage(secret_key))
    app.router.add_post('/', messages)
    return app

try:
    web.run_app(make_app(), host='localhost', port=PORT)
except Exception as e:
    raise e

i'm testing on botframework emulator then session is always clear per question.
im not sure if problem is botframework , emulator or my code. what i want is if i save session name and value it must be kept. please check this issue. example result below (but value must be all same) i just used your Rich-Cards-Bot sample:

User_code: O6ZzukPijRcjY18w
User_code: 0E1nfH6m2tMuDRcW
User_code: erZyRVLMEiM2cS7I
User_code: 1lb4Kj5azHydF59K
User_code: PORfe9xn5M2beGuh
User_code: 9nZQxgocpJI32keb
User_code: GaCLP8FkyzgZOfp4
User_code: 0ykPMoFQzDC7rtap
User_code: p7EhxhMF4XFca7EL
User_code: Ys6V372dFuddG5Ol
User_code: LfIIMt05fMkldTE4

@kkhzz1004 kkhzz1004 changed the title Session Handling Question Session Handling Dec 18, 2018
@tdurnford
Copy link

tdurnford commented Dec 18, 2018

@kkhzz1004

I'm not entirely sure what you are trying to accomplish by using sessions, but I came up with a solution using User State in the BotFramework that might work for you.

Right below where the memory is initialized in the main.py file, uncomment user_state = UserState(memory). It should be on line 32 in the Rich-Cards-Bot example, but it might be further down since I believe you've imported a few packages. You also need to uncomment ADAPTER.use(user_state) on line 35.

Then in the handle_message function, you can either access the User_code from the user_state or create a new User_code in the user_state if it doesn't exist. Your code should look something like this:

# Create MemoryStorage, UserState and ConversationState
memory = MemoryStorage()
user_state = UserState(memory)
...
# Register both State  middleware on the adapter.
ADAPTER.use(user_state)
ADAPTER.use(conversation_state)

...

async def handle_message(context: TurnContext) -> web.Response:
    # Access the state of the user
    user = await user_state.get(context)

    if not hasattr(user, 'User_code'):
        user.User_code = ''.join([random.choice(string.ascii_letters + string.digits) for _ in range(16)]) 
    
    print(user.User_code)

    ...

    return web.Response(status=202)

...

async def messages(req: web.web_request) -> web.Response:
    body = await req.json()
    activity = Activity().deserialize(body)
    auth_header = req.headers['Authorization'] if 'Authorization' in req.headers else ''
    try:
        return await ADAPTER.process_activity(activity, auth_header, request_handler)
    except Exception as e:
        raise e

app = web.Application()
app.router.add_post('/', messages)

try:
    web.run_app(app, host='localhost', port=PORT)
except Exception as e:
    raise e

Using the user state, the User_code stays consistent between messages, and the output looks like this:

User_code: BGjvg9FkUCJeUoOa
User_code: BGjvg9FkUCJeUoOa
User_code: BGjvg9FkUCJeUoOa

If this solution doesn't work for what you are trying to do, please let me know and I will look deeper into the issue you are having with sessions.

@kkhzz1004
Copy link
Author

kkhzz1004 commented Dec 19, 2018

Thanks for your reply @tdurnford. simply this is what i want. so.... by using this, we could save user information right? and its for multi user that's all. Thanks alot! and i think this is what @varunreddycs asked for state data. if we have example code with this it will be better for the starter. if you don't mind i would like to ask one thing more that how can i use adaptive_card button data as an input? example i have a Card with button value yes or no. on javascript or c# botframework i could see their value can be in the chat text, or as input value. i'm waiting for you great answer :)

@tdurnford
Copy link

@kkhzz1004

Yes, you can store user information such as name and id in the user state and the data will be preserved for each user between conversations.

In regards to your second question, the Microsoft Bot Framework team prefers that how to questions be submitted on Stack Overflow. The official Bot Framework Github repos are the preferred platform for submitting bug fixes and feature requests. However, if you send me the link for your question on Stack Overflow, I will gladly take a look at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants