This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
/
index.ts
191 lines (168 loc) · 7.07 KB
/
index.ts
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* Copyright(c) Microsoft Corporation.All rights reserved.
* Licensed under the MIT License.
*/
import {
BotFrameworkAdapterSettings,
BotTelemetryClient,
ConversationState,
NullTelemetryClient,
StatePropertyAccessor,
TurnContext,
UserState} from 'botbuilder';
import { ApplicationInsightsTelemetryClient, ApplicationInsightsWebserverMiddleware } from 'botbuilder-applicationinsights';
import {
CosmosDbStorage,
CosmosDbStorageSettings } from 'botbuilder-azure';
import {
Dialog,
DialogState } from 'botbuilder-dialogs';
import {
manifestGenerator,
SkillContext,
SkillHttpAdapter } from 'botbuilder-skills';
import {
ICognitiveModelConfiguration,
Locales,
ResponseManager} from 'botbuilder-solutions';
import i18next from 'i18next';
import i18nextNodeFsBackend from 'i18next-node-fs-backend';
import { join } from 'path';
import * as restify from 'restify';
import { DefaultAdapter } from './adapters/defaultAdapter';
import { SampleSkillAdapter } from './adapters/sampleSkillAdapter';
import * as appsettings from './appsettings.json';
import { DialogBot } from './bots/dialogBot';
import * as cognitiveModelsRaw from './cognitivemodels.json';
import { MainDialog } from './dialogs/mainDialog';
import { SampleDialog } from './dialogs/sampleDialog';
import { SkillState } from './models/skillState';
import { MainResponses } from './responses/main/mainResponses';
import { SampleResponses } from './responses/sample/sampleResponses';
import { SharedResponses } from './responses/shared/sharedResponses';
import { BotServices } from './services/botServices';
import { IBotSettings } from './services/botSettings';
// Configure internationalization and default locale
i18next.use(i18nextNodeFsBackend)
.init({
fallbackLng: 'en-us',
preload: ['de-de', 'en-us', 'es-es', 'fr-fr', 'it-it', 'zh-cn']
})
.then(async (): Promise<void> => {
await Locales.addResourcesFromPath(i18next, 'common');
});
const cognitiveModels: Map<string, ICognitiveModelConfiguration> = new Map();
const cognitiveModelDictionary: { [key: string]: Object } = cognitiveModelsRaw.cognitiveModels;
const cognitiveModelMap: Map<string, Object> = new Map(Object.entries(cognitiveModelDictionary));
cognitiveModelMap.forEach((value: Object, key: string): void => {
cognitiveModels.set(key, value as ICognitiveModelConfiguration);
});
const botSettings: Partial<IBotSettings> = {
appInsights: appsettings.appInsights,
blobStorage: appsettings.blobStorage,
cognitiveModels: cognitiveModels,
cosmosDb: appsettings.cosmosDb,
defaultLocale: cognitiveModelsRaw.defaultLocale,
microsoftAppId: appsettings.microsoftAppId,
microsoftAppPassword: appsettings.microsoftAppPassword
};
if (botSettings.appInsights === undefined) {
throw new Error('There is no appInsights value in appsettings file');
}
function getTelemetryClient(settings: Partial<IBotSettings>): BotTelemetryClient {
if (settings !== undefined && settings.appInsights !== undefined && settings.appInsights.instrumentationKey !== undefined) {
const instrumentationKey: string = settings.appInsights.instrumentationKey;
return new ApplicationInsightsTelemetryClient(instrumentationKey);
}
return new NullTelemetryClient();
}
const telemetryClient: BotTelemetryClient = getTelemetryClient(botSettings);
if (botSettings.cosmosDb === undefined) {
throw new Error();
}
const cosmosDbStorageSettings: CosmosDbStorageSettings = {
authKey: botSettings.cosmosDb.authKey,
collectionId: botSettings.cosmosDb.collectionId,
databaseId: botSettings.cosmosDb.databaseId,
serviceEndpoint: botSettings.cosmosDb.cosmosDBEndpoint
};
const storage: CosmosDbStorage = new CosmosDbStorage(cosmosDbStorageSettings);
const userState: UserState = new UserState(storage);
const conversationState: ConversationState = new ConversationState(storage);
const stateAccessor: StatePropertyAccessor<SkillState> = userState.createProperty(SkillState.name);
const dialogStateAccessor: StatePropertyAccessor<DialogState> = userState.createProperty('DialogState');
const skillContextAccessor: StatePropertyAccessor<SkillContext> = userState.createProperty(SkillContext.name);
const adapterSettings: Partial<BotFrameworkAdapterSettings> = {
appId: botSettings.microsoftAppId,
appPassword: botSettings.microsoftAppPassword
};
const botAdapter: DefaultAdapter = new DefaultAdapter(
botSettings,
adapterSettings,
userState,
conversationState,
telemetryClient);
const sampleSkillAdapter: SampleSkillAdapter = new SampleSkillAdapter(
botSettings,
userState,
conversationState,
telemetryClient,
skillContextAccessor,
dialogStateAccessor);
const adapter: SkillHttpAdapter = new SkillHttpAdapter(sampleSkillAdapter);
let bot: DialogBot<Dialog>;
try {
const responseManager: ResponseManager = new ResponseManager(
['en-us', 'de-de', 'es-es', 'fr-fr', 'it-it', 'zh-cn'],
[SampleResponses, MainResponses, SharedResponses]);
const botServices: BotServices = new BotServices(botSettings, telemetryClient);
const sampleDialog: SampleDialog = new SampleDialog(
botSettings,
botServices,
responseManager,
stateAccessor,
telemetryClient
);
const mainDialog: MainDialog = new MainDialog(
botSettings,
botServices,
responseManager,
stateAccessor,
skillContextAccessor,
sampleDialog,
telemetryClient
);
bot = new DialogBot(conversationState, userState, telemetryClient, mainDialog);
} catch (err) {
throw err;
}
// Create server
const server: restify.Server = restify.createServer();
// Enable the Application Insights middleware, which helps correlate all activity
// based on the incoming request.
server.use(restify.plugins.bodyParser());
server.use(ApplicationInsightsWebserverMiddleware);
server.listen(process.env.port || process.env.PORT || '3980', (): void => {
console.log(`${server.name} listening to ${server.url}`);
console.log(`Get the Emulator: https://aka.ms/botframework-emulator`);
console.log(`To talk to your bot, open your '.bot' file in the Emulator`);
});
// Listen for incoming requests
server.post('/api/messages', async (req: restify.Request, res: restify.Response): Promise<void> => {
// Route received a request to adapter for processing
await botAdapter.processActivity(req, res, async (turnContext: TurnContext): Promise<void> => {
// route to bot activity handler.
await bot.run(turnContext);
});
});
// Listen for incoming assistant requests
server.post('/api/skill/messages', async (req: restify.Request, res: restify.Response): Promise<void> => {
// Route received a request to adapter for processing
await adapter.processActivity(req, res, async (turnContext: TurnContext): Promise<void> => {
// route to bot activity handler.
await bot.run(turnContext);
});
});
const manifestPath: string = join(__dirname, 'manifestTemplate.json');
server.use(restify.plugins.queryParser());
server.get('/api/skill/manifest', manifestGenerator(manifestPath, botSettings));