我正在尝试自定义CoreBot示例(https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/13.core-bot),因此它还可以接收文字之外的图像。
尽管下面有很多很好的文档和有关stackoverflow的响应,但是我对C#还是陌生的,并且很难将几段代码与C#语法结合起来。
- https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-howto-add-media-attachments?view=azure-bot-service-4.0&tabs=csharp
- https://docs.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-add-media-attachments?view=azure-bot-service-3.0
- https://docs.microsoft.com/en-us/azure/bot-service/nodejs/bot-builder-nodejs-send-receive-attachments?view=azure-bot-service-3.0
- how to send images which are in local folder in microsoft botframework sdk v4 using c#
- Can a Bot receive image as message or attachment from a user
在下面的代码中,我将这段代码插入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,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#的菜鸟,任何建议或观察将不胜感激!