【问题标题】:Trying to combine Luis and attachement in C#试图在 C# 中结合 Luis 和附件
【发布时间】:2019-11-17 18:22:02
【问题描述】:

我正在尝试自定义 CoreBot 示例 (https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/13.core-bot) ,因此它还可以接收除文本之外的图像。

虽然有很多关于 stackoverflow 的优秀文档(如下)和响应,但我是 C# 新手,很难将几段代码与 C# 语法结合起来。

在下面的代码中,我将这段代码插入到 CoreBot 中:

var activity = stepContext.Context.Activity
            var reply = activity.CreateReply();
            if (activity.Attachments != null && activity.Attachments.Any())
                {
                var messageText = stepContext.Options?.ToString() ?? "this seems to be an image an i am not yet able to understand it";
                var promptMessage = MessageFactory.Text(messageText, messageText, InputHints.ExpectingInput);
                return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = promptMessage }, cancellationToken);
            }

下面是我插入“if image, then”的代码块

private async Task<DialogTurnResult> ActStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {


             if (!_luisRecognizer.IsConfigured)
            {
                // LUIS is not configured, we just run the BookingDialog path with an empty BookingDetailsInstance.
                return await stepContext.BeginDialogAsync(nameof(BookingDialog), new BookingDetails(), cancellationToken);
            }

            var activity = stepContext.Context.Activity;
            if (activity.Attachments != null && activity.Attachments.Any())
                {
                var messageText = stepContext.Options?.ToString() ?? "this seems to be an image an i am not yet able to understand it";
                var promptMessage = MessageFactory.Text(messageText, messageText, InputHints.ExpectingInput);
                return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = promptMessage }, cancellationToken);
            }



            // Call LUIS and gather any potential booking details. (Note the TurnContext has the response to the prompt.)
            var luisResult = await _luisRecognizer.RecognizeAsync<FlightBooking>(stepContext.Context, cancellationToken);
            switch (luisResult.TopIntent().intent)
            {
                case FlightBooking.Intent.BookFlight:
                    await ShowWarningForUnsupportedCities(stepContext.Context, luisResult, cancellationToken);

                    // Initialize BookingDetails with any entities we may have found in the response.
                    var bookingDetails = new BookingDetails()
                    {
                        // Get destination and origin from the composite entities arrays.
                        Destination = luisResult.ToEntities.Airport,
                        Origin = luisResult.FromEntities.Airport,
                        TravelDate = luisResult.TravelDate,
                    };

                    // Run the BookingDialog giving it whatever details we have from the LUIS call, it will fill out the remainder.
                    return await stepContext.BeginDialogAsync(nameof(BookingDialog), bookingDetails, cancellationToken);


我还在瀑布声明中添加了AddDialog(new AttachmentPrompt(nameof(AttachmentPrompt)));,如下所示

public MainDialog(FlightBookingRecognizer luisRecognizer, BookingDialog bookingDialog, ILogger<MainDialog> logger)
                : base(nameof(MainDialog))
        {
            _luisRecognizer = luisRecognizer;
            Logger = logger;

            AddDialog(new TextPrompt(nameof(TextPrompt)));
            AddDialog(bookingDialog);
            AddDialog(new AttachmentPrompt(nameof(AttachmentPrompt)));
            AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
            {
                IntroStepAsync,
                ActStepAsync,

            }));

            // The initial child Dialog to run.
            InitialDialogId = nameof(WaterfallDialog);
        }

问题是我添加的代码段没有做任何事情。

如前所述,我是 C# 的菜鸟,任何建议或观察都将不胜感激!

【问题讨论】:

  • 尝试替换行 var activity = stepContext.Activity; with var activity = stepContext.Context.Activity;
  • 感谢@probin anand!它不再显示任何错误。我已经在问题中编辑了我的代码。但是,当用户发送附件时,机器人不会做出反应。
  • 如果您暂时不使用任何附件,则可以删除该部分代码并逐步进行
  • @probin anand,谢谢。实际上我希望机器人检测用户是否发送附件。你碰巧知道这样做的好方法吗?
  • 对不起兄弟,我没有做太多,但这个链接会帮助“github.com/microsoft/BotBuilder-Samples/tree/master/samples/…

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


【解决方案1】:

我很惊讶它甚至可以让您使用Any() 进行编译。在我的测试中,Visual Studio 抛出了构建错误。

变化:

if (activity.Attachments != null &amp;&amp; activity.Attachments.Any())

if (activity.Attachments != null &amp;&amp; activity.Attachments.Count &gt; 0)


上面的答案假设activity 包含一个附件,但没有被捕获。如果activity 甚至不包含附件,那么还有其他问题。在这种情况下,请包含您的整个对话,或者最好包含指向您的代码/存储库的链接。

【讨论】:

  • 再次感谢!我想我没有把它挤在代码中的正确位置。它有时会触发图像消息以响应文本输入
猜你喜欢
  • 2019-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-22
  • 2022-08-24
  • 1970-01-01
  • 1970-01-01
  • 2022-10-07
相关资源
最近更新 更多