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 whatsappServiceKey: string;
@Column whatsappServiceNumber: string; @Column whatsappServiceNumber: string;
@Column whatsappServiceUrl: string; @Column whatsappServiceUrl: string;
@Column spellcheckerKey: string; @Column whatsappServiceWebhookUrl: string;
@Column theme: string; @Column spellcheckerKey: string;
@Column ui: string; @Column theme: string;
@Column kb: string; @Column ui: string;
@Column @Column kb: string;
@Column({ type: DataType.STRING(512) })
nlpServerUrl: string;
@Column searchHost: string; @Column
@Column({ type: DataType.STRING(512) })
nlpServerUrl: string;
@Column searchKey: string; @Column searchHost: string;
@Column searchIndex: string; @Column searchKey: string;
@Column searchIndexer: string; @Column searchIndex: string;
/* Settings section of bot.json */ @Column searchIndexer: string;
@Column({ /* Settings section of bot.json */
type: DataType.FLOAT
})
nlpVsSearch: number;
@Column({ @Column({
type: DataType.FLOAT type: DataType.FLOAT
}) })
searchScore: number; nlpVsSearch: number;
@Column({ @Column({
type: DataType.FLOAT type: DataType.FLOAT
}) })
nlpScore: number; searchScore: number;
@Column @Column({
@CreatedAt type: DataType.FLOAT
creationDate: Date; })
nlpScore: number;
@Column @Column
@UpdatedAt @CreatedAt
updatedOn: Date; 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; coreService: GBCoreService;
constructor(coreService: GBCoreService) { constructor(coreService: GBCoreService) {
this.coreService = coreService; this.coreService = coreService;
} }
sendEvent(session: Session, name: string, value: any) { sendEvent(session: Session, name: string, value: any) {
var msg = new gBuilder.Message(); var msg = new gBuilder.Message();
msg.data.type = "event"; msg.data.type = "event";
msg.data.name = name; msg.data.name = name;
msg.data.value = value; msg.data.value = value;
session.send(msg); session.send(msg);
} }
runNLP( runNLP(
session: Session, session: Session,
min: GBMinInstance, min: GBMinInstance,
text: string, text: string,
cb: GBServiceCallback<any> cb: GBServiceCallback<any>
) { ) {
LuisRecognizer.recognize( LuisRecognizer.recognize(
text, text,
min.instance.nlpServerUrl, min.instance.nlpServerUrl,
(err, intents, entities) => { (err, intents, entities) => {
if (err) { if (err) {
cb(null, new GBError(err, GBERROR_TYPE.nlpGeneralError)); cb(null, new GBError(err, GBERROR_TYPE.nlpGeneralError));
return; return;
} }
if (intents && intents.length > 0) { if (intents && intents.length > 0) {
var intent = intents[0].intent; var intent = intents[0].intent;
var entity = var entity =
entities && entities.length > 0 entities && entities.length > 0
? entities[0].entity.toUpperCase() ? entities[0].entity.toUpperCase()
: null; : null;
logger.trace( logger.trace(
"luis: intent: [" + intent + "] entity: [" + entity + "]" "luis: intent: [" + intent + "] entity: [" + entity + "]"
); );
// PACKAGE: Send to packages. // PACKAGE: Send to packages.
if (intent === "Student.CheckAttendance") { if (intent === "Student.CheckAttendance") {
session.replaceDialog("/belagua-check-attendance", {entities: entities}); session.replaceDialog("/belagua-check-attendance", { entities: entities });
} }
else if(intent === 'User.Authenticate'){ else if (intent === 'User.Authenticate') {
session.replaceDialog("/belagua-user-login", {entities: entities}); session.replaceDialog("/belagua-user-login", { entities: entities });
} }
else if (intent === "PerguntarSobreTermo") { else if (intent === "PerguntarSobreTermo") {
session.send( session.send(
"Vou mostrar um menu para ajudar você a formular sua pergunta..." "Vou mostrar um menu para ajudar você a formular sua pergunta..."
); );
session.replaceDialog("/menu"); session.replaceDialog("/menu");
} else if (intent === "ShowSubjectMenu") { } else if (intent === "ShowSubjectMenu") {
session.replaceDialog("/menu"); session.replaceDialog("/menu");
} else { } else {
session.sendTyping(); // TODO testar diálogos v2
session.send("Desculpe-me, não encontrei nada a respeito..."); // inclui diálogo
} session.sendTyping();
session.send("Desculpe-me, não encontrei nada a respeito...");
}
cb({ intent, entities }, null); cb({ intent, entities }, null);
} else { } else {
session.sendTyping(); session.sendTyping();
session.send("Lamento, não achei nada a respeito..."); session.send("Lamento, não achei nada a respeito...");
cb(null, null); cb(null, null);
} }
} }
); );
} }
} }

View file

@ -43,25 +43,25 @@ import { WhatsappDirectLine } from "./services/WhatsappDirectLine";
export class GBWhatsappPackage implements IGBPackage { export class GBWhatsappPackage implements IGBPackage {
sysPackages: IGBPackage[] = null; sysPackages: IGBPackage[] = null;
channel: WhatsappDirectLine; channel: WhatsappDirectLine;
loadPackage(core: IGBCoreService, sequelize: Sequelize): void { loadPackage(core: IGBCoreService, sequelize: Sequelize): void {
} }
unloadPackage(core: IGBCoreService): void { unloadPackage(core: IGBCoreService): void {
} }
loadBot(min: GBMinInstance): void { loadBot(min: GBMinInstance): void {
this.channel = new WhatsappDirectLine(min.botId, min.instance.whatsappBotKey, min.instance.whatsappServiceKey, this.channel = new WhatsappDirectLine(min.botId, min.instance.whatsappBotKey, min.instance.whatsappServiceKey,
min.instance.whatsappServiceNumber, min.instance.whatsappServiceUrl); min.instance.whatsappServiceNumber, min.instance.whatsappServiceUrl, min.instance.whatsappServiceWebhookUrl);
} }
unloadBot(min: GBMinInstance): void { 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:
{ {