forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (69 loc) · 3.92 KB
/
index.js
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const restify = require('restify');
const path = require('path');
const CONFIG_ERROR = 1;
// Import required bot services. See https://aka.ms/bot-services to learn more about the different part of a bot.
const { BotFrameworkAdapter, AutoSaveStateMiddleware, ConversationState, MemoryStorage } = require('botbuilder');
const { BotConfiguration } = require('botframework-config');
const { DiceRollerSkill } = require('./dialogs/diceRollerSkill');
// Read botFilePath and botFileSecret from .env file.
// Note: Ensure you have a .env file and include botFilePath and botFileSecret.
const ENV_FILE = path.join(__dirname, '.env');
require('dotenv').config({ path: ENV_FILE });
// Create HTTP server.
let server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3988, function() {
console.log(`\n${ server.name } listening to ${ server.url }.`);
console.log(`\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator.`);
console.log(`\nTo talk to your bot, open diceroller-skill.bot file in the emulator.`);
});
// .bot file path
const BOT_FILE = path.join(__dirname, (process.env.botFilePath || '/cortana-skill.bot'));
// Read the configuration from a .bot file.
// This includes information about the bot's endpoints and configuration.
let botConfig;
try {
botConfig = BotConfiguration.loadSync(BOT_FILE, process.env.botFileSecret);
} catch (err) {
console.log(`Error reading bot file. Please ensure you have valid botFilePath and botFileSecret set for your environment.`);
console.log(err);
process.exit(CONFIG_ERROR);
}
// Define the name of the bot, as specified in the .bot file.
// See https://aka.ms/about-bot-file to learn more about .bot files.
const BOT_CONFIGURATION = 'cortana-skill';
// Load the configuration profile specific to this bot identity.
const endpointConfig = botConfig.findServiceByNameOrId(BOT_CONFIGURATION);
// Create the adapter. See https://aka.ms/about-bot-adapter to learn more about using information from
// the .bot file when configuring your adapter.
const adapter = new BotFrameworkAdapter({
appId: endpointConfig.appId || process.env.MicrosoftAppId,
appPassword: endpointConfig.appPassword || process.env.MicrosoftAppPassword
});
// Define the state store for your bot. See https://aka.ms/about-bot-state to learn more about using MemoryStorage.
// A bot requires a state storage system to persist the dialog and user state between messages.
const memoryStorage = new MemoryStorage();
// CAUTION: The Memory Storage used here is for local bot debugging only. When the bot
// is restarted, anything stored in memory will be gone.
// For production bots use the Azure Cosmos DB storage, or Azure Blob storage providers.
// const { CosmosDbStorage } = require('botbuilder-azure');
// const STORAGE_CONFIGURATION = 'cosmosDB'; // Cosmos DB configuration in your .bot file
// const cosmosConfig = botConfig.findServiceByNameOrId(STORAGE_CONFIGURATION);
// const cosmosStorage = new CosmosDbStorage({serviceEndpoint: cosmosConfig.connectionString,
// authKey: ?,
// databaseId: cosmosConfig.database,
// collectionId: cosmosConfig.collection});
// Create conversation state with in-memory storage provider.
const conversationState = new ConversationState(memoryStorage);
// Add AutoSaveStateMiddleware to manage saving of conversation state.
adapter.use(new AutoSaveStateMiddleware(conversationState));
// Create the skill class, which serves as the bot's main handler.
const skill = new DiceRollerSkill(conversationState);
// Listen for incoming requests.
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (turnContext) => {
// Route the message to the bots main handler.
await skill.run(turnContext);
});
});