示例#1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BotServices"/> class.
        /// </summary>
        /// <param name="botConfiguration">Parsed .bot configuration file.</param>
        public BotServices(BotConfiguration botConfiguration)
        {
            foreach (var service in botConfiguration.Services)
            {
                switch (service.Type)
                {
                case ServiceTypes.Endpoint:
                {
                    var endpoint = (EndpointService)service;
                    EndpointServices.Add(endpoint.Name, endpoint);

                    break;
                }

                case ServiceTypes.BlobStorage:
                {
                    // Create a Storage client.
                    var storage = (BlobStorageService)service;

                    if (string.IsNullOrWhiteSpace(storage.ConnectionString))
                    {
                        throw new InvalidOperationException("The Storage ConnectionString ('connectionString') is required. Please update your '.bot' file.");
                    }

                    var storageAccount = CloudStorageAccount.Parse(storage.ConnectionString);
                    StorageServices.Add(storage.Name, storageAccount);

                    break;
                }

                case ServiceTypes.QnA:
                {
                    // Create a QnA Maker that is initialized and suitable for passing
                    // into the IBot-derived class (QnABot).
                    var qna = (QnAMakerService)service;

                    if (string.IsNullOrWhiteSpace(qna.KbId))
                    {
                        throw new InvalidOperationException("The QnA KnowledgeBaseId ('kbId') is required. Please update your '.bot' file.");
                    }

                    if (string.IsNullOrWhiteSpace(qna.EndpointKey))
                    {
                        throw new InvalidOperationException("The QnA EndpointKey ('endpointKey') is required. Please update your '.bot' file.");
                    }

                    if (string.IsNullOrWhiteSpace(qna.Hostname))
                    {
                        throw new InvalidOperationException("The QnA Host ('hostname') is required. Please update your '.bot' file.");
                    }

                    var qnaEndpoint = new QnAMakerEndpoint()
                    {
                        KnowledgeBaseId = qna.KbId,
                        EndpointKey     = qna.EndpointKey,
                        Host            = qna.Hostname,
                    };

                    var qnaMaker = new QnAMaker(qnaEndpoint);
                    QnAServices.Add(qna.Name, qnaMaker);

                    break;
                }

                case ServiceTypes.Luis:
                {
                    var luis = (LuisService)service;

                    if (string.IsNullOrWhiteSpace(luis.AppId))
                    {
                        throw new InvalidOperationException("The LUIS AppId ('appId') is required. Please update your '.bot' file.");
                    }

                    if (string.IsNullOrWhiteSpace(luis.AuthoringKey))
                    {
                        throw new InvalidOperationException("The LUIS AuthoringKey ('authoringKey') is required. Please update your '.bot' file.");
                    }

                    if (string.IsNullOrWhiteSpace(luis.Region))
                    {
                        throw new InvalidOperationException("The LUIS Region ('region') is required. Please update your '.bot' file.");
                    }

                    var app        = new LuisApplication(luis.AppId, luis.AuthoringKey, luis.GetEndpoint());
                    var recognizer = new LuisRecognizer(app);
                    LuisServices.Add(luis.Name, recognizer);

                    break;
                }

                case ServiceTypes.Generic:
                {
                    var genericService = (GenericService)service;
                    if (genericService.Name == "carwashuservicebus")
                    {
                        if (string.IsNullOrWhiteSpace(genericService.Configuration["connectionString"]))
                        {
                            throw new InvalidOperationException("The ServiceBus ConnectionString ('connectionString') is required. Please update your '.bot' file.");
                        }

                        var serviceBusConnection = new ServiceBusConnection(genericService.Configuration["connectionString"]);
                        ServiceBusServices.Add(genericService.Name, serviceBusConnection);
                    }

                    break;
                }
                }
            }
        }