为了账号安全,请及时绑定邮箱和手机立即绑定

使用 MS BotFramework 中的 PromptDialog 开始对话

使用 MS BotFramework 中的 PromptDialog 开始对话

C#
茅侃侃 2021-11-14 14:36:25
是否有适当的方式让机器人PromptDialog.Choice在 Direct Line 频道中开始对话?我正在尝试通过捕获拳头ConversationUpdate活动并从用户创建假消息来初始化对话框的丑陋技巧,如下所示:IMessageActivity greetingMessage = Activity.CreateMessageActivity();greetingMessage.From = message.Recipient;//from botgreetingMessage.Recipient = userAccount;//to usergreetingMessage.Conversation = message.Conversation;greetingMessage.Text = "Hello, I am a bot";greetingMessage.Locale = "en-us";greetingMessage.Id = Guid.NewGuid().ToString();await connector.Conversations.SendToConversationAsync((Activity)greetingMessage);IMessageActivity dialogEntryMessage = Activity.CreateMessageActivity();dialogEntryMessage.Recipient = message.Recipient;//to botdialogEntryMessage.From = message.From;//from userdialogEntryMessage.Conversation = message.Conversation;dialogEntryMessage.Text = "any text";dialogEntryMessage.Locale = "en-us";dialogEntryMessage.ChannelId = message.ChannelId;dialogEntryMessage.ServiceUrl = message.ServiceUrl;dialogEntryMessage.Id = Guid.NewGuid().ToString();dialogEntryMessage.ReplyToId = greetingMessage.Id;await Conversation.SendAsync(dialogEntryMessage, () => new Dialogs.RootDialog());哪里message来的ConversationUpdate消息。在 RootDialog 中,我以PromptDialog.Choice.它在模拟器中工作,但在 Direct Line 通道机器人中不记得对话状态,当用户选择一个对话选项并发送他的第一条真实消息时,根对话再次从 开始PromptDialog.Choice,因此它出现两次。更新我从微软找到了一篇相关的博文:https ://blog.botframework.com/2018/07/12/how-to-properly-send-a-greeting-message-and-common-issues-from-customers/
查看完整描述

1 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

在 Direct Line 通道机器人不记得对话状态,当用户选择一个对话选项并发送他的第一条真实消息时,根对话框再次从 PromptDialog.Choice 开始,所以它出现了两次。


我可以在我这边重现相同的问题,并且我发现将 bot 和用户添加到对话时将执行ConversationUpdate处理程序。


为了解决这个问题,您可以参考以下代码示例。


在消息控制器中:


else if (message.Type == ActivityTypes.ConversationUpdate)

{

    // Handle conversation state changes, like members being added and removed

    // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info

    // Not available in all channels


    if (update.MembersAdded != null && update.MembersAdded.Any())

    {

        foreach (var newMember in update.MembersAdded)

        {

            if (newMember.Name == "{your_botid_here}")

            {

                IMessageActivity greetingMessage = Activity.CreateMessageActivity();


                //...

                //your code logic

                //...


                IMessageActivity dialogEntryMessage = Activity.CreateMessageActivity();

                dialogEntryMessage.Recipient = message.Recipient;//to bot

                dialogEntryMessage.From = message.From;//from user

                dialogEntryMessage.Conversation = message.Conversation;

                dialogEntryMessage.Text = "show choices";

                dialogEntryMessage.Locale = "en-us";

                dialogEntryMessage.ChannelId = message.ChannelId;

                dialogEntryMessage.ServiceUrl = message.ServiceUrl;

                dialogEntryMessage.Id = System.Guid.NewGuid().ToString();

                dialogEntryMessage.ReplyToId = greetingMessage.Id;


                await Conversation.SendAsync(dialogEntryMessage, () => new Dialogs.RootDialog());

            }

        }

    }

}

在 RootDialog 中:


private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)

{

    var activity = await result as Activity;


    var mes = activity.Text.ToLower();


    string[] choices = new string[] { "choice 1", "choice 2" };


    if (Array.IndexOf(choices, mes) > -1)

    {

        await context.PostAsync($"You selected {mes}");

    }

    else if(mes == "show choices")

    {

        PromptDialog.Choice(context, resumeAfterPrompt, choices, "please choose an option.");

    }

    else

    {

        await context.PostAsync($"You sent {activity.Text} which was {length} characters.");

        context.Wait(MessageReceivedAsync);

    }


}


private async Task resumeAfterPrompt(IDialogContext context, IAwaitable<string> result)

{

    string choice = await result;


    await context.PostAsync($"You selected {choice}");

}

测试结果:

//img1.sycdn.imooc.com//6190aeaa000165d605420630.jpg

查看完整回答
反对 回复 2021-11-14
  • 1 回答
  • 0 关注
  • 132 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信