forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BotServices.cs
60 lines (56 loc) · 2.54 KB
/
BotServices.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License
using System;
using System.Collections.Generic;
using Microsoft.Bot.Builder.AI.Luis;
using Microsoft.Bot.Configuration;
namespace Microsoft.BotBuilderSamples
{
/// <summary>
/// Represents references to external services.
///
/// For example, LUIS services are kept here as a singleton. This external service is configured
/// using the <see cref="BotConfiguration"/> class.
/// </summary>
/// <seealso cref="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1"/>
/// <seealso cref="https://www.luis.ai/home"/>
public class BotServices
{
/// <summary>
/// Initializes a new instance of the <see cref="BotServices"/> class.
/// </summary>
/// <param name="botConfiguration">A dictionary of named <see cref="BotConfiguration"/> instances for usage within the bot.</param>
public BotServices(BotConfiguration botConfiguration)
{
foreach (var service in botConfiguration.Services)
{
switch (service.Type)
{
case ServiceTypes.Luis:
{
var luis = (LuisService)service;
if (luis == null)
{
throw new InvalidOperationException("The LUIS service is not configured correctly in your '.bot' file.");
}
var app = new LuisApplication(luis.AppId, luis.AuthoringKey, luis.GetEndpoint());
var recognizer = new LuisRecognizer(app);
this.LuisServices.Add(luis.Name, recognizer);
break;
}
}
}
}
/// <summary>
/// Gets the set of LUIS Services used.
/// Given there can be multiple <see cref="LuisRecognizer"/> services used in a single bot,
/// LuisServices is represented as a dictionary. This is also modeled in the
/// ".bot" file since the elements are named.
/// </summary>
/// <remarks>The LUIS services collection should not be modified while the bot is running.</remarks>
/// <value>
/// A <see cref="LuisRecognizer"/> client instance created based on configuration in the .bot file.
/// </value>
public Dictionary<string, LuisRecognizer> LuisServices { get; } = new Dictionary<string, LuisRecognizer>();
}
}