[英]Bot Framework: How to stop conversation after interjecting message into the message thread
我目前有一个机器人,它只是在 ms 团队中简单地发出一些提示,
用于在显示之前输出提示对话框的代码如下,这也显示了我的机器人中对话框的起点,
/* Starting point of dialogue. If no errors then we go straight to init options, which decides what the end user will see initially */
public virtual async Task StartAsync(IDialogContext context)
{
try
{
context.Wait(this.ShowInitOptions);
}
catch (Exception ex)
{
await ShowError(context, ex.Message);
}
}
public virtual async Task ShowInitOptions(IDialogContext context, IAwaitable<IMessageActivity> activity)
{
Trace.TraceInformation("ShowInitOptions()");
PromptDialog.Choice(
context: context,
resume: ChoiceReceivedAsync,
options: optionsList,
descriptions: choiceDescriptions,
prompt: "Please select an option from below.",
retry: "Please select an option from below.",
promptStyle: PromptStyle.Auto
);
}
您可以看到调用了一个 resume 方法,当您单击一个选项时,它将转到该方法。
现在在机器人代码中,我在 ApiController 类中有一个额外的 api 端点,它与默认的 api/messages 不同,在执行时将在 ms 团队中显示一个自适应卡。 此 api 端点由外部源调用,外部源提供在 ApiController 类内部处理的数据源。 然后通过在此处执行此行代码块向 ms 团队发送一条消息,
var connector = new ConnectorClient(new Uri(someUrl);
AdaptiveCardTemplate template = new AdaptiveCardTemplate(templateJson);
var myData = new
{
title = data.title,
name = data.name,
token = data.token,
reference = data.reference,
info = data.info,
newnote = data.newnote
};
string cardJson = template.Expand(myData);
// Parse the JSON
AdaptiveCardParseResult result = AdaptiveCard.FromJson(cardJson);
// Get card from result
AdaptiveCard card = result.Card;
message.Attachments.Add(new Attachment()
{
Content = card,
ContentType = AdaptiveCard.ContentType,
Name = "Card"
});
message.ChannelId = callbackInfo.channelId;
message.From = botAccount;
message.Recipient = userAccount;
message.Conversation = new ConversationAccount(id: callbackInfo.conversationId);
message.Summary = myData.name;
message.Locale = "en-uk";
MicrosoftAppCredentials.TrustServiceUrl(callbackInfo.serviceUrl);
await connector.Conversations.SendToConversationAsync((Activity)message).ConfigureAwait(false);
最后一行是将其发送到 ms 团队中的对话并显示自适应卡。 “消息”是IMessageActivity类型,包含一个自适应卡片。 这在 ms 团队中显示如下,
如您所见,此处有一个文本字段,因此在输入值并单击“确定”后,该值将提交到处理 POST 请求的 ApiController 类中的 api/messages 端点。 现在它在这里我卡住了,因为代码当前试图创建一个新对话框,如下所示, 但是,所发生的只是我在第一张图片中显示的提示再次出现。 我可以看到“活动”包含提交的数据,但我无法在对话线程中处理此信息,因为它似乎想要对之前显示的提示做出响应。 我想我的问题是有没有办法让我结束对话,这样就不需要从提示中进行选择,或者如果有人有另一个好主意,那么将不胜感激。
PS 我应该添加 Dialogs.RootDialog 类包含我需要运行的方法,因为它涉及从上下文对象收集数据,如 ShowInitOptions() 方法中所示。
您的MessagesController
类当前以相同的方式响应所有传入消息(检查是否activity.Type == ActivityTypes.Message
)。 一种可能的解决方案是首先检查消息是否是来自您的自适应卡的响应,如果是,则以不同方式处理它。 下面是一个从 Card 的提交操作中检查有效负载的示例,给您一个想法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.