简体   繁体   English

Bot Framework:如何在将消息插入消息线程后停止对话

[英]Bot Framework: How to stop conversation after interjecting message into the message thread

I currently have a bot, which simply puts out some prompts in ms teams as such,我目前有一个机器人,它只是在 ms 团队中简单地发出一些提示, 提示对话框选择

The code used to output the promptdialog before it is shown is as follows, which also shows the starting point of the dialog in my bot,用于在显示之前输出提示对话框的代码如下,这也显示了我的机器人中对话框的起点,

/* 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
                    );
}

You can see there is a resume method invoked, which when you click on a choice, it will go to this method.您可以看到调用了一个 resume 方法,当您单击一个选项时,它将转到该方法。

Now in the bot code I have an additional api endpoint in an ApiController class, which is different to the default api/messages, which when executed will display an adaptive card in ms teams.现在在机器人代码中,我在 ApiController 类中有一个额外的 api 端点,它与默认的 api/messages 不同,在执行时将在 ms 团队中显示一个自适应卡。 This api endpoint is called by an external source, which provides a data source that is processed inside of the ApiController class.此 api 端点由外部源调用,外部源提供在 ApiController 类内部处理的数据源。 A message is then sent to ms teams by executing this line block of code here,然后通过在此处执行此行代码块向 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); 

The last line is what sends it out to the conversation in ms teams and the adaptive card is displayed.最后一行是将其发送到 ms 团队中的对话并显示自适应卡。 "message" is of type IMessageActivity and contains an adaptive card. “消息”是IMessageActivity类型,包含一个自适应卡片。 This shows as follows in ms teams,这在 ms 团队中显示如下, 通知卡

As you can see there is a text field here and so upon entering a value and clicking on "OK", the value is submitted to the api/messages endpoint in my ApiController class that handles the POST request.如您所见,此处有一个文本字段,因此在输入值并单击“确定”后,该值将提交到处理 POST 请求的 ApiController 类中的 api/messages 端点。 Now it is in here where Im stuck as the code currently tries to create a new dialog as seen below,现在它在这里我卡住了,因为代码当前试图创建一个新对话框,如下所示, 发布方法 However, all that happens is that my prompts shown in the first picture show up again.但是,所发生的只是我在第一张图片中显示的提示再次出现。 I can see that "activity" contains the submitted data but Im unable to process this information within the dialog thread as it seems to want a response to the prompts, which is displayed before.我可以看到“活动”包含提交的数据,但我无法在对话线程中处理此信息,因为它似乎想要对之前显示的提示做出响应。 I guess my question is is there a way for me to end the conversation so that it doesnt require a choice to be picked from the prompts or if anyone has another bright idea then would be appreciated.我想我的问题是有没有办法让我结束对话,这样就不需要从提示中进行选择,或者如果有人有另一个好主意,那么将不胜感激。

PS I should add the Dialogs.RootDialog class contains methods I will need to run as it involves collecting data from the context object as show before in the ShowInitOptions() method. PS 我应该添加 Dialogs.RootDialog 类包含我需要运行的方法,因为它涉及从上下文对象收集数据,如 ShowInitOptions() 方法中所示。

Your MessagesController class is currently responding to all incoming messages in the same way (check if activity.Type == ActivityTypes.Message ).您的MessagesController类当前以相同的方式响应所有传入消息(检查是否activity.Type == ActivityTypes.Message )。 One possible solution is to first check if the message is a response from your Adaptive Card, and if so to process it differently.一种可能的解决方案是首先检查消息是否是来自您的自适应卡的响应,如果是,则以不同方式处理它。 Here is an example of inspecting the payload from a Card's submit action, to give you an idea:下面是一个从 Card 的提交操作中检查有效负载的示例,给您一个想法:

https://social.technet.microsoft.com/wiki/contents/articles/52224.bot-framework-how-to-read-data-on-action-buttons-of-adaptive-cards-in-a-chat-bot.aspx https://social.technet.microsoft.com/wiki/contents/articles/52224.bot-framework-how-to-read-data-on-action-buttons-of-adaptive-cards-in-a-chat-机器人.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM