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) {
|
||||
min.dialogs.add("/", [
|
||||
async (dc, args) => {
|
||||
const user = min.userState.get(dc.context);
|
||||
async (dc) => {
|
||||
const user = await min.userProfile.get(context, {});
|
||||
|
||||
const locale = dc.context.activity.locale;
|
||||
|
||||
if (!user.once) {
|
||||
user.once = true;
|
||||
await min.userProfile.set(context, user);
|
||||
var a = new Date();
|
||||
const date = a.getHours();
|
||||
var msg =
|
||||
|
|
|
@ -32,7 +32,7 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const { TextPrompt } = require("botbuilder-dialogs");
|
||||
const {DialogSet, TextPrompt } = require("botbuilder-dialogs");
|
||||
const UrlJoin = require("url-join");
|
||||
const express = require("express");
|
||||
const logger = require("../../../src/logger");
|
||||
|
@ -45,7 +45,8 @@ import {
|
|||
ConversationState,
|
||||
MemoryStorage,
|
||||
UserState,
|
||||
BotState
|
||||
AutoSaveStateMiddleware
|
||||
|
||||
} from "botbuilder";
|
||||
|
||||
import { GBMinInstance, IGBPackage } from "botlib";
|
||||
|
@ -303,11 +304,12 @@ export class GBMinService {
|
|||
appId: instance.marketplaceId,
|
||||
appPassword: instance.marketplacePassword
|
||||
});
|
||||
|
||||
|
||||
const storage = new MemoryStorage();
|
||||
const conversationState = new ConversationState(storage);
|
||||
const userState = new UserState(storage);
|
||||
|
||||
adapter.use(new AutoSaveStateMiddleware(conversationState, userState));
|
||||
|
||||
// The minimal bot is built here.
|
||||
|
||||
let min = new GBMinInstance();
|
||||
|
@ -318,7 +320,10 @@ export class GBMinService {
|
|||
min.conversationalService = this.conversationalService;
|
||||
min.adminService = this.adminService;
|
||||
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 };
|
||||
}
|
||||
|
@ -366,12 +371,12 @@ export class GBMinService {
|
|||
) {
|
||||
return adapter.processActivity(req, res, async 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.
|
||||
|
||||
try {
|
||||
const user = min.userState.get(dc.context);
|
||||
|
||||
const user = await min.userProfile.get(context, {});
|
||||
|
||||
if (!user.loaded) {
|
||||
await min.conversationalService.sendEvent(dc, "loadInstance", {
|
||||
instanceId: instance.instanceId,
|
||||
|
@ -381,6 +386,7 @@ export class GBMinService {
|
|||
});
|
||||
user.loaded = true;
|
||||
user.subjects = [];
|
||||
await min.userProfile.set(context, user);
|
||||
}
|
||||
|
||||
logger.info(
|
||||
|
@ -398,10 +404,9 @@ export class GBMinService {
|
|||
appPackages.forEach(e => {
|
||||
e.onNewSession(min, dc);
|
||||
});
|
||||
|
||||
// Processes the root dialog.
|
||||
|
||||
await dc.begin("/");
|
||||
await dc.beginDialog("/");
|
||||
} else {
|
||||
logger.info(`Member added to conversation: ${member.name}`);
|
||||
}
|
||||
|
@ -411,11 +416,11 @@ export class GBMinService {
|
|||
// Checks for /admin request.
|
||||
|
||||
if (context.activity.text === "admin") {
|
||||
await dc.begin("/admin");
|
||||
await dc.beginDialog("/admin");
|
||||
|
||||
// Checks for /menu JSON signature.
|
||||
} else if (context.activity.text.startsWith('{"title"')) {
|
||||
await dc.begin("/menu", {
|
||||
await dc.beginDialog("/menu", {
|
||||
data: JSON.parse(context.activity.text)
|
||||
});
|
||||
|
||||
|
@ -424,7 +429,7 @@ export class GBMinService {
|
|||
if (dc.activeDialog) {
|
||||
await dc.continue();
|
||||
} 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();
|
||||
|
||||
if (context.activity.name === "whoAmI") {
|
||||
await dc.begin("/whoAmI");
|
||||
await dc.beginDialog("/whoAmI");
|
||||
} else if (context.activity.name === "showSubjects") {
|
||||
await dc.begin("/menu");
|
||||
await dc.beginDialog("/menu");
|
||||
} else if (context.activity.name === "giveFeedback") {
|
||||
await dc.begin("/feedback", {
|
||||
await dc.beginDialog("/feedback", {
|
||||
fromMenu: true
|
||||
});
|
||||
} else if (context.activity.name === "showFAQ") {
|
||||
await dc.begin("/faq");
|
||||
await dc.beginDialog("/faq");
|
||||
} else if (context.activity.name === "answerEvent") {
|
||||
await dc.begin("/answerEvent", {
|
||||
await dc.beginDialog("/answerEvent", {
|
||||
questionId: (context.activity as any).data,
|
||||
fromFaq: true
|
||||
});
|
||||
} else if (context.activity.name === "quality") {
|
||||
await dc.begin("/quality", {
|
||||
await dc.beginDialog("/quality", {
|
||||
score: (context.activity as any).data
|
||||
});
|
||||
} else if (context.activity.name === "updateToken") {
|
||||
let token = (context.activity as any).data;
|
||||
await dc.begin("/adminUpdateToken", { token: token });
|
||||
await dc.beginDialog("/adminUpdateToken", { token: token });
|
||||
} else {
|
||||
await dc.continue();
|
||||
}
|
||||
|
@ -467,7 +472,7 @@ export class GBMinService {
|
|||
await dc.context.sendActivity(
|
||||
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) => {
|
||||
let locale = dc.context.activity.locale;
|
||||
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 dc.context.sendActivity(Messages[locale].thanks);
|
||||
}
|
||||
|
|
|
@ -53,7 +53,8 @@ export class QualityDialog extends IGBDialog {
|
|||
min.dialogs.add("/quality", [
|
||||
async (dc, args) => {
|
||||
const locale = dc.context.activity.locale;
|
||||
const user = min.userState.get(dc.context);
|
||||
const user = await min.userProfile.get(context, {});
|
||||
|
||||
var score = args.score;
|
||||
|
||||
setTimeout(
|
||||
|
|
|
@ -55,29 +55,29 @@ export class AskDialog extends IGBDialog {
|
|||
|
||||
min.dialogs.add("/answerEvent", [
|
||||
async (dc, args) => {
|
||||
|
||||
if (args && args.questionId) {
|
||||
let question = await service.getQuestionById(
|
||||
min.instance.instanceId,
|
||||
args.questionId
|
||||
);
|
||||
let answer = await service.getAnswerById(
|
||||
min.instance.instanceId,
|
||||
question.answerId
|
||||
);
|
||||
|
||||
let question = await service.getQuestionById(min.instance.instanceId, args.questionId);
|
||||
let answer = await service.getAnswerById(min.instance.instanceId, question.answerId)
|
||||
|
||||
// Sends the answer to all outputs, including projector.
|
||||
|
||||
await service.sendAnswer(
|
||||
min.conversationalService,
|
||||
dc,
|
||||
answer
|
||||
);
|
||||
await service.sendAnswer(min.conversationalService, dc, answer);
|
||||
|
||||
await dc.replace("/ask", { isReturning: true });
|
||||
}
|
||||
}])
|
||||
}
|
||||
]);
|
||||
|
||||
min.dialogs.add("/answer", [
|
||||
async (dc, args) => {
|
||||
// Initialize values.
|
||||
const user = await min.userProfile.get(context, {});
|
||||
|
||||
const user = min.userState.get(dc.context);
|
||||
let text = args.query;
|
||||
if (!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.
|
||||
|
||||
user.lastQuestion = text;
|
||||
await min.userProfile.set(context, user);
|
||||
let resultsA = await service.ask(
|
||||
min.instance,
|
||||
text,
|
||||
|
@ -128,6 +129,7 @@ export class AskDialog extends IGBDialog {
|
|||
|
||||
user.isAsking = false;
|
||||
user.lastQuestionId = resultsA.questionId;
|
||||
await min.userProfile.set(context, user);
|
||||
|
||||
// Sends the answer to all outputs, including projector.
|
||||
|
||||
|
@ -155,9 +157,11 @@ export class AskDialog extends IGBDialog {
|
|||
if (resultsB && resultsB.answer) {
|
||||
// Saves some context info.
|
||||
|
||||
const user = min.userState.get(dc.context);
|
||||
const user = await min.userProfile.get(context, {});
|
||||
|
||||
user.isAsking = false;
|
||||
user.lastQuestionId = resultsB.questionId;
|
||||
await min.userProfile.set(context, user);
|
||||
|
||||
// Informs user that a broader search will be used.
|
||||
|
||||
|
@ -189,7 +193,7 @@ export class AskDialog extends IGBDialog {
|
|||
min.dialogs.add("/ask", [
|
||||
async (dc, args) => {
|
||||
const locale = dc.context.activity.locale;
|
||||
const user = min.userState.get(dc.context);
|
||||
const user = await min.userProfile.get(context, {});
|
||||
user.isAsking = true;
|
||||
if (!user.subjects) {
|
||||
user.subjects = [];
|
||||
|
@ -214,7 +218,7 @@ export class AskDialog extends IGBDialog {
|
|||
},
|
||||
async (dc, value) => {
|
||||
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.
|
||||
|
||||
const user = min.userState.get(dc.context)
|
||||
const user = await min.userProfile.get(context, {});
|
||||
user.subjects.push(subject)
|
||||
rootSubjectId = subject.subjectId
|
||||
|
||||
|
@ -88,7 +88,7 @@ export class MenuDialog extends IGBDialog {
|
|||
})
|
||||
}
|
||||
} else {
|
||||
const user = min.userState.get(dc.context)
|
||||
const user = await min.userProfile.get(context, {});
|
||||
user.subjects = []
|
||||
|
||||
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) {
|
||||
const user = min.userState.get(dc.context)
|
||||
const user = await min.userProfile.get(context, {});
|
||||
|
||||
if (user.subjects && user.subjects.length > 0) {
|
||||
await dc.context.sendActivity(
|
||||
|
@ -153,7 +153,7 @@ export class MenuDialog extends IGBDialog {
|
|||
await dc.context.sendActivity(msg)
|
||||
}
|
||||
|
||||
const user = min.userState.get(dc.context)
|
||||
const user = await min.userProfile.get(context, {});
|
||||
user.isAsking = true
|
||||
},
|
||||
async (dc, value) => {
|
||||
|
|
Loading…
Add table
Reference in a new issue