Cookie CSS

Saturday, November 12, 2016

Building a Bot with Microsoft Bot Framework - Part III

Last week we took at the basic code template generated by Visual Studio when we create a new bot project.  Next week we are going to focus on user interaction using FormFlow Dialogs.  This week we focus on how to get the state of the current user, and store and retrieve information about them, and the ongoing conversation.

Before You Start

One of the key concepts to understand if your website will receive a series of messages.  These messages by default know nothing about the previous chain between the user and bot.  It is important to know how to store a state or "where we left off" so the user doesn't get stuck in a loop of repeated questions.  There are some useful properties you can use to track users, the Conversation (unique id) and the From (A user's address on a channel)  You can also use they together if you like.

So how to do you put and get information about the state of the ongoing conversation?

State Methods

The Bot State service exposes the following methods
MethodScoped toUse cases
GetUserData()UserRemembering context object with a user
GetConversationData()ConversationRemembering context object with a conversation
GetPrivateConversationData()User & ConversationRemembering context object with a person in a conversation
SetUserData()UserRemembering context object with a user
SetConversationData()ConversationRemembering context object with a conversation
SetPrivateConversationData()User & ConversationRemembering context object with a person in a conversation
DeleteStateForUser()UserWhen the user requests data be deleted or removes the bot contact
An important thing to remember is you each user conversation can have a payload of 32K, so don't try to store too much.  I recommend creating some of sort of tag for each layer of questions so you can pick up where you left off.  Example, if you ask the user for their name, or email, you should store that so each time they return you don't make then re-enter it.  You can always have a "reset" or similar command that can call the DeleteStateForUser() if in fact they do want to start over.

State Client

StateClient stateClient = activity.GetStateClient();


Setting the State

BotData userData = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id);
userData.SetProperty<bool>("SentGreeting", true);
await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);

Getting the State

BotData userData = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id);
if (userData.GetProperty<bool>("SentGreeting"))
... do something ...;

Setting a Complex Type


BotState botState = new BotState(stateClient);
BotData botData = new BotData(eTag: "*");
botData.SetProperty<BotState>("UserData", myUserData);
BotData response = await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, botData);

Getting a Complex Type

MyUserData addedUserData = new MyUserData();
BotData botData = await botState.GetUserDataAsync(activity.ChannelId, activity.From.Id);
myUserData = botData.GetProperty<MyUserData>("UserData");


So now that we know how to read and save, next week we can begin to have a conversation with someone trying to order a sandwich.  If you would like to play with a bot I designed I have added it below.




Doug Routledge, C# Lync, Skype for Business, SQL, Exchange, UC, 
Full Stack Developer  BridgeOC Bridge Operator Console
Twitter - @droutledge @ndbridge



2 comments:

  1. What is the purpose of DeleteStateForUser?

    ReplyDelete
    Replies
    1. If you had a bot asking a series of questions, and a user wanted to start over, this would clear the state you are using to store the responses.

      Delete

Any spam comments will be deleted and your user account will be disabled.