Updating to latest architectural changes from BotBuilder-js v4.
This commit is contained in:
parent
4315449a91
commit
820d4f612e
6 changed files with 56 additions and 44 deletions
|
@ -46,12 +46,14 @@ export class WelcomeDialog extends IGBDialog {
|
||||||
*/
|
*/
|
||||||
static setup(bot: BotAdapter, min: GBMinInstance) {
|
static setup(bot: BotAdapter, min: GBMinInstance) {
|
||||||
min.dialogs.add("/", [
|
min.dialogs.add("/", [
|
||||||
async (dc, args) => {
|
async (dc) => {
|
||||||
const user = min.userState.get(dc.context);
|
const user = await min.userProfile.get(context, {});
|
||||||
|
|
||||||
const locale = dc.context.activity.locale;
|
const locale = dc.context.activity.locale;
|
||||||
|
|
||||||
if (!user.once) {
|
if (!user.once) {
|
||||||
user.once = true;
|
user.once = true;
|
||||||
|
await min.userProfile.set(context, user);
|
||||||
var a = new Date();
|
var a = new Date();
|
||||||
const date = a.getHours();
|
const date = a.getHours();
|
||||||
var msg =
|
var msg =
|
||||||
|
|
|
@ -32,7 +32,7 @@
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { TextPrompt } = require("botbuilder-dialogs");
|
const {DialogSet, TextPrompt } = require("botbuilder-dialogs");
|
||||||
const UrlJoin = require("url-join");
|
const UrlJoin = require("url-join");
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const logger = require("../../../src/logger");
|
const logger = require("../../../src/logger");
|
||||||
|
@ -45,7 +45,8 @@ import {
|
||||||
ConversationState,
|
ConversationState,
|
||||||
MemoryStorage,
|
MemoryStorage,
|
||||||
UserState,
|
UserState,
|
||||||
BotState
|
AutoSaveStateMiddleware
|
||||||
|
|
||||||
} from "botbuilder";
|
} from "botbuilder";
|
||||||
|
|
||||||
import { GBMinInstance, IGBPackage } from "botlib";
|
import { GBMinInstance, IGBPackage } from "botlib";
|
||||||
|
@ -307,6 +308,7 @@ export class GBMinService {
|
||||||
const storage = new MemoryStorage();
|
const storage = new MemoryStorage();
|
||||||
const conversationState = new ConversationState(storage);
|
const conversationState = new ConversationState(storage);
|
||||||
const userState = new UserState(storage);
|
const userState = new UserState(storage);
|
||||||
|
adapter.use(new AutoSaveStateMiddleware(conversationState, userState));
|
||||||
|
|
||||||
// The minimal bot is built here.
|
// The minimal bot is built here.
|
||||||
|
|
||||||
|
@ -318,7 +320,10 @@ export class GBMinService {
|
||||||
min.conversationalService = this.conversationalService;
|
min.conversationalService = this.conversationalService;
|
||||||
min.adminService = this.adminService;
|
min.adminService = this.adminService;
|
||||||
min.instance = await this.core.loadInstance(min.botId);
|
min.instance = await this.core.loadInstance(min.botId);
|
||||||
min.dialogs.add("textPrompt", new TextPrompt());
|
min.userProfile = conversationState.createProperty('userProfile');
|
||||||
|
const dialogState = conversationState.createProperty('dialogState');
|
||||||
|
min.dialogs = new DialogSet(dialogState);
|
||||||
|
//min.dialogs.add("textPrompt", new TextPrompt());
|
||||||
|
|
||||||
return { min, adapter, conversationState };
|
return { min, adapter, conversationState };
|
||||||
}
|
}
|
||||||
|
@ -366,11 +371,11 @@ export class GBMinService {
|
||||||
) {
|
) {
|
||||||
return adapter.processActivity(req, res, async context => {
|
return adapter.processActivity(req, res, async context => {
|
||||||
const state = conversationState.get(context);
|
const state = conversationState.get(context);
|
||||||
const dc = min.dialogs.createContext(context, state);
|
const dc = await min.dialogs.createContext(context, state);
|
||||||
dc.context.activity.locale = "en-US"; // TODO: Make dynamic.
|
dc.context.activity.locale = "en-US"; // TODO: Make dynamic.
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const user = min.userState.get(dc.context);
|
const user = await min.userProfile.get(context, {});
|
||||||
|
|
||||||
if (!user.loaded) {
|
if (!user.loaded) {
|
||||||
await min.conversationalService.sendEvent(dc, "loadInstance", {
|
await min.conversationalService.sendEvent(dc, "loadInstance", {
|
||||||
|
@ -381,6 +386,7 @@ export class GBMinService {
|
||||||
});
|
});
|
||||||
user.loaded = true;
|
user.loaded = true;
|
||||||
user.subjects = [];
|
user.subjects = [];
|
||||||
|
await min.userProfile.set(context, user);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
|
@ -398,10 +404,9 @@ export class GBMinService {
|
||||||
appPackages.forEach(e => {
|
appPackages.forEach(e => {
|
||||||
e.onNewSession(min, dc);
|
e.onNewSession(min, dc);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Processes the root dialog.
|
// Processes the root dialog.
|
||||||
|
|
||||||
await dc.begin("/");
|
await dc.beginDialog("/");
|
||||||
} else {
|
} else {
|
||||||
logger.info(`Member added to conversation: ${member.name}`);
|
logger.info(`Member added to conversation: ${member.name}`);
|
||||||
}
|
}
|
||||||
|
@ -411,11 +416,11 @@ export class GBMinService {
|
||||||
// Checks for /admin request.
|
// Checks for /admin request.
|
||||||
|
|
||||||
if (context.activity.text === "admin") {
|
if (context.activity.text === "admin") {
|
||||||
await dc.begin("/admin");
|
await dc.beginDialog("/admin");
|
||||||
|
|
||||||
// Checks for /menu JSON signature.
|
// Checks for /menu JSON signature.
|
||||||
} else if (context.activity.text.startsWith('{"title"')) {
|
} else if (context.activity.text.startsWith('{"title"')) {
|
||||||
await dc.begin("/menu", {
|
await dc.beginDialog("/menu", {
|
||||||
data: JSON.parse(context.activity.text)
|
data: JSON.parse(context.activity.text)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -424,7 +429,7 @@ export class GBMinService {
|
||||||
if (dc.activeDialog) {
|
if (dc.activeDialog) {
|
||||||
await dc.continue();
|
await dc.continue();
|
||||||
} else {
|
} else {
|
||||||
await dc.begin("/answer", { query: context.activity.text });
|
await dc.beginDialog("/answer", { query: context.activity.text });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -435,27 +440,27 @@ export class GBMinService {
|
||||||
await dc.endAll();
|
await dc.endAll();
|
||||||
|
|
||||||
if (context.activity.name === "whoAmI") {
|
if (context.activity.name === "whoAmI") {
|
||||||
await dc.begin("/whoAmI");
|
await dc.beginDialog("/whoAmI");
|
||||||
} else if (context.activity.name === "showSubjects") {
|
} else if (context.activity.name === "showSubjects") {
|
||||||
await dc.begin("/menu");
|
await dc.beginDialog("/menu");
|
||||||
} else if (context.activity.name === "giveFeedback") {
|
} else if (context.activity.name === "giveFeedback") {
|
||||||
await dc.begin("/feedback", {
|
await dc.beginDialog("/feedback", {
|
||||||
fromMenu: true
|
fromMenu: true
|
||||||
});
|
});
|
||||||
} else if (context.activity.name === "showFAQ") {
|
} else if (context.activity.name === "showFAQ") {
|
||||||
await dc.begin("/faq");
|
await dc.beginDialog("/faq");
|
||||||
} else if (context.activity.name === "answerEvent") {
|
} else if (context.activity.name === "answerEvent") {
|
||||||
await dc.begin("/answerEvent", {
|
await dc.beginDialog("/answerEvent", {
|
||||||
questionId: (context.activity as any).data,
|
questionId: (context.activity as any).data,
|
||||||
fromFaq: true
|
fromFaq: true
|
||||||
});
|
});
|
||||||
} else if (context.activity.name === "quality") {
|
} else if (context.activity.name === "quality") {
|
||||||
await dc.begin("/quality", {
|
await dc.beginDialog("/quality", {
|
||||||
score: (context.activity as any).data
|
score: (context.activity as any).data
|
||||||
});
|
});
|
||||||
} else if (context.activity.name === "updateToken") {
|
} else if (context.activity.name === "updateToken") {
|
||||||
let token = (context.activity as any).data;
|
let token = (context.activity as any).data;
|
||||||
await dc.begin("/adminUpdateToken", { token: token });
|
await dc.beginDialog("/adminUpdateToken", { token: token });
|
||||||
} else {
|
} else {
|
||||||
await dc.continue();
|
await dc.continue();
|
||||||
}
|
}
|
||||||
|
@ -467,7 +472,7 @@ export class GBMinService {
|
||||||
await dc.context.sendActivity(
|
await dc.context.sendActivity(
|
||||||
Messages[dc.context.activity.locale].very_sorry_about_error
|
Messages[dc.context.activity.locale].very_sorry_about_error
|
||||||
);
|
);
|
||||||
await dc.begin("/ask", { isReturning: true });
|
await dc.beginDialog("/ask", { isReturning: true });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,7 +63,7 @@ export class FeedbackDialog extends IGBDialog {
|
||||||
async (dc, value) => {
|
async (dc, value) => {
|
||||||
let locale = dc.context.activity.locale;
|
let locale = dc.context.activity.locale;
|
||||||
let rate = value.entity;
|
let rate = value.entity;
|
||||||
const user = min.userState.get(dc.context);
|
const user = await min.userProfile.get(context, {});
|
||||||
await service.updateConversationRate(user.conversation, rate);
|
await service.updateConversationRate(user.conversation, rate);
|
||||||
await dc.context.sendActivity(Messages[locale].thanks);
|
await dc.context.sendActivity(Messages[locale].thanks);
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,7 +53,8 @@ export class QualityDialog extends IGBDialog {
|
||||||
min.dialogs.add("/quality", [
|
min.dialogs.add("/quality", [
|
||||||
async (dc, args) => {
|
async (dc, args) => {
|
||||||
const locale = dc.context.activity.locale;
|
const locale = dc.context.activity.locale;
|
||||||
const user = min.userState.get(dc.context);
|
const user = await min.userProfile.get(context, {});
|
||||||
|
|
||||||
var score = args.score;
|
var score = args.score;
|
||||||
|
|
||||||
setTimeout(
|
setTimeout(
|
||||||
|
|
|
@ -55,29 +55,29 @@ export class AskDialog extends IGBDialog {
|
||||||
|
|
||||||
min.dialogs.add("/answerEvent", [
|
min.dialogs.add("/answerEvent", [
|
||||||
async (dc, args) => {
|
async (dc, args) => {
|
||||||
|
|
||||||
if (args && args.questionId) {
|
if (args && args.questionId) {
|
||||||
|
let question = await service.getQuestionById(
|
||||||
let question = await service.getQuestionById(min.instance.instanceId, args.questionId);
|
min.instance.instanceId,
|
||||||
let answer = await service.getAnswerById(min.instance.instanceId, question.answerId)
|
args.questionId
|
||||||
|
);
|
||||||
|
let answer = await service.getAnswerById(
|
||||||
|
min.instance.instanceId,
|
||||||
|
question.answerId
|
||||||
|
);
|
||||||
|
|
||||||
// Sends the answer to all outputs, including projector.
|
// Sends the answer to all outputs, including projector.
|
||||||
|
|
||||||
await service.sendAnswer(
|
await service.sendAnswer(min.conversationalService, dc, answer);
|
||||||
min.conversationalService,
|
|
||||||
dc,
|
|
||||||
answer
|
|
||||||
);
|
|
||||||
|
|
||||||
await dc.replace("/ask", { isReturning: true });
|
await dc.replace("/ask", { isReturning: true });
|
||||||
}
|
}
|
||||||
}])
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
min.dialogs.add("/answer", [
|
min.dialogs.add("/answer", [
|
||||||
async (dc, args) => {
|
async (dc, args) => {
|
||||||
// Initialize values.
|
const user = await min.userProfile.get(context, {});
|
||||||
|
|
||||||
const user = min.userState.get(dc.context);
|
|
||||||
let text = args.query;
|
let text = args.query;
|
||||||
if (!text) {
|
if (!text) {
|
||||||
throw new Error(`/answer being called with no args.query text.`);
|
throw new Error(`/answer being called with no args.query text.`);
|
||||||
|
@ -114,6 +114,7 @@ export class AskDialog extends IGBDialog {
|
||||||
// Searches KB for the first time.
|
// Searches KB for the first time.
|
||||||
|
|
||||||
user.lastQuestion = text;
|
user.lastQuestion = text;
|
||||||
|
await min.userProfile.set(context, user);
|
||||||
let resultsA = await service.ask(
|
let resultsA = await service.ask(
|
||||||
min.instance,
|
min.instance,
|
||||||
text,
|
text,
|
||||||
|
@ -128,6 +129,7 @@ export class AskDialog extends IGBDialog {
|
||||||
|
|
||||||
user.isAsking = false;
|
user.isAsking = false;
|
||||||
user.lastQuestionId = resultsA.questionId;
|
user.lastQuestionId = resultsA.questionId;
|
||||||
|
await min.userProfile.set(context, user);
|
||||||
|
|
||||||
// Sends the answer to all outputs, including projector.
|
// Sends the answer to all outputs, including projector.
|
||||||
|
|
||||||
|
@ -155,9 +157,11 @@ export class AskDialog extends IGBDialog {
|
||||||
if (resultsB && resultsB.answer) {
|
if (resultsB && resultsB.answer) {
|
||||||
// Saves some context info.
|
// Saves some context info.
|
||||||
|
|
||||||
const user = min.userState.get(dc.context);
|
const user = await min.userProfile.get(context, {});
|
||||||
|
|
||||||
user.isAsking = false;
|
user.isAsking = false;
|
||||||
user.lastQuestionId = resultsB.questionId;
|
user.lastQuestionId = resultsB.questionId;
|
||||||
|
await min.userProfile.set(context, user);
|
||||||
|
|
||||||
// Informs user that a broader search will be used.
|
// Informs user that a broader search will be used.
|
||||||
|
|
||||||
|
@ -189,7 +193,7 @@ export class AskDialog extends IGBDialog {
|
||||||
min.dialogs.add("/ask", [
|
min.dialogs.add("/ask", [
|
||||||
async (dc, args) => {
|
async (dc, args) => {
|
||||||
const locale = dc.context.activity.locale;
|
const locale = dc.context.activity.locale;
|
||||||
const user = min.userState.get(dc.context);
|
const user = await min.userProfile.get(context, {});
|
||||||
user.isAsking = true;
|
user.isAsking = true;
|
||||||
if (!user.subjects) {
|
if (!user.subjects) {
|
||||||
user.subjects = [];
|
user.subjects = [];
|
||||||
|
@ -214,7 +218,7 @@ export class AskDialog extends IGBDialog {
|
||||||
},
|
},
|
||||||
async (dc, value) => {
|
async (dc, value) => {
|
||||||
await dc.endAll();
|
await dc.endAll();
|
||||||
await dc.begin("/answer", { query: value });
|
await dc.beginDialog("/answer", { query: value });
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,7 +71,7 @@ export class MenuDialog extends IGBDialog {
|
||||||
|
|
||||||
// Adds to bot a perception of a new subject.
|
// Adds to bot a perception of a new subject.
|
||||||
|
|
||||||
const user = min.userState.get(dc.context)
|
const user = await min.userProfile.get(context, {});
|
||||||
user.subjects.push(subject)
|
user.subjects.push(subject)
|
||||||
rootSubjectId = subject.subjectId
|
rootSubjectId = subject.subjectId
|
||||||
|
|
||||||
|
@ -88,7 +88,7 @@ export class MenuDialog extends IGBDialog {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const user = min.userState.get(dc.context)
|
const user = await min.userProfile.get(context, {});
|
||||||
user.subjects = []
|
user.subjects = []
|
||||||
|
|
||||||
await dc.context.sendActivity(Messages[locale].here_is_subjects) // TODO: Handle rnd.
|
await dc.context.sendActivity(Messages[locale].here_is_subjects) // TODO: Handle rnd.
|
||||||
|
@ -137,7 +137,7 @@ export class MenuDialog extends IGBDialog {
|
||||||
})
|
})
|
||||||
|
|
||||||
if (attachments.length == 0) {
|
if (attachments.length == 0) {
|
||||||
const user = min.userState.get(dc.context)
|
const user = await min.userProfile.get(context, {});
|
||||||
|
|
||||||
if (user.subjects && user.subjects.length > 0) {
|
if (user.subjects && user.subjects.length > 0) {
|
||||||
await dc.context.sendActivity(
|
await dc.context.sendActivity(
|
||||||
|
@ -153,7 +153,7 @@ export class MenuDialog extends IGBDialog {
|
||||||
await dc.context.sendActivity(msg)
|
await dc.context.sendActivity(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = min.userState.get(dc.context)
|
const user = await min.userProfile.get(context, {});
|
||||||
user.isAsking = true
|
user.isAsking = true
|
||||||
},
|
},
|
||||||
async (dc, value) => {
|
async (dc, value) => {
|
||||||
|
|
Loading…
Add table
Reference in a new issue