[#3499] Migrate JS samples and generators to Cloud Adapter - samples folder - second batch (#3505)

* Migrate JS samples to CloudAdapter (16-44)

* Fix comment in index
This commit is contained in:
Cecilia Avila 2021-10-12 14:26:20 -03:00 коммит произвёл GitHub
Родитель 249b095c93
Коммит b6fcfddf28
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
14 изменённых файлов: 242 добавлений и 133 удалений

Просмотреть файл

@ -14,23 +14,31 @@ const restify = require('restify');
// Import required bot services.
// See https://aka.ms/bot-services to learn more about the different parts of a bot.
const { BotFrameworkAdapter } = require('botbuilder');
const {
CloudAdapter,
ConfigurationServiceClientCredentialFactory,
createBotFrameworkAuthenticationFromConfiguration
} = require('botbuilder');
// This bot's main dialog.
const { ProactiveBot } = require('./bots/proactiveBot');
const credentialsFactory = new ConfigurationServiceClientCredentialFactory({
MicrosoftAppId: process.env.MicrosoftAppId,
MicrosoftAppPassword: process.env.MicrosoftAppPassword
});
const botFrameworkAuthentication = createBotFrameworkAuthenticationFromConfiguration(null, credentialsFactory);
// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about adapters.
const adapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
});
const adapter = new CloudAdapter(botFrameworkAuthentication);
// Catch-all for errors.
adapter.onTurnError = async (context, error) => {
// This check writes out errors to console log .vs. app insights.
// NOTE: In production environment, you should consider logging this to Azure
// application insights. See https://aka.ms/bottelemetry for telemetry
// application insights. See https://aka.ms/bottelemetry for telemetry
// configuration instructions.
console.error(`\n [onTurnError] unhandled error: ${ error }`);
@ -53,6 +61,8 @@ const bot = new ProactiveBot(conversationReferences);
// Create HTTP server.
const server = restify.createServer();
server.use(restify.plugins.bodyParser());
server.listen(process.env.port || process.env.PORT || 3978, function() {
console.log(`\n${ server.name } listening to ${ server.url }`);
console.log('\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator');
@ -60,18 +70,16 @@ server.listen(process.env.port || process.env.PORT || 3978, function() {
});
// Listen for incoming activities and route them to your bot main dialog.
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (turnContext) => {
// route to main dialog.
await bot.run(turnContext);
});
server.post('/api/messages', async (req, res) => {
// Route received a request to adapter for processing
await adapter.process(req, res, (context) => bot.run(context));
});
// Listen for incoming notifications and send proactive messages to users.
server.get('/api/notify', async (req, res) => {
for (const conversationReference of Object.values(conversationReferences)) {
await adapter.continueConversation(conversationReference, async turnContext => {
await turnContext.sendActivity('proactive hello');
await adapter.continueConversationAsync(process.env.MicrosoftAppId, conversationReference, async context => {
await context.sendActivity('proactive hello');
});
}

Просмотреть файл

@ -16,7 +16,15 @@ const { TranslatorMiddleware } = require('./translation/translatorMiddleware');
// Import required bot services.
// See https://aka.ms/bot-services to learn more about the different parts of a bot.
const { BotFrameworkAdapter, MemoryStorage, UserState, ActivityTypes, TurnContext } = require('botbuilder');
const {
ActivityTypes,
CloudAdapter,
ConfigurationServiceClientCredentialFactory,
createBotFrameworkAuthenticationFromConfiguration,
MemoryStorage,
TurnContext,
UserState
} = require('botbuilder');
// This bot's main dialog.
const { MultilingualBot } = require('./bots/multilingualBot');
@ -24,17 +32,21 @@ const { MultilingualBot } = require('./bots/multilingualBot');
// Used to create the BotStatePropertyAccessor for storing the user's language preference.
const LANGUAGE_PREFERENCE = 'language_preference';
// Create adapter. See https://aka.ms/about-bot-adapter to learn more about adapters.
const adapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
const credentialsFactory = new ConfigurationServiceClientCredentialFactory({
MicrosoftAppId: process.env.MicrosoftAppId,
MicrosoftAppPassword: process.env.MicrosoftAppPassword
});
const botFrameworkAuthentication = createBotFrameworkAuthenticationFromConfiguration(null, credentialsFactory);
// See https://aka.ms/about-bot-adapter to learn more about adapters.
const adapter = new CloudAdapter(botFrameworkAuthentication);
// Catch-all for errors.
adapter.onTurnError = async (context, error) => {
// This check writes out errors to console log .vs. app insights.
// NOTE: In production environment, you should consider logging this to Azure
// application insights. See https://aka.ms/bottelemetry for telemetry
// application insights. See https://aka.ms/bottelemetry for telemetry
// configuration instructions.
console.error(`\n [onTurnError] unhandled error: ${ error }`);
@ -72,6 +84,8 @@ const bot = new MultilingualBot(userState, languagePreferenceProperty);
// Create HTTP server.
const server = restify.createServer();
server.use(restify.plugins.bodyParser());
server.listen(process.env.port || process.env.PORT || 3978, function() {
console.log(`\n${ server.name } listening to ${ server.url }.`);
console.log('\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator');
@ -79,10 +93,7 @@ server.listen(process.env.port || process.env.PORT || 3978, function() {
});
// Listen for incoming activities and route them to your bot main dialog.
server.post('/api/messages', (req, res) => {
// Route received a request to adapter for processing.
adapter.processActivity(req, res, async (context) => {
// Route to bot activity handler.
await bot.run(context);
});
server.post('/api/messages', async (req, res) => {
// Route received a request to adapter for processing
await adapter.process(req, res, (context) => bot.run(context));
});

Просмотреть файл

@ -32,9 +32,11 @@ class LogoutDialog extends ComponentDialog {
if (innerDc.context.activity.type === ActivityTypes.Message) {
const text = innerDc.context.activity.text.toLowerCase();
if (text === 'logout') {
// The bot adapter encapsulates the authentication processes.
const botAdapter = innerDc.context.adapter;
await botAdapter.signOutUser(innerDc.context, this.connectionName);
const userTokenClient = innerDc.context.turnState.get(innerDc.context.adapter.UserTokenClientKey);
const { activity } = innerDc.context;
await userTokenClient.signOutUser(activity.from.id, this.connectionName, activity.channelId);
await innerDc.context.sendActivity('You have been signed out.');
return await innerDc.cancelAllDialogs();
}

Просмотреть файл

@ -14,22 +14,33 @@ const restify = require('restify');
// Import required bot services.
// See https://aka.ms/bot-services to learn more about the different parts of a bot.
const { BotFrameworkAdapter, ConversationState, MemoryStorage, UserState } = require('botbuilder');
const {
CloudAdapter,
ConfigurationServiceClientCredentialFactory,
ConversationState,
createBotFrameworkAuthenticationFromConfiguration,
MemoryStorage,
UserState
} = require('botbuilder');
const { AuthBot } = require('./bots/authBot');
const { MainDialog } = require('./dialogs/mainDialog');
const credentialsFactory = new ConfigurationServiceClientCredentialFactory({
MicrosoftAppId: process.env.MicrosoftAppId,
MicrosoftAppPassword: process.env.MicrosoftAppPassword
});
const botFrameworkAuthentication = createBotFrameworkAuthenticationFromConfiguration(null, credentialsFactory);
// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about adapters.
const adapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
});
const adapter = new CloudAdapter(botFrameworkAuthentication);
adapter.onTurnError = async (context, error) => {
// This check writes out errors to console log .vs. app insights.
// NOTE: In production environment, you should consider logging this to Azure
// application insights. See https://aka.ms/bottelemetry for telemetry
// application insights. See https://aka.ms/bottelemetry for telemetry
// configuration instructions.
console.error(`\n [onTurnError] unhandled error: ${ error }`);
@ -64,6 +75,8 @@ const bot = new AuthBot(conversationState, userState, dialog);
// Create HTTP server.
const server = restify.createServer();
server.use(restify.plugins.bodyParser());
server.listen(process.env.port || process.env.PORT || 3978, function() {
console.log(`\n${ server.name } listening to ${ server.url }`);
console.log('\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator');
@ -71,8 +84,7 @@ server.listen(process.env.port || process.env.PORT || 3978, function() {
});
// Listen for incoming requests.
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (context) => {
await bot.run(context);
});
server.post('/api/messages', async (req, res) => {
// Route received a request to adapter for processing
await adapter.process(req, res, (context) => bot.run(context));
});

Просмотреть файл

@ -10,25 +10,38 @@ require('dotenv').config({ path: ENV_FILE });
// Import required bot services.
// See https://aka.ms/bot-services to learn more about the different parts of a bot.
const { BotFrameworkAdapter, ConversationState, MemoryStorage, UserState } = require('botbuilder');
const {
CloudAdapter,
ConfigurationServiceClientCredentialFactory,
ConversationState,
createBotFrameworkAuthenticationFromConfiguration,
MemoryStorage,
UserState
} = require('botbuilder');
const { DialogBot } = require('./bots/dialogBot');
const { RootDialog } = require('./dialogs/rootDialog');
// Create HTTP server.
const server = restify.createServer();
server.use(restify.plugins.bodyParser());
server.listen(process.env.port || process.env.PORT || 3978, 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 the emulator select "Open Bot"');
});
const credentialsFactory = new ConfigurationServiceClientCredentialFactory({
MicrosoftAppId: process.env.MicrosoftAppId,
MicrosoftAppPassword: process.env.MicrosoftAppPassword
});
const botFrameworkAuthentication = createBotFrameworkAuthenticationFromConfiguration(null, credentialsFactory);
// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about adapters.
const adapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
});
const adapter = new CloudAdapter(botFrameworkAuthentication);
// 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.
@ -45,18 +58,16 @@ const dialog = new RootDialog(userState);
const bot = new DialogBot(conversationState, userState, dialog);
// Listen for incoming requests.
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (turnContext) => {
// Route the message to the bot's main handler.
await bot.run(turnContext);
});
server.post('/api/messages', async (req, res) => {
// Route received a request to adapter for processing
await adapter.process(req, res, (context) => bot.run(context));
});
// Catch-all for errors.
adapter.onTurnError = async (context, error) => {
// This check writes out errors to console log .vs. app insights.
// NOTE: In production environment, you should consider logging this to Azure
// application insights. See https://aka.ms/bottelemetry for telemetry
// application insights. See https://aka.ms/bottelemetry for telemetry
// configuration instructions.
console.error(`\n [onTurnError] unhandled error: ${ error }`);

Просмотреть файл

@ -17,7 +17,17 @@ const { ApplicationInsightsTelemetryClient, TelemetryInitializerMiddleware } = r
const { TelemetryLoggerMiddleware } = require('botbuilder-core');
// Import required bot services. See https://aka.ms/bot-services to learn more about the different parts of a bot.
const { BotFrameworkAdapter, ConversationState, InputHints, MemoryStorage, NullTelemetryClient, UserState } = require('botbuilder');
const {
CloudAdapter,
ConfigurationServiceClientCredentialFactory,
ConversationState,
createBotFrameworkAuthenticationFromConfiguration,
InputHints,
MemoryStorage,
NullTelemetryClient,
UserState
} = require('botbuilder');
const { FlightBookingRecognizer } = require('./dialogs/flightBookingRecognizer');
// This bot's main dialog.
@ -28,18 +38,22 @@ const { MainDialog } = require('./dialogs/mainDialog');
const { BookingDialog } = require('./dialogs/bookingDialog');
const BOOKING_DIALOG = 'bookingDialog';
const credentialsFactory = new ConfigurationServiceClientCredentialFactory({
MicrosoftAppId: process.env.MicrosoftAppId,
MicrosoftAppPassword: process.env.MicrosoftAppPassword
});
const botFrameworkAuthentication = createBotFrameworkAuthenticationFromConfiguration(null, credentialsFactory);
// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about adapters.
const adapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
});
const adapter = new CloudAdapter(botFrameworkAuthentication);
// Catch-all for errors.
const onTurnErrorHandler = async (context, error) => {
// This check writes out errors to console log .vs. app insights.
// NOTE: In production environment, you should consider logging this to Azure
// application insights. See https://aka.ms/bottelemetry for telemetry
// application insights. See https://aka.ms/bottelemetry for telemetry
// configuration instructions.
console.error(`\n [onTurnError] unhandled error: ${ error }`);
@ -60,7 +74,7 @@ const onTurnErrorHandler = async (context, error) => {
await conversationState.delete(context);
};
// Set the onTurnError for the singleton BotFrameworkAdapter.
// Set the onTurnError for the singleton CloudAdapter.
adapter.onTurnError = onTurnErrorHandler;
// Add telemetry middleware to the adapter middleware pipeline
@ -101,12 +115,9 @@ server.listen(process.env.port || process.env.PORT || 3978, function() {
});
// Listen for incoming activities and route them to your bot main dialog.
server.post('/api/messages', (req, res) => {
server.post('/api/messages', async (req, res) => {
// Route received a request to adapter for processing
adapter.processActivity(req, res, async (turnContext) => {
// route to bot activity handler.
await bot.run(turnContext);
});
await adapter.process(req, res, (context) => bot.run(context));
});
// Enable the Application Insights middleware, which helps correlate all activity
@ -122,18 +133,12 @@ function getTelemetryClient(instrumentationKey) {
}
// Listen for Upgrade requests for Streaming.
server.on('upgrade', (req, socket, head) => {
server.on('upgrade', async (req, socket, head) => {
// Create an adapter scoped to this WebSocket connection to allow storing session data.
const streamingAdapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
});
// Set onTurnError for the BotFrameworkAdapter created for each connection.
const streamingAdapter = new CloudAdapter(botFrameworkAuthentication);
// Set onTurnError for the CloudAdapter created for each connection.
streamingAdapter.onTurnError = onTurnErrorHandler;
streamingAdapter.useWebSocket(req, socket, head, async (context) => {
// After connecting via WebSocket, run this logic for every request sent over
// the WebSocket connection.
await bot.run(context);
});
await streamingAdapter.process(req, socket, head, (context) => bot.run(context));
});

Просмотреть файл

@ -14,21 +14,31 @@ const restify = require('restify');
// Import required bot services.
// See https://aka.ms/bot-services to learn more about the different parts of a bot.
const { BotFrameworkAdapter } = require('botbuilder');
const {
CloudAdapter,
ConfigurationServiceClientCredentialFactory,
createBotFrameworkAuthenticationFromConfiguration
} = require('botbuilder');
const { FacebookBot } = require('./bots/facebookBot');
const credentialsFactory = new ConfigurationServiceClientCredentialFactory({
MicrosoftAppId: process.env.MicrosoftAppId,
MicrosoftAppPassword: process.env.MicrosoftAppPassword
});
const botFrameworkAuthentication = createBotFrameworkAuthenticationFromConfiguration(null, credentialsFactory);
// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about adapters.
const adapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
});
const adapter = new CloudAdapter(botFrameworkAuthentication);
const bot = new FacebookBot();
// Create HTTP server
const server = restify.createServer();
server.use(restify.plugins.bodyParser());
server.listen(process.env.port || process.env.PORT || 3978, function() {
console.log(`\n${ server.name } listening to ${ server.url }`);
console.log('\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator');
@ -36,19 +46,16 @@ server.listen(process.env.port || process.env.PORT || 3978, function() {
});
// Listen for incoming activities and route them to your bot main dialog.
server.post('/api/messages', (req, res) => {
server.post('/api/messages', async (req, res) => {
// Route received a request to adapter for processing
adapter.processActivity(req, res, async (turnContext) => {
// route to bot activity handler.
await bot.run(turnContext);
});
await adapter.process(req, res, (context) => bot.run(context));
});
// Catch-all for errors.
adapter.onTurnError = async (context, error) => {
// This check writes out errors to console log .vs. app insights.
// NOTE: In production environment, you should consider logging this to Azure
// application insights. See https://aka.ms/bottelemetry for telemetry
// application insights. See https://aka.ms/bottelemetry for telemetry
// configuration instructions.
console.error(`\n [onTurnError] unhandled error: ${ error }`);

Просмотреть файл

@ -5,6 +5,11 @@ const { ActivityTypes } = require('botbuilder');
const { ComponentDialog } = require('botbuilder-dialogs');
class LogoutDialog extends ComponentDialog {
constructor(id, connectionName) {
super(id);
this.connectionName = connectionName;
}
async onBeginDialog(innerDc, options) {
const result = await this.interrupt(innerDc);
if (result) {
@ -28,8 +33,11 @@ class LogoutDialog extends ComponentDialog {
const text = innerDc.context.activity.text ? innerDc.context.activity.text.toLowerCase() : '';
if (text === 'logout') {
// The bot adapter encapsulates the authentication processes.
const botAdapter = innerDc.context.adapter;
await botAdapter.signOutUser(innerDc.context, process.env.ConnectionName);
const userTokenClient = innerDc.context.turnState.get(innerDc.context.adapter.UserTokenClientKey);
const { activity } = innerDc.context;
await userTokenClient.signOutUser(activity.from.id, this.connectionName, activity.channelId);
await innerDc.context.sendActivity('You have been signed out.');
return await innerDc.cancelAllDialogs();
}

Просмотреть файл

@ -12,7 +12,7 @@ const TEXT_PROMPT = 'textPrompt';
class MainDialog extends LogoutDialog {
constructor() {
super('MainDialog');
super('MainDialog', process.env.connectionName);
this.addDialog(new ChoicePrompt(CHOICE_PROMPT))
.addDialog(new OAuthPrompt(OAUTH_PROMPT, {
connectionName: process.env.ConnectionName,

Просмотреть файл

@ -11,22 +11,34 @@ require('dotenv').config({ path: ENV_FILE });
// Import required bot services.
// See https://aka.ms/bot-services to learn more about the different parts of a bot.
const { BotFrameworkAdapter, ConversationState, MemoryStorage, UserState } = require('botbuilder');
const {
CloudAdapter,
ConfigurationServiceClientCredentialFactory,
ConversationState,
createBotFrameworkAuthenticationFromConfiguration,
MemoryStorage,
UserState
} = require('botbuilder');
const { AuthBot } = require('./bots/authBot');
const { MainDialog } = require('./dialogs/mainDialog');
// Create the adapter. See https://aka.ms/about-bot-adapter to learn more adapters.
const adapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
const credentialsFactory = new ConfigurationServiceClientCredentialFactory({
MicrosoftAppId: process.env.MicrosoftAppId,
MicrosoftAppPassword: process.env.MicrosoftAppPassword
});
const botFrameworkAuthentication = createBotFrameworkAuthenticationFromConfiguration(null, credentialsFactory);
// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about adapters.
const adapter = new CloudAdapter(botFrameworkAuthentication);
// Catch-all for errors.
adapter.onTurnError = async (context, error) => {
// This check writes out errors to console log .vs. app insights.
// NOTE: In production environment, you should consider logging this to Azure
// application insights. See https://aka.ms/bottelemetry for telemetry
// application insights. See https://aka.ms/bottelemetry for telemetry
// configuration instructions.
console.error(`\n [onTurnError] unhandled error: ${ error }`);
@ -61,6 +73,8 @@ const bot = new AuthBot(conversationState, userState, dialog);
// Create HTTP server.
const server = restify.createServer();
server.use(restify.plugins.bodyParser());
server.listen(process.env.port || process.env.PORT || 3978, function() {
console.log(`\n${ server.name } listening to ${ server.url }.`);
console.log('\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator');
@ -68,8 +82,7 @@ server.listen(process.env.port || process.env.PORT || 3978, function() {
});
// Listen for incoming requests.
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (context) => {
await bot.run(context);
});
server.post('/api/messages', async (req, res) => {
// Route received a request to adapter for processing
await adapter.process(req, res, (context) => bot.run(context));
});

Просмотреть файл

@ -14,22 +14,31 @@ const restify = require('restify');
// Import required bot services.
// See https://aka.ms/bot-services to learn more about the different parts of a bot.
const { BotFrameworkAdapter, MemoryStorage } = require('botbuilder');
const {
CloudAdapter,
ConfigurationServiceClientCredentialFactory,
createBotFrameworkAuthenticationFromConfiguration,
MemoryStorage
} = require('botbuilder');
const { ActivityLog } = require('./activityLog');
const { MessageReactionBot } = require('./bots/messageReactionBot');
// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about adapters.
const adapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
const credentialsFactory = new ConfigurationServiceClientCredentialFactory({
MicrosoftAppId: process.env.MicrosoftAppId,
MicrosoftAppPassword: process.env.MicrosoftAppPassword
});
const botFrameworkAuthentication = createBotFrameworkAuthenticationFromConfiguration(null, credentialsFactory);
// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about how bots work.
const adapter = new CloudAdapter(botFrameworkAuthentication);
adapter.onTurnError = async (context, error) => {
// This check writes out errors to console log .vs. app insights.
// NOTE: In production environment, you should consider logging this to Azure
// application insights. See https://aka.ms/bottelemetry for telemetry
// application insights. See https://aka.ms/bottelemetry for telemetry
// configuration instructions.
console.error(`\n [onTurnError] unhandled error: ${ error }`);
@ -55,13 +64,14 @@ const bot = new MessageReactionBot(activityLog);
// Create HTTP server.
const server = restify.createServer();
server.use(restify.plugins.bodyParser());
server.listen(process.env.port || process.env.PORT || 3978, function() {
console.log(`\n${ server.name } listening to ${ server.url }`);
});
// Listen for incoming requests.
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (context) => {
await bot.run(context);
});
server.post('/api/messages', async (req, res) => {
// Route received a request to adapter for processing
await adapter.process(req, res, (context) => bot.run(context));
});

Просмотреть файл

@ -11,8 +11,8 @@
"termsOfUseUrl": "https://www.teams.com/termsofuser"
},
"icons": {
"color": "color.png",
"outline": "outline.png"
"color": "icon-color.png",
"outline": "icon-outline.png"
},
"name": {
"short": "MessageReactionBot",

Просмотреть файл

@ -11,26 +11,39 @@ const restify = require('restify');
// Import required bot services.
// See https://aka.ms/bot-services to learn more about the different parts of a bot.
const { BotFrameworkAdapter, MemoryStorage, UserState, ConversationState } = require('botbuilder');
const {
CloudAdapter,
ConfigurationServiceClientCredentialFactory,
ConversationState,
createBotFrameworkAuthenticationFromConfiguration,
MemoryStorage,
UserState
} = require('botbuilder');
const { DialogAndWelcomeBot } = require('./bots/dialogAndWelcomeBot');
const { MainDialog } = require('./dialogs/mainDialog');
// Create HTTP server
const server = restify.createServer();
server.use(restify.plugins.bodyParser());
server.listen(process.env.port || process.env.PORT || 3978, 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 the emulator select "Open Bot"');
});
// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about adapters.
const adapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
const credentialsFactory = new ConfigurationServiceClientCredentialFactory({
MicrosoftAppId: process.env.MicrosoftAppId,
MicrosoftAppPassword: process.env.MicrosoftAppPassword
});
const botFrameworkAuthentication = createBotFrameworkAuthenticationFromConfiguration(null, credentialsFactory);
// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about how bots work.
const adapter = new CloudAdapter(botFrameworkAuthentication);
// Define state store for your bot.
// See https://aka.ms/about-bot-state to learn more about bot state.
const memoryStorage = new MemoryStorage();
@ -47,7 +60,7 @@ const bot = new DialogAndWelcomeBot(conversationState, userState, dialog);
adapter.onTurnError = async (context, error) => {
// This check writes out errors to console log .vs. app insights.
// NOTE: In production environment, you should consider logging this to Azure
// application insights. See https://aka.ms/bottelemetry for telemetry
// application insights. See https://aka.ms/bottelemetry for telemetry
// configuration instructions.
console.error(`\n [onTurnError] unhandled error: ${ error }`);
@ -67,9 +80,7 @@ adapter.onTurnError = async (context, error) => {
};
// Listen for incoming requests.
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (context) => {
// Route to main dialog.
await bot.run(context);
});
server.post('/api/messages', async (req, res) => {
// Route received a request to adapter for processing
await adapter.process(req, res, (context) => bot.run(context));
});

Просмотреть файл

@ -11,26 +11,39 @@ const restify = require('restify');
// Import required bot services.
// See https://aka.ms/bot-services to learn more about the different parts of a bot.
const { BotFrameworkAdapter, MemoryStorage, ConversationState, UserState } = require('botbuilder');
const {
CloudAdapter,
ConfigurationServiceClientCredentialFactory,
ConversationState,
createBotFrameworkAuthenticationFromConfiguration,
MemoryStorage,
UserState
} = require('botbuilder');
// This bot's main dialog.
const { CustomPromptBot } = require('./bots/customPromptBot');
// Create HTTP server
const server = restify.createServer();
server.use(restify.plugins.bodyParser());
server.listen(process.env.port || process.env.PORT || 3978, 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 the emulator select "Open Bot"');
});
// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about adapters.
const adapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
const credentialsFactory = new ConfigurationServiceClientCredentialFactory({
MicrosoftAppId: process.env.MicrosoftAppId,
MicrosoftAppPassword: process.env.MicrosoftAppPassword
});
const botFrameworkAuthentication = createBotFrameworkAuthenticationFromConfiguration(null, credentialsFactory);
// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about how bots work.
const adapter = new CloudAdapter(botFrameworkAuthentication);
// Create conversation and user state with in-memory storage provider.
const memoryStorage = new MemoryStorage();
const conversationState = new ConversationState(memoryStorage);
@ -43,7 +56,7 @@ const bot = new CustomPromptBot(conversationState, userState);
adapter.onTurnError = async (context, error) => {
// This check writes out errors to console log .vs. app insights.
// NOTE: In production environment, you should consider logging this to Azure
// application insights. See https://aka.ms/bottelemetry for telemetry
// application insights. See https://aka.ms/bottelemetry for telemetry
// configuration instructions.
console.error(`\n [onTurnError] unhandled error: ${ error }`);
@ -63,9 +76,7 @@ adapter.onTurnError = async (context, error) => {
};
// Listen for incoming requests.
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (context) => {
// Route to main dialog.
await bot.run(context);
});
server.post('/api/messages', async (req, res) => {
// Route received a request to adapter for processing
await adapter.process(req, res, (context) => bot.run(context));
});