【发布时间】:2019-11-17 18:22:02
【问题描述】:
我正在尝试自定义 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, 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