botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

Where should I write code to operate with/on session variable only once per session?

Open ishandutta2007 opened this issue 9 years ago • 8 comments

What I understand session is only accessible in bot.dialog(), If I do so outside in app.js I don't have access to session variable and if it do it inside dialog() I have to do it for each and every matching dialog(), but I need to perform such session related operations only once. please let me know whats the best practice.

ishandutta2007 avatar Jan 12 '17 09:01 ishandutta2007

You might consider using middleware (since it will run for every request, and it has access to the sesssion state) e.g. sendTyping:

https://github.com/Microsoft/BotBuilder/blob/0fb918065126b1208756d2e5be0c2d00ecb55451/Node/core/src/middleware/Middleware.ts#L106

willportnoy avatar Jan 13 '17 00:01 willportnoy

Is there a function that will run the very first time the conversation is initiated? There are a couple things I'd like to do as setup the first time the conversation is initiated. What is the best way to do this?

suvamsh avatar Jan 18 '17 19:01 suvamsh

you might consider the "firstRun" middleware: https://github.com/Microsoft/BotBuilder/blob/0fb918065126b1208756d2e5be0c2d00ecb55451/Node/core/src/middleware/Middleware.ts#L74

or you can hook the conversationUpdate event (though you won't have access to the session): https://github.com/Microsoft/BotBuilder/blob/a7370bdd38b81c9414bb8c22b047f6b3f7a5c54c/Node/examples/demo-skype/app.js#L58

willportnoy avatar Jan 19 '17 21:01 willportnoy

"firstRun" does work for me but here's a scenario.

  • User gets into bad state
  • Types "reset" and dialog state is reset
  • But now it does NOT run "firstRun" middleware Is there a way to run firstRun on a "reset"? @willportnoy thanks for your help so far btw

suvamsh avatar Jan 24 '17 07:01 suvamsh

I think you'd have to modify reset to also clear the first run state in the user data bag. Generally "reset" does not clear the user data bag (I believe it clears the private conversation data bag, or perhaps just the dialog stack and state).

willportnoy avatar Jan 25 '17 17:01 willportnoy

Hi @ishandutta2007, @suvamsh, was this solved?

Andrea-Orimoto avatar Feb 14 '17 19:02 Andrea-Orimoto

@Andrea-Orimoto yup this was solved for me!

suvamsh avatar Feb 15 '17 00:02 suvamsh

Great to hear! Closing this issue, feel free to reopen if needed. Thanks!

Andrea-Orimoto avatar Feb 15 '17 01:02 Andrea-Orimoto