错误412 - 管理机器人状态数据

时间:2017-07-27 20:28:46

标签: c# botframework

我一直在关注Bot Framework文档来存储Bot-Data,但即使使用提供here的代码片段来处理并发,我仍然会在模拟器中收到错误412。你能不能告诉我,如果我拯救和放弃正确检索Bot状态。

我在保存机器人状态时尝试设置断点,但它没有击中任何一个阻塞块。

模拟器的屏幕截图enter 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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在对话框的上下文中,我会尝试使用context.PrivateConversationData代替StateClient

您可以查看State C#示例,以完全了解其工作原理。