簡體   English   中英

Bot Framework:如何在將消息插入消息線程后停止對話

[英]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 的提交操作中檢查有效負載的示例,給您一個想法:

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