user6083088
user6083088

Reputation: 1037

Error 412 - Manage Bot state Data

I've been following Bot Framework documentation to store Bot-Data but even after using the code snippet provided here for handling concurrency I still get error 412 in the emulator. Can you please tell me if I'm saving & retrieving the Bot state correctly.

I've tried setting breakpoints when I'm saving the bot state but it doesn't hit either of the catch blocks.

Emulator's screenshotenter image description here

namespace HealthBot
{
    [Serializable]
    [LuisModel("id", "password")]
    public class RootDialog : LuisDialog<object>
    {
        private const string EntityDateTime = "builtin.datetimeV2.date";
        protected override async Task MessageReceived(IDialogContext context, IAwaitable<IMessageActivity> item)
        {
            var message = await item;
            if (message.Text == null)
            {
                await Help(context, item, null);
            } 
            else
            {
                await base.MessageReceived(context, item);
            }
        }


        [LuisIntent("Claims")]
        private async Task Claims(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
        {
            StateClient stateClient = context.Activity.GetStateClient();
            BotData userData = stateClient.BotState.GetPrivateConversationData(context.Activity.ChannelId, context.Activity.Conversation.Id ,context.Activity.From.Id);
            bool isVerified = userData.GetProperty<bool>("isVerified");

            if (isVerified)
            {
                string message = "Yes, but please tell me your claim ID";
                var response = context.MakeMessage();
                response.Text = message;
                response.Speak = message;
                response.InputHint = InputHints.ExpectingInput;
                await context.PostAsync(response);
                context.Wait(MessageReceived);
            }
            else
            {
                await StartVerification(context, activity);
            }
        }

        private async Task StartVerification(IDialogContext context, IAwaitable<IMessageActivity> activity)
        {
            string message = "What is your customer ID";
            var response = context.MakeMessage();
            response.Text = message;
            response.Speak = message;
            response.InputHint = InputHints.ExpectingInput;

            await context.PostAsync(response);
            context.Wait(VerifyCustomerId);
        } 

        private async Task VerifyCustomerId(IDialogContext context, IAwaitable<IMessageActivity> activity)
        {
            var message = await activity;
            string customerID = message.Text.Replace(" ", "");
            string responseMessage = $"You provided, {customerID}";

            try
            {
                StateClient stateClient = context.Activity.GetStateClient();
                BotData userData = await stateClient.BotState.GetPrivateConversationDataAsync(context.Activity.ChannelId, context.Activity.Conversation.Id, context.Activity.From.Id, CancellationToken.None);
                userData.SetProperty<bool>("isVerified", true);
                await stateClient.BotState.SetPrivateConversationDataAsync(context.Activity.ChannelId, context.Activity.Conversation.Id, context.Activity.From.Id, userData);
            }
            catch (HttpOperationException err)
            {
                responseMessage = "Oops! something went wrong";
            }
            catch (Exception ex)
            {
                responseMessage = ex.Message;
            }
            finally
            {
                await context.PostAsync(CreateMessageWith(context, responseMessage));
                context.Wait(MessageReceived);
            }
        }

        private IMessageActivity CreateMessageWith(IDialogContext context, string response)
        {
            var reply = context.MakeMessage();
            reply.Text = response;
            reply.Speak = response;
            reply.InputHint = InputHints.ExpectingInput;
            return reply;
        }
    }
}

Upvotes: 1

Views: 183

Answers (1)

Ezequiel Jadib
Ezequiel Jadib

Reputation: 14787

In the context of a dialog, I would try using context.PrivateConversationData instead of the StateClient.

You can check the State C# sample to fully understand how this works.

Upvotes: 1

Related Questions