From 16473952a199a1e2a190298e59a95fa2dc3ec890 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 19 Dec 2021 16:39:50 -0300 Subject: [PATCH] new(all): hubspot.gblib introduced. --- package-lock.json | 14 ++- package.json | 1 + .../basic.gblib/services/DialogKeywords.ts | 11 +++ packages/basic.gblib/services/GBVMService.ts | 7 ++ packages/core.gbapp/services/GBCoreService.ts | 4 +- packages/core.gbapp/services/GBDeployer.ts | 3 +- packages/hubspot.gblib/index.ts | 71 +++++++++++++ .../hubspot.gblib/services/HubSpotServices.ts | 99 +++++++++++++++++++ packages/hubspot.gblib/strings.ts | 8 ++ src/app.ts | 2 +- 10 files changed, 215 insertions(+), 5 deletions(-) create mode 100644 packages/hubspot.gblib/index.ts create mode 100644 packages/hubspot.gblib/services/HubSpotServices.ts create mode 100644 packages/hubspot.gblib/strings.ts diff --git a/package-lock.json b/package-lock.json index 34a002809..05417af19 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1768,6 +1768,17 @@ } } }, + "@hubspot/api-client": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/@hubspot/api-client/-/api-client-5.0.0.tgz", + "integrity": "sha512-ywgS7A79v9Y2Wyc3CWJ6dF1i3iEK+9tsdYdt48osQkW8ypvby7PvcHJV5WTkvdkYXAXTcWjGJoji06JCcSAqgQ==", + "requires": { + "bluebird": "^3.7.2", + "bottleneck": "^2.19.5", + "lodash": "^4.17.21", + "request": "^2.88.0" + } + }, "@iarna/toml": { "version": "2.2.3", "resolved": "https://registry.npmjs.org/@iarna/toml/-/toml-2.2.3.tgz", @@ -5333,8 +5344,7 @@ "bottleneck": { "version": "2.19.5", "resolved": "https://registry.npmjs.org/bottleneck/-/bottleneck-2.19.5.tgz", - "integrity": "sha512-VHiNCbI1lKdl44tGrhNfU3lup0Tj/ZBMJB5/2ZbNXRCPuRCO7ed2mgcK4r17y+KB2EfuYuRaVlwNbAeaWGSpbw==", - "dev": true + "integrity": "sha512-VHiNCbI1lKdl44tGrhNfU3lup0Tj/ZBMJB5/2ZbNXRCPuRCO7ed2mgcK4r17y+KB2EfuYuRaVlwNbAeaWGSpbw==" }, "boxen": { "version": "1.3.0", diff --git a/package.json b/package.json index 5f749389d..f95a3453f 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "@azure/ms-rest-js": "2.5.1", "@google-cloud/pubsub": "2.13.0", "@google-cloud/translate": "6.2.6", + "@hubspot/api-client": "5.0.0", "@microsoft/microsoft-graph-client": "2.2.1", "@semantic-release/changelog": "5.0.1", "@semantic-release/exec": "5.0.0", diff --git a/packages/basic.gblib/services/DialogKeywords.ts b/packages/basic.gblib/services/DialogKeywords.ts index 46514c6b4..70932d703 100644 --- a/packages/basic.gblib/services/DialogKeywords.ts +++ b/packages/basic.gblib/services/DialogKeywords.ts @@ -40,6 +40,7 @@ import { GBDeployer } from '../../core.gbapp/services/GBDeployer'; import { SecService } from '../../security.gbapp/services/SecService'; import { SystemKeywords } from './SystemKeywords'; import { GBMinService } from '../../core.gbapp/services/GBMinService'; +import { HubSpotServices } from '../../hubspot.gblib/services/HubSpotServices'; var DateDiff = require('date-diff'); @@ -123,6 +124,16 @@ export class DialogKeywords { await step.endDialog(); } + /** + * Quits the dialog, currently required to get out of VM context. + * + * @example list = ACTIVE TASKS + */ + public async getActiveTasks (){ + let s = new HubSpotServices(null, null, 'ddcc3f4e-edfe-4dc5-a337-3d39bcc2d833'); + return await s.getActiveTasks(); + } + public getContentLocaleWithCulture(contentLocale) { switch (contentLocale) { case 'pt': diff --git a/packages/basic.gblib/services/GBVMService.ts b/packages/basic.gblib/services/GBVMService.ts index 04c0df886..85b9a8c7a 100644 --- a/packages/basic.gblib/services/GBVMService.ts +++ b/packages/basic.gblib/services/GBVMService.ts @@ -264,6 +264,10 @@ export class GBVMService extends GBService { return `${$1} = sys().find(${$2})\n`; }); + code = code.replace(/(\w)\s*\=\s*active tasks/gi, ($0, $1) => { + return `${$1} = getActiveTasks()\n`; + }); + code = code.replace(/(\w)\s*\=\s*append\s*(.*)/gi, ($0, $1, $2, $3) => { return `${$1} = sys().append(${$2})\n`; }); @@ -565,6 +569,9 @@ export class GBVMService extends GBService { code = code.replace(/("[^"]*"|'[^']*')|\btransfer\b/gi, ($0, $1) => { return $1 === undefined ? 'this.transfer' : $1; }); + code = code.replace(/("[^"]*"|'[^']*')|\bgetActiveTasks\b/gi, ($0, $1) => { + return $1 === undefined ? 'this.getActiveTasks' : $1; + }); code = code.replace(/("[^"]*"|'[^']*')|\bmenu\b/gi, ($0, $1) => { return $1 === undefined ? 'this.menu' : $1; }); diff --git a/packages/core.gbapp/services/GBCoreService.ts b/packages/core.gbapp/services/GBCoreService.ts index 33ad98ce4..755a164a0 100644 --- a/packages/core.gbapp/services/GBCoreService.ts +++ b/packages/core.gbapp/services/GBCoreService.ts @@ -57,6 +57,7 @@ import { GBSharePointPackage } from '../../sharepoint.gblib'; import { CollectionUtil } from 'pragmatismo-io-framework'; import { GBBasicPackage } from '../../basic.gblib'; import { GBGoogleChatPackage } from '../../google-chat.gblib'; +import { GBHubSpotPackage } from '../../hubspot.gblib'; const opn = require('opn'); const cron = require('node-cron'); @@ -462,7 +463,8 @@ ENDPOINT_UPDATE=true GBAzureDeployerPackage, GBSharePointPackage, GBGoogleChatPackage, - GBBasicPackage + GBBasicPackage, + GBHubSpotPackage ], async e => { GBLog.info(`Loading sys package: ${e.name}...`); diff --git a/packages/core.gbapp/services/GBDeployer.ts b/packages/core.gbapp/services/GBDeployer.ts index 4061a3e9c..4b7691d04 100644 --- a/packages/core.gbapp/services/GBDeployer.ts +++ b/packages/core.gbapp/services/GBDeployer.ts @@ -915,7 +915,8 @@ export class GBDeployer implements IGBDeployer { 'customer-satisfaction.gbapp', 'kb.gbapp', 'google-chat.gblib', - 'teams.gblib' + 'teams.gblib', + 'hubspot.gblib' ]; return names.indexOf(name) > -1; diff --git a/packages/hubspot.gblib/index.ts b/packages/hubspot.gblib/index.ts new file mode 100644 index 000000000..edd62096c --- /dev/null +++ b/packages/hubspot.gblib/index.ts @@ -0,0 +1,71 @@ +/*****************************************************************************\ +| ( )_ _ | +| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ | +| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' v `\ /'_`\ | +| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| (˅) |( (_) ) | +| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' | +| | | ( )_) | | +| (_) \___/' | +| | +| General Bots Copyright (c) Pragmatismo.io. All rights reserved. | +| Licensed under the AGPL-3.0. | +| | +| According to our dual licensing model, this program can be used either | +| under the terms of the GNU Affero General Public License, version 3, | +| or under a proprietary license. | +| | +| The texts of the GNU Affero General Public License with an additional | +| permission and of our proprietary license can be found at and | +| in the LICENSE file you have received along with this program. | +| | +| This program is distributed in the hope that it will be useful, | +| but WITHOUT ANY WARRANTY, without even the implied warranty of | +| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | +| GNU Affero General Public License for more details. | +| | +| "General Bots" is a registered trademark of Pragmatismo.io. | +| The licensing of the program under the AGPLv3 does not imply a | +| trademark license. Therefore any rights, title and interest in | +| our trademarks remain entirely with us. | +| | +\*****************************************************************************/ + +/** + * @fileoverview General Bots server core. + */ + +'use strict'; + +import { GBDialogStep, GBLog, GBMinInstance, IGBCoreService, IGBPackage } from 'botlib'; +import { Sequelize } from 'sequelize-typescript'; + +/** + * Package for GoogleChat.gblib + */ +export class GBHubSpotPackage implements IGBPackage { + public sysPackages: IGBPackage[]; + + public async loadBot(min: GBMinInstance): Promise { + + } + + public async getDialogs(min: GBMinInstance) { + GBLog.verbose(`getDialogs called.`); + } + public async loadPackage(core: IGBCoreService, sequelize: Sequelize): Promise { + GBLog.verbose(`loadPackage called.`); + } + public async unloadPackage(core: IGBCoreService): Promise { + GBLog.verbose(`unloadPackage called.`); + } + public async unloadBot(min: GBMinInstance): Promise { + GBLog.verbose(`unloadBot called.`); + } + public async onNewSession(min: GBMinInstance, step: GBDialogStep): Promise { + GBLog.verbose(`onNewSession called.`); + } + public async onExchangeData(min: GBMinInstance, kind: string, data: any) { + GBLog.verbose(`onExchangeData called.`); + } + +} diff --git a/packages/hubspot.gblib/services/HubSpotServices.ts b/packages/hubspot.gblib/services/HubSpotServices.ts new file mode 100644 index 000000000..e142bbf80 --- /dev/null +++ b/packages/hubspot.gblib/services/HubSpotServices.ts @@ -0,0 +1,99 @@ +/*****************************************************************************\ +| ( )_ _ | +| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ | +| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' v `\ /'_`\ | +| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| (˅) |( (_) ) | +| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' | +| | | ( )_) | | +| (_) \___/' | +| | +| General Bots Copyright (c) Pragmatismo.io. All rights reserved. | +| Licensed under the AGPL-3.0. | +| | +| According to our dual licensing model, this program can be used either | +| under the terms of the GNU Affero General Public License, version 3, | +| or under a proprietary license. | +| | +| The texts of the GNU Affero General Public License with an additional | +| permission and of our proprietary license can be found at and | +| in the LICENSE file you have received along with this program. | +| | +| This program is distributed in the hope that it will be useful, | +| but WITHOUT ANY WARRANTY, without even the implied warranty of | +| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | +| GNU Affero General Public License for more details. | +| | +| "General Bots" is a registered trademark of Pragmatismo.io. | +| The licensing of the program under the AGPLv3 does not imply a | +| trademark license. Therefore any rights, title and interest in | +| our trademarks remain entirely with us. | +| | +\*****************************************************************************/ + +const Swagger = require('swagger-client'); +const fs = require('fs'); +const { promisify } = require('util'); +import { GBLog, GBMinInstance, GBService } from 'botlib'; +import { GBServer } from '../../../src/app'; +import { SecService } from '../../security.gbapp/services/SecService'; + +/** + * Support for Hub Spot XRM. + */ +export class HubSpotServices extends GBService { + + public static conversationIds = {}; + public pollInterval = 5000; + + public botId: string; + public min: GBMinInstance; + private key: any; + + constructor( + min: GBMinInstance, + botId, + key + ) { + super(); + + this.min = min; + this.botId = botId; + this.key = key; + + } + public static async asyncForEach(array, callback) { + for (let index = 0; index < array.length; index++) { + await callback(array[index], index, array); + } + } + + public async getActiveTasks(): Promise<[]> { + + const hubspot = require('@hubspot/api-client'); + const client = new hubspot.Client({ apiKey: this.key }); + let properties = ['hs_task_subject', 'hubspot_owner_id', 'hs_task_status', 'hs_task_priority']; + const pageSize = 100; + let list; + list = []; + + let r = await client.crm.objects.basicApi.getPage("TASK", pageSize, 0, properties); + list = list.concat(r.body.results); + + while (r.body.results && r.body.results.length === pageSize) { + r = await client.crm.objects.basicApi.getPage("TASK", pageSize, r.body.paging.next.after, properties); + list = list.concat(r.body.results); + } + + let final; + final = []; + list.forEach(e => { + if (e.properties.hs_task_status === "NOT_STARTED") + { + final.push(e); + } + }); + + + return final; + } +} \ No newline at end of file diff --git a/packages/hubspot.gblib/strings.ts b/packages/hubspot.gblib/strings.ts new file mode 100644 index 000000000..d30f544b1 --- /dev/null +++ b/packages/hubspot.gblib/strings.ts @@ -0,0 +1,8 @@ +export const Messages = { + 'en-US': { + + }, + 'pt-BR': { + + } +}; diff --git a/src/app.ts b/src/app.ts index 05d312f39..846629aa5 100644 --- a/src/app.ts +++ b/src/app.ts @@ -67,7 +67,6 @@ export class RootData { public wwwroot: string; // .gbui or a static webapp. public entryPointDialog: string; // To replace default welcome dialog. } - /** * General Bots open-core entry point. */ @@ -106,6 +105,7 @@ export class GBServer { server.listen(port, () => { (async () => { + try { GBLog.info(`Now accepting connections on ${port}...`);