尝试在C#

我正在尝试自定义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,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#的菜鸟,任何建议或观察将不胜感激!

zhangjiany2929 回答:尝试在C#

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

更改:

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

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


以上答案假定activity包含附件,但没有被捕获。如果activity甚至不包含附件,则说明存在其他错误。在这种情况下,请包括整个对话框,或者最好提供代码/存储库的链接。

本文链接:https://www.f2er.com/3148512.html

大家都在问