简体   繁体   English

Bot Framework始终会触发Dialog的sames方法

[英]Bot Framework always fires sames method of Dialog

I'm playing with examples of Bot Framework and made a simple Dialog which intends to salute the user. 我正在玩Bot Framework的示例,并制作了一个简单的Dialog,旨在向用户致敬。 The issue I'm having is that after prompting for user name, the resume method never fires. 我遇到的问题是提示输入用户名后,resume方法从不触发。 It always returns to the ConverstationStartedAsync method. 它始终返回到ConverstationStartedAsync方法。 Any idea why? 知道为什么吗?

This is the dialog: 这是对话框:

    public class HelloDialog : IDialog<string>
{

    public async Task StartAsync(IDialogContext context)
    {
        context.Wait(ConversationStartedAsync);
    }

    public async Task ConversationStartedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
    {
        var message = await argument;
        PromptDialog.Text(
            context: context, 
            resume: AfterNameInput, 
            prompt: "Hi! what's your name?", 
            retry: "Sorry, I didn't get that.");

    }

    public async Task AfterNameInput(IDialogContext context, IAwaitable<string> result)
    {
        var name = await result;
        PromptDialog.Text(context, AfterAskNeed, "Hi {name}. How can I help you?", "Sorry, I didn't get that.", 3);
    }

and this is the action in the MessagesController: 这是MessagesController中的操作:

        public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {
        if (activity != null)
        {
            // one of these will have an interface and process it
            switch (activity.GetActivityType())
            {
                case ActivityTypes.Message:
                    try
                    {
                        await Conversation.SendAsync(activity, () => new HelloDialog());
                    }
                    catch(Exception ex)
                    {

                    }
                    break;

                case ActivityTypes.ConversationUpdate:
                case ActivityTypes.ContactRelationUpdate:
                case ActivityTypes.Typing:
                case ActivityTypes.DeleteUserData:
                default:
                    break;
            }
        }
        return new HttpResponseMessage(System.Net.HttpStatusCode.Accepted);
    }

我真的不知道发生了什么,但是我可以通过卸载软件包Microsoft.Bot.Builder(v3.0)然后升级到v3.3.3来解决。

This happened for me when I made a mistake in the code, then fixed and redeployed the code. 当我在代码中犯了一个错误,然后修复并重新部署了代码时,这发生在我身上。 However, because Bot Framework saves the state, you can get stuck using the old logic. 但是,由于Bot Framework会保存状态,因此您可能会因为使用旧逻辑而陷入困境。

On Facebook, you can restart clear the saved state by typing /deleteprofile, and in the emulator just by creating a new conversation or closing/reopening the emulator. 在Facebook上,您可以通过键入/ deleteprofile重新启动以清除保存的状态,而在模拟器中只需创建新对话或关闭/重新打开模拟器即可。

在此处输入图片说明 As MSDN said it ( https://docs.microsoft.com/en-us/bot-framework/dotnet/bot-builder-dotnet-manage-conversation-flow ): 正如MSDN所说( https://docs.microsoft.com/en-us/bot-framework/dotnet/bot-builder-dotnet-manage-conversation-flow ):

Dialog lifecycle 对话生命周期

When a dialog is invoked, it takes control of the conversation flow. 调用对话框时,它将控制对话流。 Every new message will be subject to processing by that dialog until it either closes or redirects to another dialog. 每个新消息将由该对话框处理,直到关闭或重定向到另一个对话框。 In C#, you can use context.Wait() to specify the callback to invoke the next time the user sends a message. 在C#中,可以使用context.Wait()来指定要在用户下次发送消息时调用的回调。 To close a dialog and remove it from the stack (thereby sending the user back to the prior dialog in the stack), use context.Done(). 要关闭对话框并将其从堆栈中删除(从而将用户发送回堆栈中的上一个对话框),请使用context.Done()。 You must end every dialog method with context.Wait(), context.Fail(), context.Done(), or some redirection directive such as context.Forward() or context.Call(). 您必须以context.Wait(),context.Fail(),context.Done()或某些重定向指令(例如context.Forward()或context.Call())结束每个对话框方法。 A dialog method that does not end with one of these will result in an error (because the framework does not know what action to take the next time the user sends a message). 不以上述任何一种结尾的对话框方法将导致错误(因为框架不知道用户下次发送消息时要采取什么措施)。

The solution is to gracefully exit the Dialog, see the code below for a complete example 解决方案是正常退出对话框,有关完整示例,请参见下面的代码

    [Serializable]
    public class RootDialog : IDialog<object>
    {
            public async Task StartAsync(IDialogContext context)
            {
                context.Wait(ConversationStartedAsync);
            }

            public async Task ConversationStartedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
            {
                var message = await argument;
                PromptDialog.Text(
                    context: context,
                    resume: AfterNameInput,
                    prompt: "Hi! what's your name?",
                    retry: "Sorry, I didn't get that.");

            }

            public async Task AfterNameInput(IDialogContext context, IAwaitable<string> result)
            {
                var name = await result;
            //Set value in the context, like holding value in ASP.NET Session
            context.PrivateConversationData.SetValue<string>("Name", name);

                PromptDialog.Choice(context, this.ResumeAfterTakingGender, new[] { "Male", "Female" }, "Please enter your gender", "I am sorry I didn't get that, try selecting one of the options below", 3);
            }

        private async Task ResumeAfterTakingGender(IDialogContext context, IAwaitable<string> result)
        {
            string gender = await result;

            string name = string.Empty;

            //Get the data from the context
            context.PrivateConversationData.TryGetValue<string>("Name", out name);

            await context.PostAsync($"Hi {name} you are a {gender}");

            //Gracefully exit the dialog, because its implementing the IDialog<object>, so use 
            context.Done<object>(null);
        }
      }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM