How to store & retrieve Bot Data in Azure Table storage with directLine channel?

前端 未结 3 1765
醉梦人生
醉梦人生 2020-12-18 14:24

I\'m using Microsoft Bot Framework with directLine channel. My Bot is a part of company\'s customer portal from where I fetch some user information

3条回答
  •  隐瞒了意图╮
    2020-12-18 14:36

    The code you are using is using the deprecated default state and will not work. In order to accomplish what you would like it depends on where you are in your code. The deciding factor is if you have access to the context object or not.

    For example if you are in the MessagesController you will not have access to the context object and your code might look like this:

    public async Task Post([FromBody]Activity activity)
                {
                    if (activity.Type == ActivityTypes.Message)
                    {
    
                        var message = activity as IMessageActivity;
                        using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
                        {
                            var botDataStore = scope.Resolve>();
                            var key = Address.FromActivity(message);
    
                            var userData = await botDataStore.LoadAsync(key, BotStoreType.BotUserData, CancellationToken.None);
    
                            userData.SetProperty("key 1", "value1");
                            userData.SetProperty("key 2", "value2");
    
                            await botDataStore.SaveAsync(key, BotStoreType.BotUserData, userData, CancellationToken.None);
                            await botDataStore.FlushAsync(key, CancellationToken.None);
                        }
                        await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
                    }
                } 
    

    then to get the data:

    userData.GetProperty("key 1");
    

    The other situation would be if you did have access to the context object like in a Dialog for example, your code might look like this:

            context.UserData.SetValue("key 1", "value1");
            context.UserData.SetValue("key 2", "value2");
    

    then to get the data:

    context.UserData.GetValue("key 1");
    

提交回复
热议问题