Bot对话框不等待 [英] Bot Dialog not waiting

查看:105
本文介绍了Bot对话框不等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • SDK平台:.NET
  • SDK版本:3.15.3
  • 有效渠道:N/A
  • 部署环境:使用模拟器进行本地开发

使用仿真器进行测试时,该程序不会等待用户在上下文上的输入,也不会等待将对话框转发到另一个对话框时

When testing with the emulator, the program does not wait for user input on the context.Wait nor when forwarding the dialog to another dialog.

连接新用户时,我的MessagesController会打招呼:

My MessagesController fires a greet when a new user is connected:

if(activity.Type == ActivityTypes.ConversationUpdate)
{
    if (activity.MembersAdded.Count == 1)
    {
        await Conversation.SendAsync(activity, () => new Dialogs.GreetDialog());
    }
}

然后,我的问候"对话框输出"Hello",并转发到GatherUserDialog:

Then my Greet Dialog outputs "Hello" and forwards to GatherUserDialog:

public async Task StartAsync(IDialogContext context)
{
       context.UserData.Clear();
       context.Wait(GatherInfoAsync);
}

private async Task GatherInfoAsync(IDialogContext context, IAwaitable<object> args)
{
     var activity = await args as Activity;

     if (!(context.UserData.ContainsKey("askedname")))
     {
           await context.PostAsync("Hello!");
           await context.Forward(new GatherUserDialog(), this.MessageReceivedAsync, activity);
     }

}

我的GatherUserDialog应该提示用户输入用户名,然后连接到db并使用该用户名获取用户:

My GatherUserDialog should prompt the user to enter a username and then connect to db and get user with said username:

Object person;

public async Task StartAsync(IDialogContext context)
{
    await context.PostAsync("May I please have your username?");
    context.UserData.SetValue("askedname", true);
    context.Wait(MessageReceivedAsync);
}

private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
    var activity = await result as Activity;
    await result;
    using (SoCoDirectoryModel model = new SoCoDirectoryModel())
    {
        var ents = model.Entities;
        var text = activity.Text;
        this.person = (from e in model.Entities
                               where e.EmployeeId == activity.Text
                               select e).SingleOrDefault();
    }
    if (this.person == null)
    {
        await context.PostAsync("Could not find that user. Please try again.");
        context.Wait(MessageReceivedAsync);
    }
    else
    {
        this.person = person as Ent;
        await context.PostAsync($"Hello {(this.person as Ent).FirstName}, what can I do for you?");
        context.Wait(MessageReceivedAsync);

    }
    context.Wait(MessageReceivedAsync);
}

```

我不确定要在这里放什么,除了上面的代码和更新NuGet软件包以使用软件包的最新稳定版本之外,我没有做任何特别的事情.

I am not sure what to put here, I didn't do anything special besides the code above and updating the NuGet packages to use the latest stable versions of packages.

预期的行为应该是问候语,然后提示输入用户名,然后传递该用户名以获取帐户信息.

Expected behavior should be a greeting followed by a prompt for a username, the username is then passed used to fetch the account info.

看来,模拟器在启动时会发送2条信息, state.getConversationData& state.getPrivateConversationData,它们同时运行,并在机器人应保持输入等待的所有等待中继续进行.到达GatherUserDialog后,该程序无法停止供用户输入,并尝试使用空字符串输入linq代码. 我相信这会导致超时异常,抱歉,我的机器人代码有问题."

It seems that the emulator sends 2 posts on startup, state.getConversationData & state.getPrivateConversationData, both running simultaneously, and continuing through any waits that the bot should hold for input. Upon reaching the GatherUserDialog, the program fails to stop for user input and tries the linq code with an empty string. I believe this causes a timeout exception and "Sorry, my bot code is having an issue."

显然我不能发布图片,所以这里是聊天室图片的链接: https://user-images.githubusercontent. com/18318261/42243397-0a373820-7ec6-11e8-99c4-5fefced6a06c.png

Apparently i cant post pictures, so here is a link to the picture of the chat: https://user-images.githubusercontent.com/18318261/42243397-0a373820-7ec6-11e8-99c4-5fefced6a06c.png

推荐答案

这是一个已知问题.当bot连接到对话时,以及当用户连接到对话时,由Direct Line连接器发送ConversationUpdate.当Direct Line连接器发送ConversationUpdate时,它不会发送正确的User.Id,因此该漫游器无法构造对话框堆栈.模拟器的行为有所不同(它立即发送bot和用户ConversationUpdate事件,并且确实发送了正确的user.id.)

This is a known issue. ConversationUpdate is sent by the Direct Line connector when the bot connects to the conversation, and again when the user connects to the conversation. When the Direct Line connector sends ConversationUpdate, it does not send the correct User.Id, so the bot cannot construct the dialog stack. The emulator is behaving differently (It sends bot and user ConversationUpdate events immediately, and it DOES send the correct user.id.)

一种解决方法是不使用ConversationUpdate,而是从客户端发送事件,然后通过对话框响应该事件.

A workaround is to not use ConversationUpdate, and send an event from the client, then respond to that event with a dialog.

客户端javascript:

Client javascript:

 var user = {
            id: 'user-id',
            name: 'user name'
        };

        var botConnection = new BotChat.DirectLine({
            token: '[DirectLineSecretHere]',
            user: user
        });

        BotChat.App({
            user: user,
            botConnection: botConnection,
            bot: { id: 'bot-id', name: 'bot name' },
            resize: 'detect'
        }, document.getElementById("bot"));

        botConnection
            .postActivity({
                from: user,
                name: 'requestWelcomeDialog',
                type: 'event',
                value: ''
            })
            .subscribe(function (id) {
                console.log('"trigger requestWelcomeDialog" sent');
            });

响应事件:

if (activity.Type == ActivityTypes.Event)
{
    var eventActivity = activity.AsEventActivity();

    if (eventActivity.Name == "requestWelcomeDialog")
    {
         await Conversation.SendAsync(activity, () => new Dialogs.GreetDialog());
    }
}

更多信息可在此处找到: https://github.com/Microsoft/BotBuilder/issues/4245#issuecomment-369311452

More information can be found here: https://github.com/Microsoft/BotBuilder/issues/4245#issuecomment-369311452

更新(关于此的博客文章): https://blog.botframework.com/2018/07/12/how-to-properly-send-a-greeting-message-and-common-issues -from-customers/

Update (blog post about this here): https://blog.botframework.com/2018/07/12/how-to-properly-send-a-greeting-message-and-common-issues-from-customers/

这篇关于Bot对话框不等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆