试图在C#中结合Luis和atthement



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

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

  • https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-howto-add-media-attachments?view=azure-僵尸服务-4.0&tabs=csharp
  • https://learn.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-add-media-attachments?view=azure-僵尸服务-3.0
  • https://learn.microsoft.com/en-us/azure/bot-service/nodejs/bot-builder-nodejs-send-receive-attachments?view=azure-僵尸服务-3.0
  • 如何使用c发送microsoftbotframeworksdkv4中本地文件夹中的图像#
  • Bot是否可以从用户处接收图像作为消息或附件

在下面的代码中,我将这段代码插入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);
}

下面是我插入"如果图像,那么"的代码块

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#的一员,任何建议或意见都将不胜感激!

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

更改:

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

if (activity.Attachments != null && activity.Attachments.Count > 0)


上面的答案假设activity包含一个附件,但没有被捕获。如果activity甚至不包含附件,则说明存在其他问题。在这种情况下,请包括您的整个对话框,或者最好是您的代码/回购链接。

相关内容

  • 没有找到相关文章

最新更新