Mark Robson
Mark Robson

Reputation: 1328

MS Bot framework, associate user id from auth0 with conversation state

I'm looking to associate an auth0 id with a conversation state in MS Bot but I cannot find out how to do this. Is this even possible?

const userState = new UserState(storage);
const conversationState = new ConversationState(storage);
const botInstance = new bot(conversationState, userState);

The journey should go something like:

Ideally some way to associate the conversation id to the user, as well as the auth0 user id.

Upvotes: 0

Views: 125

Answers (1)

mdrichardson
mdrichardson

Reputation: 7241

As you're using Auth0, I'm assuming that you're using Web Chat (which uses the Direct Line channel) to display the chat window, so this answer will center around that. If you're using a different channel, let me know and I'll update the answer.

Sending Auth0 User ID to the bot, with your site handling Authentication

The Web Chat REAMDE shows that you can specify a userId for the bot. Just set that as the Auth0 user id once you have it.

Having the Bot handle Authentication

There isn't a tutorial/sample that explicitly shows how to use Auth0 for authentication, but since Auth0 is an OAuth2 provider, you should be able to do this. These links should be helpful:

Conversation IDs

For the most part Conversation ID will be the same so long as the userId is the same and the user hasn't been inactive with the bot for very long. This is managed differently by each channel.

Conversation messages are only stored for 24 hours in DirectLine. Conversation metadata (such as conversation id and couple other housekeeping data) are removed after a period of inactivity (between 15 to 20 days)

Generally speaking, so long as the user resumes the conversation within 24 hours and the userID remains the same, the bot will maintain their conversation state.

There's a couple of different approaches you can use to get around this and ensure the user can resume conversation at any time (both approaches assume you pass in the same userId).

  1. Use TranscriptLoggerMiddleware to store the whole conversation

  2. Save user data and at the start of a new conversation, check to see if the data already exists for the userId. If it does, skip unnecessary dialogs and programatically attempt to resume. Basic Bot (soon to be Core Bot) does this in its dialogs by using context.next() to skip a dialog step if the user data already exists. I'd recommend this approach since if the user hasn't chatted with the bot in several days, it might be good for them to sort of "start over" without the bot also having to get all of their data again.

Upvotes: 2

Related Questions