botserver/packages/saas.gbapp/index.ts

149 lines
5.8 KiB
TypeScript
Raw Normal View History

2024-09-01 18:21:34 -03:00
/*****************************************************************************\
| ® |
| |
| |
| |
| |
| |
| General Bots Copyright (c) pragmatismo.com.br. All rights reserved. |
2024-09-01 18:21:34 -03:00
| 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.com.br. |
2024-09-01 18:21:34 -03:00
| 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. |
| |
\*****************************************************************************/
"use strict"
import { IGBPackage, GBMinInstance, IGBCoreService, GBLog, IGBAdminService, GBDialogStep } from 'botlib'
import { Sequelize } from 'sequelize-typescript'
2024-09-26 13:11:09 -03:00
import { GBOnlineSubscription } from './model/MainModel.js'
2024-09-01 18:21:34 -03:00
2024-09-26 13:11:09 -03:00
import { MSSubscriptionService } from './service/MSSubscription.js'
2024-09-01 18:21:34 -03:00
import { CollectionUtil } from 'pragmatismo-io-framework';
2024-09-26 13:11:09 -03:00
import { NewUserDialog } from './dialog/NewUserDialog.js'
import { GBOService } from './service/GBOService.js'
2024-09-01 18:21:34 -03:00
2024-09-26 13:11:09 -03:00
export class SaaSPackage implements IGBPackage {
2024-09-01 18:21:34 -03:00
sysPackages: IGBPackage[]
adminService: IGBAdminService;
public static welcomes = {};
instanceId: any
public getDialogs(min: GBMinInstance) {
return [NewUserDialog.getDialog(min),
NewUserDialog.getBotNameDialog(min),
NewUserDialog.getVoucherDialog(min),
NewUserDialog.getBotTemplateDialog(min),
NewUserDialog.getReturnFromPayment(min),
NewUserDialog.getReturnFromCC(min),
NewUserDialog.getReturnFromDocument(min),
NewUserDialog.getDialogBatch(min)
];
}
async loadPackage(core: IGBCoreService, sequelize: Sequelize): Promise<void> {
sequelize.addModels([GBOnlineSubscription]);
2024-09-01 18:21:34 -03:00
core.setEntryPointDialog('/welcome_saas');
// Installs webhook for Microsoft intercommunication.
core.installWebHook(true, '/mslanding', async (req, res) => {
const service = new MSSubscriptionService();
await service.handleMSLanding(req, res);
});
core.installWebHook(true, '/mshook', async (req, res) => {
const service = new MSSubscriptionService();
await service.handleMSHook(req, res);
});
core.installWebHook(true, '/signup', async (req, res) => {
const service = new MSSubscriptionService();
await service.handleMSSignUp(req, res);
});
}
/**
* Setups schedule to trigger notifications as pro-active messages.
*/
private setupScheduler(min, sendToDevice) {
const schedule = '30 09 * * 1-5';
const options = {
scheduled: true,
timezone: 'America/Sao_Paulo'
};
this.adminService = min.adminService;
this.instanceId = min.instanceId;
}
/**
* Called by scheduler to send notification message to phones.
* @param sendToDevice The function used to notify.
*/
private async notifyJob(sendToDevice) {
}
async unloadPackage(core: IGBCoreService): Promise<void> {
}
async loadBot(min: GBMinInstance): Promise<void> {
let gboService = new GBOService();
2024-09-01 18:21:34 -03:00
// Gets the sendToDevice method of whatsapp.gblib and setups scheduler.
2024-09-26 13:11:09 -03:00
if (min.whatsAppDirectLine !== undefined) {
2024-09-01 18:21:34 -03:00
const sendToDevice = min.whatsAppDirectLine.sendToDevice.bind(min.whatsAppDirectLine);
this.setupScheduler(min, sendToDevice);
this.notifyJob(sendToDevice);
}
}
async unloadBot(min: GBMinInstance): Promise<void> {
}
async onNewSession(min: GBMinInstance, step: GBDialogStep): Promise<void> {
}
public async onExchangeData(min: GBMinInstance, kind: string, data: any) {
switch (kind) {
case "whatsappMessage":
const from = data.from;
const fromName = data.fromName;
2024-09-26 13:11:09 -03:00
SaaSPackage.welcomes[from] = fromName;
2024-09-01 18:21:34 -03:00
break;
default:
GBLog.verbose('saas.gbapp onExchangeData called');
break;
}
}
}