机器人在Direct Line频道中是否有与PromptDialog.Choice
开始对话的正确方法?
我正在尝试抓紧拳头ConversationUpdate
活动并创建一条来自用户的虚假消息来初始化对话框,这是一个丑陋的骇客:
IMessageActivity greetingMessage = Activity.CreateMessageActivity();
greetingMessage.From = message.Recipient;//from bot
greetingMessage.Recipient = userAccount;//to user
greetingMessage.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 bot
dialogEntryMessage.From = message.From;//from user
dialogEntryMessage.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 Channel通道中,bot不会记住对话框的状态,并且当用户选择对话框选项之一并发送他的第一条真实消息时,根对话框再次从PromptDialog.Choice
开始,因此它出现两次。
更新
我从Microsoft找到了一个相关的博客文章:https://blog.botframework.com/2018/07/12/how-to-properly-send-a-greeting-message-and-common-issues-from-customers/
答案 0 :(得分:2)
在Direct Line通道bot中,它不记得对话框的状态,并且当用户选择一个对话框选项并发送他的第一条真实消息时,根对话框再次从PromptDialog.Choice开始,因此它出现了两次。
我可以重现相同的问题,并且发现将bot和用户都添加到对话中后,将执行 ConversationUpdate 处理程序。
要解决此问题,您可以参考以下代码示例。
在MessagesController中:
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}");
}
测试结果: