机器人对话未等待

时间:2018-07-03 22:26:27

标签: .net botframework

启动信息

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

问题说明

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

代码示例

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

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

然后我的“问候”对话框输出“ Hello”,并转发到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应该提示用户输入用户名,然后连接到数据库并使用该用户名获取用户:

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软件包以使用最新的稳定版本的软件包之外,我没有做任何其他特别的事情。

预期行为

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

实际结果

似乎模拟器在启动时会发送2条帖子, state.getConversationData和state.getPrivateConversationData会同时运行,并在机器人应保留输入内容的所有等待中继续运行。到达GatherUserDialog后,该程序无法停止供用户输入,并尝试使用空字符串输入linq代码。 我相信这会导致超时异常,“抱歉,我的机器人代码有问题。”

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

1 个答案:

答案 0 :(得分:2)

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

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

客户端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

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