C# 路易斯+;单个Microsoft Bot框架中的CustomVision

C# 路易斯+;单个Microsoft Bot框架中的CustomVision,c#,.net-core,botframework,image-recognition,azure-language-understanding,C#,.net Core,Botframework,Image Recognition,Azure Language Understanding,我是C#的新手,我想制作一个机器人,将Luis services和customvision混合在一起 我希望用户能够发送一个图像或文本到机器人 我从microsoft bot框架“核心bot”和图像处理bot**(*,**)开始 目前,我确实有这段代码,它是两个示例之间的混合代码 #region References using System; using System.Collections.Generic; using System.Text; using System.Text.Regu

我是C#的新手,我想制作一个机器人,将Luis services和customvision混合在一起

我希望用户能够发送一个图像或文本到机器人

我从microsoft bot框架“核心bot”和图像处理bot**(*,**)开始

目前,我确实有这段代码,它是两个示例之间的混合代码

#region  References
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Schema;
using Newtonsoft.Json;
using Microsoft.Extensions.Configuration;
using System.Net.Http;
using System.IO;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Recognizers.Text.DataTypes.TimexExpression;
#endregion

namespace ImageProcessingBot
{
    public class ImageProcessingBot : IBot
    {
        private readonly FlightBookingRecognizer _luisRecognizer;
        protected readonly ILogger Logger;
        private readonly ImageProcessingBotAccessors _accessors;

        private readonly IConfiguration _configuration;

        private readonly DialogSet _dialogs;

        public ImageProcessingBot(ImageProcessingBotAccessors accessors, IConfiguration configuration)
        {
            _accessors = accessors ?? throw new ArgumentNullException(nameof(accessors));
            _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
            _dialogs = new DialogSet(_accessors.ConversationDialogState);
        }

        public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
        {
            Activity reply = null;
            HeroCard card ;
            StringBuilder sb;

            switch(turnContext.Activity.Type)
            {
                case ActivityTypes.ConversationUpdate:
                    foreach(var member in turnContext.Activity.MembersAdded)
                    {
                        if(member.Id != turnContext.Activity.Recipient.Id)
                        {

                            reply = await CreateReplyAsync(turnContext, "Welcome. Please select and operation");
                            await turnContext.SendActivityAsync(reply, cancellationToken:cancellationToken);
                        }
                    }
                    break;

                case ActivityTypes.Message:

                    int attachmentCount =  turnContext.Activity.Attachments != null ?  turnContext.Activity.Attachments.Count() : 0;

                    var command =  !string.IsNullOrEmpty(turnContext.Activity.Text) ? turnContext.Activity.Text : await _accessors.CommandState.GetAsync(turnContext, () => string.Empty, cancellationToken);
                    command = command.ToLowerInvariant();

                    if(attachmentCount == 0)
                    {
                        var luisResult = await _luisRecognizer.RecognizeAsync<FlightBooking>(turnContext, cancellationToken);
                            switch (luisResult.TopIntent().intent)
                            {
                                case FlightBooking.Intent.Weather:
                                    // We haven't implemented the GetWeatherDialog so we just display a TODO message.
                                    var getWeatherMessageText = "TODO: get weather flow here";
                                    var getWeatherMessage = MessageFactory.Text(getWeatherMessageText, getWeatherMessageText, InputHints.IgnoringInput);
                                    await turnContext.SendActivityAsync(getWeatherMessage, cancellationToken);
                                    var attachments = new List<Attachment>();
                                    var reply = MessageFactory.Attachment(attachments);
                                    reply.Attachments.Add(Cards.CreateAdaptiveCardAttachment());


                                default:
                                    // Catch all for unhandled intents
                                    var didntUnderstandMessageText = $"Sorry, I didn't get that. Please try asking in a different way (intent was {luisResult.TopIntent().intent})";
                                    var didntUnderstandMessage = MessageFactory.Text(didntUnderstandMessageText, didntUnderstandMessageText, InputHints.IgnoringInput);
                                    await turnContext.SendActivityAsync(didntUnderstandMessage, cancellationToken);
                                    break;
                            }


                    }
                    else
                    {


                        HttpClient client = new HttpClient();
                        Attachment attachment = turnContext.Activity.Attachments[0];

...// then it stays as in https://github.com/mandardhikari/ImageProcessingBot/blob/master/ImageProcessingBot/ImageProcessingBot/ImageProcessingBot.cs


我是c#的新手,因此如果您对以上内容有任何见解,我将不胜感激

这实际上是重复@stuartd在评论中所说的内容。 方法
MessageFactory.attachment
返回一个
IMessageActivity
,因此最好使用第二个变量,而不是
活动回复
,以避免强制转换

case FlightBooking.Intent.Weather:
 // We haven't implemented the GetWeatherDialog so we just display a TODO message.
 var getWeatherMessageText = "TODO: get weather flow here";
 var getWeatherMessage = MessageFactory.Text(getWeatherMessageText, getWeatherMessageText, InputHints.IgnoringInput);
 await turnContext.SendActivityAsync(getWeatherMessage, cancellationToken);
 var attachments = new List<Attachment>();
 meesageReply = MessageFactory.Attachment(attachments); //new variable
 messageReply.Attachments.Add(Cards.CreateAdaptiveCardAttachment());
 break;
case FlightBooking.Intent.Weather:
//我们没有实现GetWeatherDialog,所以只显示一条TODO消息。
var getWeatherMessageText=“TODO:在此处获取天气流量”;
var getWeatherMessage=MessageFactory.Text(getWeatherMessageText,getWeatherMessageText,InputHints.IgnoringInput);
等待turnContext.SendActivityAsync(getWeatherMessage,cancellationToken);
var attachments=新列表();
meesageReply=MessageFactory.Attachment(附件)//新变量
messageReply.Attachments.Add(Cards.CreateAdaptiveCardAttachment());
打破

错误消息告诉您要执行以下操作:1)为第二个
回复使用不同的变量名,2)在
案例飞行预订的末尾添加
break
语句。Intent.Weather:
code
case FlightBooking.Intent.Weather:
 // We haven't implemented the GetWeatherDialog so we just display a TODO message.
 var getWeatherMessageText = "TODO: get weather flow here";
 var getWeatherMessage = MessageFactory.Text(getWeatherMessageText, getWeatherMessageText, InputHints.IgnoringInput);
 await turnContext.SendActivityAsync(getWeatherMessage, cancellationToken);
 var attachments = new List<Attachment>();
 meesageReply = MessageFactory.Attachment(attachments); //new variable
 messageReply.Attachments.Add(Cards.CreateAdaptiveCardAttachment());
 break;