【问题标题】:How to call carousel in my luis dialog如何在我的 luis 对话框中调用轮播
【发布时间】:2024-04-16 08:25:02
【问题描述】:

我想调用一个对话框类(我在其中添加了轮播)。

这是我的代码

[LuisIntent("Greeting")]
public async Task Greeting(IDialogContext context, LuisResult result)
{
    context.Call(new GuesserBot, this.ResumeAfterOptionDialog);
}

轮播在新页面中:

using System;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using System.Text;
using System.Collections.Generic;

namespace project.Dialogs
{
public class GuesserBot
{
    public class GuesserDialog : IDialog<object>
    {
        string strBaseURL;
        protected int intNumberToGuess;
        protected int intAttempts;

        #region public async Task StartAsync(IDialogContext context)
        public async Task StartAsync(IDialogContext context)
        {
            // Generate a random number
            Random random = new Random();
            this.intNumberToGuess = random.Next(1, 6);

            // Set Attempts
            this.intAttempts = 1;

            // Start the Game
            context.Wait(MessageReceivedAsync);
        }
        #endregion

        public virtual async Task MessageReceivedAsync(
            IDialogContext context,
            IAwaitable<IMessageActivity> argument)
        {
            // Set BaseURL
            context.UserData.TryGetValue<string>(
                "CurrentBaseURL", out strBaseURL);

            int intGuessedNumber;

            // Get the text passed
            var message = await argument;

            // See if a number was passed
            if (!int.TryParse(message.Text, out intGuessedNumber))
            {
                // A number was not passed  

                // Create a reply Activity
                Activity replyToConversation = (Activity)context.MakeMessage();
                replyToConversation.Recipient = replyToConversation.Recipient;
                replyToConversation.Type = "message";

                string strNumberGuesserCard =
                    String.Format(@"{0}/{1}",
                    strBaseURL,
                    "Images/NumberGuesserCard.png");

                List<CardImage> cardImages = new List<CardImage>();
                cardImages.Add(new CardImage(url: strNumberGuesserCard));

                // Create the Buttons
                // Call the CreateButtons utility method
                List<CardAction> cardButtons = CreateButtons();

                // Create the Hero Card
                // Set the image and the buttons
                HeroCard plCard = new HeroCard()
                {
                    Images = cardImages,
                    Buttons = cardButtons,
                };

                // Create an Attachment by calling the
                // ToAttachment() method of the Hero Card                
                Attachment plAttachment = plCard.ToAttachment();
                // Attach the Attachment to the reply
                replyToConversation.Attachments.Add(plAttachment);
                // set the AttachmentLayout as 'list'
                replyToConversation.AttachmentLayout = "list";

                // Send the reply
                await context.PostAsync(replyToConversation);
                context.Wait(MessageReceivedAsync);
            }

            // This code will run when the user has entered a number
            if (int.TryParse(message.Text, out intGuessedNumber))
            {
                // A number was passed
                // See if it was the correct number
                if (intGuessedNumber != this.intNumberToGuess)
                {
                    // The number was not correct
                    this.intAttempts++;

                    // Create a response
                    // This time call the ** ShowButtons ** method
                    Activity replyToConversation =
                        ShowButtons(context, "Not correct. Guess again.");

                    await context.PostAsync(replyToConversation);
                    context.Wait(MessageReceivedAsync);
                }
                else
                {
                    // Game completed
                    StringBuilder sb = new StringBuilder();
                    sb.Append("Congratulations! ");
                    sb.Append("The number to guess was {0}. ");
                    sb.Append("You needed {1} attempts. ");
                    sb.Append("Would you like to play again?");

                    string CongratulationsStringPrompt =
                        string.Format(sb.ToString(),
                        this.intNumberToGuess,
                        this.intAttempts);

                    // Put PromptDialog here
                    PromptDialog.Confirm(
                        context,
                        PlayAgainAsync,
                        CongratulationsStringPrompt,
                        "Didn't get that!");
                }
            }
        }

        private async Task PlayAgainAsync(IDialogContext context, IAwaitable<bool> result)
        {
            // Generate new random number
            Random random = new Random();
            this.intNumberToGuess = random.Next(1, 6);

            // Reset attempts
            this.intAttempts = 1;

            // Get the response from the user
            var confirm = await result;

            if (confirm) // They said yes
            {
                // Start a new Game
                // Create a response
                // This time call the ** ShowButtons ** method
                Activity replyToConversation =
                    ShowButtons(context, "Hi Welcome! - Guess a number between 1 and 5");
                await context.PostAsync(replyToConversation);
                context.Wait(MessageReceivedAsync);
            }
            else // They said no
            {
                await context.PostAsync("Goodbye!");
                context.Wait(MessageReceivedAsync);
            }
        }

        // Utility

        #region private static List<CardAction> CreateButtons()
        private static List<CardAction> CreateButtons()
        {
            // Create 5 CardAction buttons 
            // and return to the calling method 
            List<CardAction> cardButtons = new List<CardAction>();
            for (int i = 1; i < 6; i++)
            {
                string CurrentNumber = Convert.ToString(i);
                CardAction CardButton = new CardAction()
                {
                    Type = "imBack",
                    Title = CurrentNumber,
                    Value = CurrentNumber
                };

                cardButtons.Add(CardButton);
            }

            return cardButtons;
        }
        #endregion

        #region private static Activity ShowButtons(IDialogContext context, string strText)
        private static Activity ShowButtons(IDialogContext context, string strText)
        {
            // Create a reply Activity
            Activity replyToConversation = (Activity)context.MakeMessage();
            replyToConversation.Text = strText;
            replyToConversation.Recipient = replyToConversation.Recipient;
            replyToConversation.Type = "message";

            // Call the CreateButtons utility method 
            // that will create 5 buttons to put on the Here Card
            List<CardAction> cardButtons = CreateButtons();

            // Create a Hero Card and add the buttons 
            HeroCard plCard = new HeroCard()
            {
                Buttons = cardButtons
            };

            // Create an Attachment
            // set the AttachmentLayout as 'list'
            Attachment plAttachment = plCard.ToAttachment();
            replyToConversation.Attachments.Add(plAttachment);
            replyToConversation.AttachmentLayout = "list";

            // Return the reply to the calling method
            return replyToConversation;
        }
        #endregion
    }
}
}

我收到了这个错误:

错误 CS0411:无法从用法推断方法“IDialogStack.Call(IDialog, ResumeAfter)”的类型参数。尝试明确指定类型参数。

ResumeAfterOption:

 private async Task ResumeAfterOptionDialog(IDialogContext context,          IAwaitable<object> result)
    {
        await context.PostAsync("Do you want something else!");
    }

你能帮我在调用一个意图时如何显示轮播吗?

我可以在我有 luis 对话意图的同一类中添加轮播代码吗?

【问题讨论】:

  • 请显示 ResumeAfterOptionDialog 的代码。此外,您在新的 GuesserBot 之后缺少一个 ()...
  • @EzequielJadib 我添加了 ResumeAfterOption ..在我的代码中是好的 GuesserBot() ..但我仍然收到错误
  • 您的代码有 2 个嵌套类。 GuesserBot 和 GuesserDialog.. 你应该调用 GuesserDialog...

标签: c# bots botframework azure-language-understanding


【解决方案1】:

问题是您试图调用不是对话框的东西,因此编译器无法推断类型。通过查看您的代码,您似乎正在尝试调用GuesserBot(这不是一个对话框),但是您应该调用GuesserDialog

[LuisIntent("Greeting")]
public async Task Greeting(IDialogContext context, LuisResult result)
{
    context.Call(new GuesserDialog(), this.ResumeAfterOptionDialog);
}

顺便说一句,我注意到您的GuesserDialog 不可序列化,因此请确保将[Serializable] 属性添加到它;否则以后会失败。

关于您的第二个问题,是的,您可以在基于 LuisDialog 的类中添加轮播代码。

【讨论】:

  • 你能给我一些代码吗?我该怎么做?@ezequiel
  • 我添加了 [Serializable] 但仍未修复
  • 关于什么的代码?什么是不固定的?您是否更改了 context.Call 行?你打电话给 GuesserBot,这是错误的。
  • context.Call(new GuesserBot(), this.ResumeAfterOptionDialog);
  • [Serializable] 公共类 GuesserBot { 公共类 GuesserDialog : IDialog {
最近更新 更多