Parameter whatsappServiceWebhookUrl added.

This commit is contained in:
Jorge Ramos 2018-05-14 01:48:39 -03:00
parent 1bc4b37827
commit 3b6edd4f3a
4 changed files with 183 additions and 179 deletions

View file

@ -33,27 +33,27 @@
"use strict"; "use strict";
import { import {
Sequelize, Sequelize,
DataTypes, DataTypes,
DataTypeUUIDv4, DataTypeUUIDv4,
DataTypeDate, DataTypeDate,
DataTypeDecimal DataTypeDecimal
} from "sequelize"; } from "sequelize";
import { import {
Table, Table,
Column, Column,
Model, Model,
HasMany, HasMany,
BelongsTo, BelongsTo,
BelongsToMany, BelongsToMany,
Length, Length,
ForeignKey, ForeignKey,
CreatedAt, CreatedAt,
UpdatedAt, UpdatedAt,
DataType, DataType,
IsUUID, IsUUID,
PrimaryKey, PrimaryKey,
AutoIncrement AutoIncrement
} from "sequelize-typescript"; } from "sequelize-typescript";
import { IGBInstance } from "botlib"; import { IGBInstance } from "botlib";
@ -61,132 +61,134 @@ import { IGBInstance } from "botlib";
@Table @Table
export class GuaribasInstance extends Model<GuaribasInstance> implements IGBInstance { export class GuaribasInstance extends Model<GuaribasInstance> implements IGBInstance {
@Column @Column
whoAmIVideo: string; whoAmIVideo: string;
@PrimaryKey @PrimaryKey
@AutoIncrement @AutoIncrement
@Column @Column
instanceId: number; instanceId: number;
@Column botId: string; @Column botId: string;
@Column title: string; @Column title: string;
@Column description: string; @Column description: string;
@Column version: string; @Column version: string;
@Column enabledAdmin: boolean; @Column enabledAdmin: boolean;
/* Services section on bot.json */ /* Services section on bot.json */
@Column engineName: string; @Column engineName: string;
@Column marketplaceId: string; @Column marketplaceId: string;
@Column textAnalyticsKey: string; @Column textAnalyticsKey: string;
@Column marketplacePassword: string; @Column marketplacePassword: string;
@Column webchatKey: string; @Column webchatKey: string;
@Column whatsappBotKey: string; @Column whatsappBotKey: string;
@Column whatsappServiceKey: string;
@Column whatsappServiceNumber: string; @Column whatsappServiceKey: string;
@Column whatsappServiceUrl: string; @Column whatsappServiceNumber: string;
@Column spellcheckerKey: string; @Column whatsappServiceUrl: string;
@Column theme: string; @Column whatsappServiceWebhookUrl: string;
@Column ui: string; @Column spellcheckerKey: string;
@Column kb: string; @Column theme: string;
@Column @Column ui: string;
@Column({ type: DataType.STRING(512) })
nlpServerUrl: string;
@Column searchHost: string; @Column kb: string;
@Column searchKey: string; @Column
@Column({ type: DataType.STRING(512) })
nlpServerUrl: string;
@Column searchIndex: string; @Column searchHost: string;
@Column searchIndexer: string; @Column searchKey: string;
/* Settings section of bot.json */ @Column searchIndex: string;
@Column({ @Column searchIndexer: string;
type: DataType.FLOAT
})
nlpVsSearch: number;
@Column({ /* Settings section of bot.json */
type: DataType.FLOAT
})
searchScore: number;
@Column({ @Column({
type: DataType.FLOAT type: DataType.FLOAT
}) })
nlpScore: number; nlpVsSearch: number;
@Column @Column({
@CreatedAt type: DataType.FLOAT
creationDate: Date; })
searchScore: number;
@Column @Column({
@UpdatedAt type: DataType.FLOAT
updatedOn: Date; })
nlpScore: number;
@Column
@CreatedAt
creationDate: Date;
@Column
@UpdatedAt
updatedOn: Date;
} }
@Table @Table
export class GuaribasPackage extends Model<GuaribasPackage> { export class GuaribasPackage extends Model<GuaribasPackage> {
@PrimaryKey @PrimaryKey
@AutoIncrement @AutoIncrement
@Column @Column
packageId: number; packageId: number;
@Column @Column
packageName: string; packageName: string;
@ForeignKey(() => GuaribasInstance) @ForeignKey(() => GuaribasInstance)
@Column @Column
instanceId: number; instanceId: number;
} }
@Table @Table
export class GuaribasChannel extends Model<GuaribasChannel> { export class GuaribasChannel extends Model<GuaribasChannel> {
@PrimaryKey @PrimaryKey
@AutoIncrement @AutoIncrement
@Column @Column
channelId: number; channelId: number;
@Column title: string; @Column title: string;
} }
@Table @Table
export class GuaribasException extends Model<GuaribasException> { export class GuaribasException extends Model<GuaribasException> {
@PrimaryKey @PrimaryKey
@AutoIncrement @AutoIncrement
@Column @Column
exceptionId: number; exceptionId: number;
@Column message: string; @Column message: string;
@ForeignKey(() => GuaribasInstance) @ForeignKey(() => GuaribasInstance)
@Column @Column
instanceId: number; instanceId: number;
@Column @Column
@CreatedAt @CreatedAt
creationDate: Date; creationDate: Date;
} }

View file

@ -41,80 +41,82 @@ import { GBCoreService } from "./GBCoreService";
import { Session, Message, LuisRecognizer } from "botbuilder"; import { Session, Message, LuisRecognizer } from "botbuilder";
import { GBService, GBServiceCallback, IGBConversationalService} from "botlib"; import { GBService, GBServiceCallback, IGBConversationalService } from "botlib";
import { GBError } from "botlib"; import { GBError } from "botlib";
import { GBERROR_TYPE } from "botlib"; import { GBERROR_TYPE } from "botlib";
import { GBMinInstance } from "botlib"; import { GBMinInstance } from "botlib";
export class GBConversationalService implements IGBConversationalService{ export class GBConversationalService implements IGBConversationalService {
coreService: GBCoreService;
constructor(coreService: GBCoreService) { coreService: GBCoreService;
this.coreService = coreService;
}
sendEvent(session: Session, name: string, value: any) { constructor(coreService: GBCoreService) {
var msg = new gBuilder.Message(); this.coreService = coreService;
msg.data.type = "event"; }
msg.data.name = name;
msg.data.value = value;
session.send(msg);
}
runNLP( sendEvent(session: Session, name: string, value: any) {
session: Session, var msg = new gBuilder.Message();
min: GBMinInstance, msg.data.type = "event";
text: string, msg.data.name = name;
cb: GBServiceCallback<any> msg.data.value = value;
) { session.send(msg);
LuisRecognizer.recognize( }
text,
min.instance.nlpServerUrl,
(err, intents, entities) => {
if (err) {
cb(null, new GBError(err, GBERROR_TYPE.nlpGeneralError));
return;
}
if (intents && intents.length > 0) { runNLP(
var intent = intents[0].intent; session: Session,
var entity = min: GBMinInstance,
entities && entities.length > 0 text: string,
? entities[0].entity.toUpperCase() cb: GBServiceCallback<any>
: null; ) {
logger.trace( LuisRecognizer.recognize(
"luis: intent: [" + intent + "] entity: [" + entity + "]" text,
); min.instance.nlpServerUrl,
(err, intents, entities) => {
if (err) {
cb(null, new GBError(err, GBERROR_TYPE.nlpGeneralError));
return;
}
// PACKAGE: Send to packages. if (intents && intents.length > 0) {
var intent = intents[0].intent;
var entity =
entities && entities.length > 0
? entities[0].entity.toUpperCase()
: null;
logger.trace(
"luis: intent: [" + intent + "] entity: [" + entity + "]"
);
if (intent === "Student.CheckAttendance") { // PACKAGE: Send to packages.
session.replaceDialog("/belagua-check-attendance", {entities: entities});
}
else if(intent === 'User.Authenticate'){
session.replaceDialog("/belagua-user-login", {entities: entities});
}
else if (intent === "PerguntarSobreTermo") {
session.send(
"Vou mostrar um menu para ajudar você a formular sua pergunta..."
);
session.replaceDialog("/menu");
} else if (intent === "ShowSubjectMenu") {
session.replaceDialog("/menu");
} else {
session.sendTyping();
session.send("Desculpe-me, não encontrei nada a respeito...");
}
cb({ intent, entities }, null); if (intent === "Student.CheckAttendance") {
} else { session.replaceDialog("/belagua-check-attendance", { entities: entities });
session.sendTyping(); }
session.send("Lamento, não achei nada a respeito..."); else if (intent === 'User.Authenticate') {
cb(null, null); session.replaceDialog("/belagua-user-login", { entities: entities });
} }
} else if (intent === "PerguntarSobreTermo") {
); session.send(
} "Vou mostrar um menu para ajudar você a formular sua pergunta..."
);
session.replaceDialog("/menu");
} else if (intent === "ShowSubjectMenu") {
session.replaceDialog("/menu");
} else {
// TODO testar diálogos v2
// inclui diálogo
session.sendTyping();
session.send("Desculpe-me, não encontrei nada a respeito...");
}
cb({ intent, entities }, null);
} else {
session.sendTyping();
session.send("Lamento, não achei nada a respeito...");
cb(null, null);
}
}
);
}
} }

View file

@ -42,26 +42,26 @@ import { WhatsappDirectLine } from "./services/WhatsappDirectLine";
export class GBWhatsappPackage implements IGBPackage { export class GBWhatsappPackage implements IGBPackage {
sysPackages: IGBPackage[] = null;
channel: WhatsappDirectLine;
loadPackage(core: IGBCoreService, sequelize: Sequelize): void { sysPackages: IGBPackage[] = null;
} channel: WhatsappDirectLine;
unloadPackage(core: IGBCoreService): void { loadPackage(core: IGBCoreService, sequelize: Sequelize): void {
}
} unloadPackage(core: IGBCoreService): void {
loadBot(min: GBMinInstance): void { }
this.channel = new WhatsappDirectLine(min.botId, min.instance.whatsappBotKey, min.instance.whatsappServiceKey,
min.instance.whatsappServiceNumber, min.instance.whatsappServiceUrl);
}
unloadBot(min: GBMinInstance): void { loadBot(min: GBMinInstance): void {
this.channel = new WhatsappDirectLine(min.botId, min.instance.whatsappBotKey, min.instance.whatsappServiceKey,
min.instance.whatsappServiceNumber, min.instance.whatsappServiceUrl, min.instance.whatsappServiceWebhookUrl);
}
} unloadBot(min: GBMinInstance): void {
onNewSession(min: GBMinInstance, session: Session): void {
} }
onNewSession(min: GBMinInstance, session: Session): void {
}
} }

View file

@ -54,12 +54,13 @@ export class WhatsappDirectLine extends GBService {
whatsappServiceKey: string; whatsappServiceKey: string;
whatsappServiceNumber: string; whatsappServiceNumber: string;
whatsappServiceUrl: string; whatsappServiceUrl: string;
whatsappServiceWebhookUrl: string;
botId: string; botId: string;
watermark: string = null; watermark: string = null;
conversationIds = {}; conversationIds = {};
constructor(botId, directLineSecret, whatsappServiceKey, whatsappServiceNumber, whatsappServiceUrl) { constructor(botId, directLineSecret, whatsappServiceKey, whatsappServiceNumber, whatsappServiceUrl, whatsappServiceWebhookUrl) {
super(); super();
@ -67,6 +68,7 @@ export class WhatsappDirectLine extends GBService {
this.whatsappServiceKey = whatsappServiceKey; this.whatsappServiceKey = whatsappServiceKey;
this.whatsappServiceNumber = whatsappServiceNumber; this.whatsappServiceNumber = whatsappServiceNumber;
this.whatsappServiceUrl = whatsappServiceUrl; this.whatsappServiceUrl = whatsappServiceUrl;
this.whatsappServiceWebhookUrl = whatsappServiceWebhookUrl;
// TODO: Migrate to Swagger 3. // TODO: Migrate to Swagger 3.
this.directLineClient = rp(this.directLineSpecUrl) this.directLineClient = rp(this.directLineSpecUrl)
@ -81,15 +83,13 @@ export class WhatsappDirectLine extends GBService {
new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' +
directLineSecret, 'header')); directLineSecret, 'header'));
var options = { var options = {
method: 'POST', method: 'POST',
url: UrlJoin(this.whatsappServiceUrl, "webhook"), url: UrlJoin(this.whatsappServiceUrl, "webhook"),
qs: qs:
{ {
token: this.whatsappServiceKey, token: this.whatsappServiceKey,
webhookUrl: `https://a2fc0900.ngrok.io/instances/${this.botId}/whatsapp` webhookUrl: `${this.whatsappServiceWebhookUrl}/instances/${this.botId}/whatsapp`
}, },
headers: headers:
{ {