@wabot-dev/framework 0.1.0-beta.2 → 0.1.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/ai/deepseek/DeepSeekChatBotAdapter.js +3 -5
- package/dist/src/ai/openia/OpenaiChatBotAdapter.js +3 -5
- package/dist/src/channels/cmd/@cmd.js +0 -4
- package/dist/src/channels/cmd/CmdChannel.js +0 -4
- package/dist/src/channels/express/ExpressProvider.js +46 -0
- package/dist/src/channels/http/HttpServerProvider.js +31 -0
- package/dist/src/channels/{socket-io → socket}/@socket.js +0 -4
- package/dist/src/channels/{socket-io → socket}/SocketChannel.js +12 -11
- package/dist/src/channels/socket/SocketServerProvider.js +50 -0
- package/dist/src/channels/telegram/@telegram.js +0 -4
- package/dist/src/channels/telegram/TelegramChannel.js +0 -4
- package/dist/src/channels/wabot/WabotDevConnection.js +57 -0
- package/dist/src/channels/{whatsapp/WhatsAppDevSocketContracts.js → wabot/WabotDevSocketContracts.js} +3 -3
- package/dist/src/channels/whatsapp/@whatsapp.js +0 -4
- package/dist/src/channels/whatsapp/EnvWhatsAppRepository.js +49 -0
- package/dist/src/channels/whatsapp/PgWhatsAppRepository.js +41 -0
- package/dist/src/channels/whatsapp/WhatsApp.js +32 -0
- package/dist/src/channels/whatsapp/WhatsAppChannel.js +33 -32
- package/dist/src/channels/whatsapp/{WhatsAppConnection.js → WhatsAppReceiver.js} +4 -6
- package/dist/src/channels/whatsapp/WhatsAppReceiverByDevConnection.js +32 -0
- package/dist/src/channels/whatsapp/WhatsAppReceiverByWebHook.js +63 -0
- package/dist/src/channels/whatsapp/WhatsAppRepository.js +10 -0
- package/dist/src/channels/whatsapp/WhatsAppSender.js +76 -17
- package/dist/src/channels/whatsapp/WhatsAppSenderByCloudApi.js +124 -0
- package/dist/src/channels/whatsapp/WhatsAppSenderByDevConnection.js +61 -0
- package/dist/src/chatbot/ChatBotAdapter.js +1 -5
- package/dist/src/chatbot/metadata/@chatBot.js +0 -5
- package/dist/src/chatbot/metadata/ChatBotMetadataStore.js +0 -5
- package/dist/src/controller/metadata/ControllerMetadataStore.js +0 -2
- package/dist/src/controller/metadata/controller/@chatController.js +0 -2
- package/dist/src/core/IMessageContext.js +0 -2
- package/dist/src/env/WabotEnv.js +6 -0
- package/dist/src/index.d.ts +270 -202
- package/dist/src/index.js +28 -27
- package/dist/src/mindset/MindsetOperator.js +0 -2
- package/dist/src/mindset/metadata/MindsetMetadataStore.js +0 -2
- package/dist/src/mindset/metadata/mindsets/@mindset.js +0 -2
- package/dist/src/mindset/metadata/modules/@mindsetModule.js +0 -2
- package/dist/src/pre-made/module/authentication/requests/SendOneTimePasswordRequest.js +0 -2
- package/dist/src/pre-made/module/authentication/requests/ValidateOneTimePasswordRequest.js +0 -2
- package/dist/src/pre-made/module/register-user/requests/RegisterUserWithEmailRequest.js +0 -2
- package/dist/src/pre-made/repository/chat/pg/PgChatMemory.js +1 -7
- package/dist/src/pre-made/repository/chat/pg/PgChatRepository.js +0 -5
- package/dist/src/pre-made/repository/user/pg/PgUserRepository.js +0 -5
- package/dist/src/repository/pg/PgCrudRepository.js +0 -1
- package/dist/src/server/prepareChatContainer.js +4 -4
- package/dist/src/server/runChannel.js +2 -10
- package/dist/src/server/runServer.js +0 -43
- package/package.json +2 -4
- package/dist/src/channels/whatsapp/WhatsAppDevConnection.js +0 -69
- package/dist/src/channels/whatsapp/WhatsAppProdConnection.js +0 -39
- package/dist/src/controller/express/Express.js +0 -5
- package/dist/src/controller/socket.io/SocketIO.js +0 -5
- package/dist/src/pre-made/repository/chat/sqlite/SqliteChatMemory.js +0 -23
- package/dist/src/pre-made/repository/chat/sqlite/SqliteChatRepository.js +0 -30
- package/dist/src/pre-made/repository/user/sqlite/SqliteUserRepository.js +0 -23
- package/dist/src/repository/sqlite/SqliteCrudRepository.js +0 -85
- package/dist/src/repository/sqlite/SqlitePersistentMapper.js +0 -17
- /package/dist/src/channels/{socket-io → socket}/SocketChannelConfig.js +0 -0
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import { __decorate, __metadata } from 'tslib';
|
|
2
2
|
import { injectable } from '../../injection/index.js';
|
|
3
3
|
import { OpenAI } from 'openai';
|
|
4
|
-
import 'reflect-metadata';
|
|
5
|
-
import '../../mindset/metadata/MindsetMetadataStore.js';
|
|
6
|
-
import '../../core/chat/repository/IChatRepository.js';
|
|
7
|
-
import '../../core/user/IUserRepository.js';
|
|
8
|
-
import { MindsetOperator } from '../../mindset/MindsetOperator.js';
|
|
9
4
|
import 'uuid';
|
|
10
5
|
import '../../chatbot/metadata/ChatBotMetadataStore.js';
|
|
11
6
|
import '../../chatbot/ChatBot.js';
|
|
12
7
|
import { ChatBotAdapter } from '../../chatbot/ChatBotAdapter.js';
|
|
8
|
+
import 'reflect-metadata';
|
|
9
|
+
import '../../mindset/metadata/MindsetMetadataStore.js';
|
|
10
|
+
import { MindsetOperator } from '../../mindset/MindsetOperator.js';
|
|
13
11
|
|
|
14
12
|
let DeepSeekChatBotAdapter = class DeepSeekChatBotAdapter extends ChatBotAdapter {
|
|
15
13
|
deepSeek;
|
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import { __decorate, __metadata } from 'tslib';
|
|
2
2
|
import { injectable } from '../../injection/index.js';
|
|
3
3
|
import { OpenAI } from 'openai';
|
|
4
|
-
import 'reflect-metadata';
|
|
5
|
-
import '../../mindset/metadata/MindsetMetadataStore.js';
|
|
6
|
-
import '../../core/chat/repository/IChatRepository.js';
|
|
7
|
-
import '../../core/user/IUserRepository.js';
|
|
8
|
-
import { MindsetOperator } from '../../mindset/MindsetOperator.js';
|
|
9
4
|
import 'uuid';
|
|
10
5
|
import '../../chatbot/metadata/ChatBotMetadataStore.js';
|
|
11
6
|
import '../../chatbot/ChatBot.js';
|
|
12
7
|
import { ChatBotAdapter } from '../../chatbot/ChatBotAdapter.js';
|
|
8
|
+
import 'reflect-metadata';
|
|
9
|
+
import '../../mindset/metadata/MindsetMetadataStore.js';
|
|
10
|
+
import { MindsetOperator } from '../../mindset/MindsetOperator.js';
|
|
13
11
|
|
|
14
12
|
let OpenaiChatBotAdapter = class OpenaiChatBotAdapter extends ChatBotAdapter {
|
|
15
13
|
openai = new OpenAI();
|
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import '../../controller/channel/ChatResolver.js';
|
|
2
2
|
import '../../controller/channel/UserResolver.js';
|
|
3
3
|
import { container } from '../../injection/index.js';
|
|
4
|
-
import '../../core/chat/repository/IChatRepository.js';
|
|
5
|
-
import '../../core/user/IUserRepository.js';
|
|
6
4
|
import { ControllerMetadataStore } from '../../controller/metadata/ControllerMetadataStore.js';
|
|
7
|
-
import 'express';
|
|
8
|
-
import 'socket.io';
|
|
9
5
|
import { CmdChannel } from './CmdChannel.js';
|
|
10
6
|
|
|
11
7
|
function cmd() {
|
|
@@ -2,11 +2,7 @@ import { __decorate, __metadata } from 'tslib';
|
|
|
2
2
|
import { ChatResolver } from '../../controller/channel/ChatResolver.js';
|
|
3
3
|
import { UserResolver } from '../../controller/channel/UserResolver.js';
|
|
4
4
|
import { injectable } from '../../injection/index.js';
|
|
5
|
-
import '../../core/chat/repository/IChatRepository.js';
|
|
6
|
-
import '../../core/user/IUserRepository.js';
|
|
7
5
|
import '../../controller/metadata/ControllerMetadataStore.js';
|
|
8
|
-
import 'express';
|
|
9
|
-
import 'socket.io';
|
|
10
6
|
import * as readline from 'readline';
|
|
11
7
|
|
|
12
8
|
var CmdChannel_1;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { HttpServerProvider } from '../http/HttpServerProvider.js';
|
|
3
|
+
import express from 'express';
|
|
4
|
+
import { singleton } from 'tsyringe';
|
|
5
|
+
import bodyParser from 'body-parser';
|
|
6
|
+
import { Logger } from '../../logger/Logger.js';
|
|
7
|
+
|
|
8
|
+
let ExpressProvider = class ExpressProvider {
|
|
9
|
+
httpServerProvider;
|
|
10
|
+
expressApp = null;
|
|
11
|
+
logger = new Logger('wabot:express');
|
|
12
|
+
constructor(httpServerProvider) {
|
|
13
|
+
this.httpServerProvider = httpServerProvider;
|
|
14
|
+
}
|
|
15
|
+
getExpress() {
|
|
16
|
+
if (!this.expressApp) {
|
|
17
|
+
this.expressApp = this.createExpress();
|
|
18
|
+
}
|
|
19
|
+
return this.expressApp;
|
|
20
|
+
}
|
|
21
|
+
listen() {
|
|
22
|
+
this.httpServerProvider.listen();
|
|
23
|
+
}
|
|
24
|
+
createExpress() {
|
|
25
|
+
const expressApp = express();
|
|
26
|
+
expressApp.use(bodyParser.json());
|
|
27
|
+
expressApp.use((req, res, next) => {
|
|
28
|
+
const start = process.hrtime();
|
|
29
|
+
res.on('finish', () => {
|
|
30
|
+
const [seconds, nanoseconds] = process.hrtime(start);
|
|
31
|
+
const ms = (seconds * 1000 + nanoseconds / 1e6).toFixed(2);
|
|
32
|
+
this.logger.trace(`${req.method} ${req.originalUrl} ${res.statusCode} - ${ms}ms`);
|
|
33
|
+
});
|
|
34
|
+
next();
|
|
35
|
+
});
|
|
36
|
+
const httpServer = this.httpServerProvider.getHttpServer();
|
|
37
|
+
httpServer.on('request', expressApp);
|
|
38
|
+
return expressApp;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
ExpressProvider = __decorate([
|
|
42
|
+
singleton(),
|
|
43
|
+
__metadata("design:paramtypes", [HttpServerProvider])
|
|
44
|
+
], ExpressProvider);
|
|
45
|
+
|
|
46
|
+
export { ExpressProvider };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { __decorate } from 'tslib';
|
|
2
|
+
import { Logger } from '../../logger/Logger.js';
|
|
3
|
+
import { Server } from 'http';
|
|
4
|
+
import { singleton } from 'tsyringe';
|
|
5
|
+
|
|
6
|
+
let HttpServerProvider = class HttpServerProvider {
|
|
7
|
+
server = null;
|
|
8
|
+
listening = false;
|
|
9
|
+
logger = new Logger('wabot:http');
|
|
10
|
+
getHttpServer() {
|
|
11
|
+
if (!this.server) {
|
|
12
|
+
this.server = new Server();
|
|
13
|
+
}
|
|
14
|
+
return this.server;
|
|
15
|
+
}
|
|
16
|
+
listen() {
|
|
17
|
+
if (!this.server || this.listening) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
this.listening = true;
|
|
21
|
+
const PORT = process.env.PORT || 3000;
|
|
22
|
+
this.server.listen(PORT, () => {
|
|
23
|
+
this.logger.info(`server listenig on port ${PORT}`);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
HttpServerProvider = __decorate([
|
|
28
|
+
singleton()
|
|
29
|
+
], HttpServerProvider);
|
|
30
|
+
|
|
31
|
+
export { HttpServerProvider };
|
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import '../../controller/channel/ChatResolver.js';
|
|
2
2
|
import '../../controller/channel/UserResolver.js';
|
|
3
3
|
import { container } from '../../injection/index.js';
|
|
4
|
-
import '../../core/chat/repository/IChatRepository.js';
|
|
5
|
-
import '../../core/user/IUserRepository.js';
|
|
6
4
|
import { ControllerMetadataStore } from '../../controller/metadata/ControllerMetadataStore.js';
|
|
7
|
-
import 'express';
|
|
8
|
-
import 'socket.io';
|
|
9
5
|
import { SocketChannel } from './SocketChannel.js';
|
|
10
6
|
import { SocketChannelConfig } from './SocketChannelConfig.js';
|
|
11
7
|
|
|
@@ -1,26 +1,25 @@
|
|
|
1
|
-
import { __decorate,
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
2
|
import { SocketChannelConfig } from './SocketChannelConfig.js';
|
|
3
|
-
import { injectable
|
|
3
|
+
import { injectable } from '../../injection/index.js';
|
|
4
4
|
import { ChatResolver } from '../../controller/channel/ChatResolver.js';
|
|
5
5
|
import { UserResolver } from '../../controller/channel/UserResolver.js';
|
|
6
|
-
import '../../core/chat/repository/IChatRepository.js';
|
|
7
|
-
import '../../core/user/IUserRepository.js';
|
|
8
6
|
import '../../controller/metadata/ControllerMetadataStore.js';
|
|
9
|
-
import '
|
|
10
|
-
import { SocketIoApp } from '../../controller/socket.io/SocketIO.js';
|
|
7
|
+
import { SocketServerProvider } from './SocketServerProvider.js';
|
|
11
8
|
|
|
12
9
|
var SocketChannel_1;
|
|
13
10
|
let SocketChannel = SocketChannel_1 = class SocketChannel {
|
|
14
11
|
config;
|
|
15
|
-
|
|
12
|
+
socketServerProvider;
|
|
16
13
|
chatResolver;
|
|
17
14
|
userResolver;
|
|
18
15
|
callBack = null;
|
|
19
|
-
|
|
16
|
+
server;
|
|
17
|
+
constructor(config, socketServerProvider, chatResolver, userResolver) {
|
|
20
18
|
this.config = config;
|
|
21
|
-
this.
|
|
19
|
+
this.socketServerProvider = socketServerProvider;
|
|
22
20
|
this.chatResolver = chatResolver;
|
|
23
21
|
this.userResolver = userResolver;
|
|
22
|
+
this.server = this.socketServerProvider.getSocketServer();
|
|
24
23
|
}
|
|
25
24
|
listen(callback) {
|
|
26
25
|
this.callBack = callback;
|
|
@@ -66,12 +65,14 @@ let SocketChannel = SocketChannel_1 = class SocketChannel {
|
|
|
66
65
|
});
|
|
67
66
|
});
|
|
68
67
|
});
|
|
68
|
+
this.socketServerProvider.listen();
|
|
69
69
|
}
|
|
70
70
|
};
|
|
71
71
|
SocketChannel = SocketChannel_1 = __decorate([
|
|
72
72
|
injectable(),
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
__metadata("design:paramtypes", [SocketChannelConfig,
|
|
74
|
+
SocketServerProvider,
|
|
75
|
+
ChatResolver,
|
|
75
76
|
UserResolver])
|
|
76
77
|
], SocketChannel);
|
|
77
78
|
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { Logger } from '../../logger/Logger.js';
|
|
3
|
+
import { HttpServerProvider } from '../http/HttpServerProvider.js';
|
|
4
|
+
import { Server } from 'socket.io';
|
|
5
|
+
import { singleton } from 'tsyringe';
|
|
6
|
+
|
|
7
|
+
let SocketServerProvider = class SocketServerProvider {
|
|
8
|
+
httpServerProvider;
|
|
9
|
+
socketServer = null;
|
|
10
|
+
logger = new Logger('wabot:socket');
|
|
11
|
+
constructor(httpServerProvider) {
|
|
12
|
+
this.httpServerProvider = httpServerProvider;
|
|
13
|
+
}
|
|
14
|
+
getSocketServer() {
|
|
15
|
+
if (!this.socketServer) {
|
|
16
|
+
this.socketServer = this.createSocketServer();
|
|
17
|
+
}
|
|
18
|
+
return this.socketServer;
|
|
19
|
+
}
|
|
20
|
+
listen() {
|
|
21
|
+
this.httpServerProvider.listen();
|
|
22
|
+
}
|
|
23
|
+
createSocketServer() {
|
|
24
|
+
const httpServer = this.httpServerProvider.getHttpServer();
|
|
25
|
+
const socketServer = new Server(httpServer, {
|
|
26
|
+
cors: {
|
|
27
|
+
origin: '*',
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
socketServer.on('connection', (socket) => {
|
|
31
|
+
this.logger.trace(`socket:${socket.id} connection`);
|
|
32
|
+
socket.onAny((event) => {
|
|
33
|
+
this.logger.trace(`socket:${socket.id} emmits ${event}`);
|
|
34
|
+
});
|
|
35
|
+
socket.on('disconnect', (reason) => {
|
|
36
|
+
this.logger.trace(`socket:${socket.id} disconnect with reason: ${reason}`);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
httpServer.on('listening', () => {
|
|
40
|
+
this.logger.info(`socket server listening`);
|
|
41
|
+
});
|
|
42
|
+
return socketServer;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
SocketServerProvider = __decorate([
|
|
46
|
+
singleton(),
|
|
47
|
+
__metadata("design:paramtypes", [HttpServerProvider])
|
|
48
|
+
], SocketServerProvider);
|
|
49
|
+
|
|
50
|
+
export { SocketServerProvider };
|
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import { container } from '../../injection/index.js';
|
|
2
2
|
import '../../controller/channel/ChatResolver.js';
|
|
3
3
|
import '../../controller/channel/UserResolver.js';
|
|
4
|
-
import '../../core/chat/repository/IChatRepository.js';
|
|
5
|
-
import '../../core/user/IUserRepository.js';
|
|
6
4
|
import { ControllerMetadataStore } from '../../controller/metadata/ControllerMetadataStore.js';
|
|
7
|
-
import 'express';
|
|
8
|
-
import 'socket.io';
|
|
9
5
|
import { TelegramChannel } from './TelegramChannel.js';
|
|
10
6
|
import { TelegramChannelConfig } from './TelegramChannelConfig.js';
|
|
11
7
|
|
|
@@ -2,11 +2,7 @@ import { __decorate, __metadata } from 'tslib';
|
|
|
2
2
|
import { ChatResolver } from '../../controller/channel/ChatResolver.js';
|
|
3
3
|
import { UserResolver } from '../../controller/channel/UserResolver.js';
|
|
4
4
|
import { injectable } from '../../injection/index.js';
|
|
5
|
-
import '../../core/chat/repository/IChatRepository.js';
|
|
6
|
-
import '../../core/user/IUserRepository.js';
|
|
7
5
|
import '../../controller/metadata/ControllerMetadataStore.js';
|
|
8
|
-
import 'express';
|
|
9
|
-
import 'socket.io';
|
|
10
6
|
import { Bot } from 'grammy';
|
|
11
7
|
import { TelegramChannelConfig } from './TelegramChannelConfig.js';
|
|
12
8
|
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { singleton } from '../../injection/index.js';
|
|
3
|
+
import { Logger } from '../../logger/Logger.js';
|
|
4
|
+
import { io } from 'socket.io-client';
|
|
5
|
+
import { devEmitEvent } from './WabotDevSocketContracts.js';
|
|
6
|
+
|
|
7
|
+
var WabotDevConnection_1;
|
|
8
|
+
let WabotDevConnection = WabotDevConnection_1 = class WabotDevConnection {
|
|
9
|
+
devProxy;
|
|
10
|
+
devProxySocket = null;
|
|
11
|
+
devToken;
|
|
12
|
+
logger = new Logger('wabot:dev-connection');
|
|
13
|
+
static isTokenAvailable() {
|
|
14
|
+
return !!process.env.WABOT_DEV_TOKEN;
|
|
15
|
+
}
|
|
16
|
+
constructor() {
|
|
17
|
+
if (!WabotDevConnection_1.isTokenAvailable()) {
|
|
18
|
+
throw new Error('WABOT_DEV_TOKEN is not set in environment variables');
|
|
19
|
+
}
|
|
20
|
+
this.devToken = process.env.WABOT_DEV_TOKEN;
|
|
21
|
+
this.devProxy = process.env.WABOT_DEV_PROXY ?? 'https://proxy.wabot.dev';
|
|
22
|
+
}
|
|
23
|
+
async getSocket() {
|
|
24
|
+
if (this.devProxySocket) {
|
|
25
|
+
return this.devProxySocket;
|
|
26
|
+
}
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
const devProxySocket = io(this.devProxy, { autoConnect: false });
|
|
29
|
+
devProxySocket.on('connect', async () => {
|
|
30
|
+
try {
|
|
31
|
+
const req = {
|
|
32
|
+
token: this.devToken,
|
|
33
|
+
};
|
|
34
|
+
this.logger.debug('dev connection request');
|
|
35
|
+
const ack = await devProxySocket.emitWithAck(devEmitEvent.DEV_CONNECTION, req);
|
|
36
|
+
if (ack != 'OK') {
|
|
37
|
+
return reject(new Error('Dev connection failed'));
|
|
38
|
+
}
|
|
39
|
+
this.logger.debug('success dev connection');
|
|
40
|
+
this.devProxySocket = devProxySocket;
|
|
41
|
+
resolve(devProxySocket);
|
|
42
|
+
}
|
|
43
|
+
catch (err) {
|
|
44
|
+
this.logger.error(err);
|
|
45
|
+
reject(err);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
devProxySocket.connect();
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
WabotDevConnection = WabotDevConnection_1 = __decorate([
|
|
53
|
+
singleton(),
|
|
54
|
+
__metadata("design:paramtypes", [])
|
|
55
|
+
], WabotDevConnection);
|
|
56
|
+
|
|
57
|
+
export { WabotDevConnection };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
const
|
|
1
|
+
const devListentEvent = {
|
|
2
2
|
DEV_WATSAPP_WEBHOOK: 'dev-whatsapp-webhook',
|
|
3
3
|
};
|
|
4
|
-
const
|
|
4
|
+
const devEmitEvent = {
|
|
5
5
|
DEV_CONNECTION: 'dev-connection',
|
|
6
6
|
DEV_SEND_WHATSAPP: 'dev-send-whatsapp',
|
|
7
7
|
DEV_SEND_WHATSAPP_TEMPLATE: 'dev-send-whatsapp-template',
|
|
8
8
|
};
|
|
9
9
|
|
|
10
|
-
export {
|
|
10
|
+
export { devEmitEvent, devListentEvent };
|
|
@@ -2,11 +2,7 @@ import { container } from '../../injection/index.js';
|
|
|
2
2
|
import { WhatsappChannelConfig } from './WhatsAppChannelConfig.js';
|
|
3
3
|
import '../../controller/channel/ChatResolver.js';
|
|
4
4
|
import '../../controller/channel/UserResolver.js';
|
|
5
|
-
import '../../core/chat/repository/IChatRepository.js';
|
|
6
|
-
import '../../core/user/IUserRepository.js';
|
|
7
5
|
import { ControllerMetadataStore } from '../../controller/metadata/ControllerMetadataStore.js';
|
|
8
|
-
import 'express';
|
|
9
|
-
import 'socket.io';
|
|
10
6
|
import { WhatsAppChannel } from './WhatsAppChannel.js';
|
|
11
7
|
|
|
12
8
|
function whatsapp(config) {
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { singleton } from '../../injection/index.js';
|
|
3
|
+
import { WhatsApp } from './WhatsApp.js';
|
|
4
|
+
import { WabotEnv } from '../../env/WabotEnv.js';
|
|
5
|
+
|
|
6
|
+
let EnvWhatsAppRepository = class EnvWhatsAppRepository {
|
|
7
|
+
env;
|
|
8
|
+
constructor(env) {
|
|
9
|
+
this.env = env;
|
|
10
|
+
}
|
|
11
|
+
async findBySlug(slug) {
|
|
12
|
+
const whatsApp = this.getFromEnvVars();
|
|
13
|
+
if (whatsApp.getSlug() !== slug) {
|
|
14
|
+
throw new Error(`Not found WhatsApp configuration in env variables for the required slug '${slug}'`);
|
|
15
|
+
}
|
|
16
|
+
return whatsApp;
|
|
17
|
+
}
|
|
18
|
+
async findByBusinessNumber(number) {
|
|
19
|
+
const whatsApp = this.getFromEnvVars();
|
|
20
|
+
if (!whatsApp.getBussinessNumber(number)) {
|
|
21
|
+
throw new Error(`Not found WhatsApp configuration in env variables for the required number '${number}'`);
|
|
22
|
+
}
|
|
23
|
+
return whatsApp;
|
|
24
|
+
}
|
|
25
|
+
getFromEnvVars() {
|
|
26
|
+
const whatsApp = new WhatsApp({
|
|
27
|
+
verifyToken: this.env.requireString('WHATSAPP_HOOK_TOKEN'),
|
|
28
|
+
slug: this.env.requireString('WHATSAPP_HOOK_SLUG'),
|
|
29
|
+
appSecret: this.env.requireString('WHATSAPP_APP_SECRET'),
|
|
30
|
+
businessAccount: {
|
|
31
|
+
id: this.env.requireString('WHATSAPP_ACCOUNT_ID'),
|
|
32
|
+
},
|
|
33
|
+
accessToken: this.env.requireString('WHATSAPP_ACCESS_TOKEN'),
|
|
34
|
+
businessNumbers: [
|
|
35
|
+
{
|
|
36
|
+
id: this.env.requireString('WHATSAPP_BUSINESS_NUMBER_ID'),
|
|
37
|
+
number: this.env.requireString('WHATSAPP_BUSINESS_NUMBER'),
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
});
|
|
41
|
+
return whatsApp;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
EnvWhatsAppRepository = __decorate([
|
|
45
|
+
singleton(),
|
|
46
|
+
__metadata("design:paramtypes", [WabotEnv])
|
|
47
|
+
], EnvWhatsAppRepository);
|
|
48
|
+
|
|
49
|
+
export { EnvWhatsAppRepository };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { Pool } from 'pg';
|
|
3
|
+
import { WhatsApp } from './WhatsApp.js';
|
|
4
|
+
import { singleton } from '../../injection/index.js';
|
|
5
|
+
import { PgCrudRepository } from '../../repository/pg/PgCrudRepository.js';
|
|
6
|
+
|
|
7
|
+
let PgWhatsAppRepository = class PgWhatsAppRepository extends PgCrudRepository {
|
|
8
|
+
constructor(pool) {
|
|
9
|
+
super(pool, {
|
|
10
|
+
table: 'whatsapp',
|
|
11
|
+
constructor: WhatsApp,
|
|
12
|
+
schema: 'wabot',
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
async findBySlug(slug) {
|
|
16
|
+
const sql = `
|
|
17
|
+
SELECT ${this.columns}
|
|
18
|
+
FROM ${this.table}
|
|
19
|
+
WHERE data @> $1
|
|
20
|
+
LIMIT 1
|
|
21
|
+
`;
|
|
22
|
+
const items = await this.query(sql, [JSON.stringify({ slug })]);
|
|
23
|
+
return items.at(0) ?? null;
|
|
24
|
+
}
|
|
25
|
+
async findByBusinessNumber(number) {
|
|
26
|
+
const sql = `
|
|
27
|
+
SELECT ${this.columns}
|
|
28
|
+
FROM ${this.table}
|
|
29
|
+
WHERE data->'businessNumbers' @> $1
|
|
30
|
+
LIMIT 1
|
|
31
|
+
`;
|
|
32
|
+
const items = await this.query(sql, [JSON.stringify([{ number }])]);
|
|
33
|
+
return items.at(0) ?? null;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
PgWhatsAppRepository = __decorate([
|
|
37
|
+
singleton(),
|
|
38
|
+
__metadata("design:paramtypes", [Pool])
|
|
39
|
+
], PgWhatsAppRepository);
|
|
40
|
+
|
|
41
|
+
export { PgWhatsAppRepository };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import '../../core/chat/repository/IChatRepository.js';
|
|
2
|
+
import { Persistent } from '../../core/Persistent.js';
|
|
3
|
+
import '../../core/user/IUserRepository.js';
|
|
4
|
+
|
|
5
|
+
class WhatsApp extends Persistent {
|
|
6
|
+
getVerifyToken() {
|
|
7
|
+
return this.data.verifyToken;
|
|
8
|
+
}
|
|
9
|
+
getSlug() {
|
|
10
|
+
return this.data.slug;
|
|
11
|
+
}
|
|
12
|
+
getBusinessAccount() {
|
|
13
|
+
return this.data.businessAccount;
|
|
14
|
+
}
|
|
15
|
+
getAppSecret() {
|
|
16
|
+
return this.data.appSecret;
|
|
17
|
+
}
|
|
18
|
+
getBusinessNumbers() {
|
|
19
|
+
return this.data.businessNumbers;
|
|
20
|
+
}
|
|
21
|
+
getAccessToken() {
|
|
22
|
+
return this.data.accessToken;
|
|
23
|
+
}
|
|
24
|
+
hasBusinessNumber(number) {
|
|
25
|
+
return this.data.businessNumbers.some((x) => x.number === number);
|
|
26
|
+
}
|
|
27
|
+
getBussinessNumber(number) {
|
|
28
|
+
return this.data.businessNumbers.find((x) => x.number === number) ?? null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { WhatsApp };
|
|
@@ -2,52 +2,54 @@ import { __decorate, __metadata } from 'tslib';
|
|
|
2
2
|
import { ChatResolver } from '../../controller/channel/ChatResolver.js';
|
|
3
3
|
import { UserResolver } from '../../controller/channel/UserResolver.js';
|
|
4
4
|
import { injectable } from '../../injection/index.js';
|
|
5
|
-
import '../../core/chat/repository/IChatRepository.js';
|
|
6
|
-
import '../../core/user/IUserRepository.js';
|
|
7
5
|
import '../../controller/metadata/ControllerMetadataStore.js';
|
|
8
|
-
import 'express';
|
|
9
|
-
import 'socket.io';
|
|
10
|
-
import { WabotEnv } from '../../env/WabotEnv.js';
|
|
11
6
|
import { Logger } from '../../logger/Logger.js';
|
|
12
7
|
import { WhatsappChannelConfig } from './WhatsAppChannelConfig.js';
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
8
|
+
import { WhatsAppReceiver } from './WhatsAppReceiver.js';
|
|
9
|
+
import { WhatsAppSender } from './WhatsAppSender.js';
|
|
15
10
|
|
|
16
11
|
let WhatsAppChannel = class WhatsAppChannel {
|
|
17
12
|
config;
|
|
18
13
|
chatResolver;
|
|
19
14
|
userResolver;
|
|
20
|
-
|
|
15
|
+
sender;
|
|
16
|
+
receiver;
|
|
21
17
|
logger = new Logger('wabot:whatsapp-channel');
|
|
22
|
-
|
|
23
|
-
constructor(config, chatResolver, userResolver, wabotEnv, devConnection, prodConnection) {
|
|
18
|
+
constructor(config, chatResolver, userResolver, sender, receiver) {
|
|
24
19
|
this.config = config;
|
|
25
20
|
this.chatResolver = chatResolver;
|
|
26
21
|
this.userResolver = userResolver;
|
|
27
|
-
this.
|
|
28
|
-
this.
|
|
22
|
+
this.sender = sender;
|
|
23
|
+
this.receiver = receiver;
|
|
29
24
|
}
|
|
30
25
|
listen(callback) {
|
|
31
|
-
this.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
26
|
+
this.receiver.listenMessage({
|
|
27
|
+
to: this.config.number,
|
|
28
|
+
listener: async (message) => {
|
|
29
|
+
try {
|
|
30
|
+
const chat = await this.chatResolver.resolve(message.chatConnection);
|
|
31
|
+
const user = await this.userResolver.resolve(message.userConnection);
|
|
32
|
+
callback({
|
|
33
|
+
chat,
|
|
34
|
+
user,
|
|
35
|
+
message,
|
|
36
|
+
reply: (replyMessage) => {
|
|
37
|
+
this.sender.sendWhatsApp({
|
|
38
|
+
from: this.config.number,
|
|
39
|
+
to: message.userConnection.id,
|
|
40
|
+
message: replyMessage,
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
this.logger.error(err);
|
|
47
|
+
}
|
|
48
|
+
},
|
|
47
49
|
});
|
|
48
50
|
}
|
|
49
51
|
connect() {
|
|
50
|
-
this.
|
|
52
|
+
this.receiver.connect();
|
|
51
53
|
}
|
|
52
54
|
};
|
|
53
55
|
WhatsAppChannel = __decorate([
|
|
@@ -55,9 +57,8 @@ WhatsAppChannel = __decorate([
|
|
|
55
57
|
__metadata("design:paramtypes", [WhatsappChannelConfig,
|
|
56
58
|
ChatResolver,
|
|
57
59
|
UserResolver,
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
WhatsAppProdConnection])
|
|
60
|
+
WhatsAppSender,
|
|
61
|
+
WhatsAppReceiver])
|
|
61
62
|
], WhatsAppChannel);
|
|
62
63
|
|
|
63
64
|
export { WhatsAppChannel };
|
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
class WhatsAppConnection {
|
|
1
|
+
class WhatsAppReceiver {
|
|
4
2
|
logger;
|
|
5
3
|
listeners = new Map();
|
|
6
4
|
constructor(logger) {
|
|
7
5
|
this.logger = logger;
|
|
8
6
|
}
|
|
9
|
-
listenMessage(
|
|
10
|
-
this.listeners.set(
|
|
7
|
+
listenMessage(request) {
|
|
8
|
+
this.listeners.set(request.to, request.listener);
|
|
11
9
|
}
|
|
12
10
|
async handlePayload(payload) {
|
|
13
11
|
try {
|
|
@@ -58,4 +56,4 @@ class WhatsAppConnection {
|
|
|
58
56
|
}
|
|
59
57
|
}
|
|
60
58
|
|
|
61
|
-
export {
|
|
59
|
+
export { WhatsAppReceiver };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { Logger } from '../../logger/Logger.js';
|
|
3
|
+
import { WhatsAppReceiver } from './WhatsAppReceiver.js';
|
|
4
|
+
import { WabotDevConnection } from '../wabot/WabotDevConnection.js';
|
|
5
|
+
import { devListentEvent } from '../wabot/WabotDevSocketContracts.js';
|
|
6
|
+
import { singleton } from 'tsyringe';
|
|
7
|
+
import { container } from '../../injection/index.js';
|
|
8
|
+
|
|
9
|
+
let WhatsAppReceiverByDevConnection = class WhatsAppReceiverByDevConnection extends WhatsAppReceiver {
|
|
10
|
+
wabotDevConnection;
|
|
11
|
+
constructor(wabotDevConnection) {
|
|
12
|
+
super(new Logger('wabot:whatsapp-receiver-by-dev-connection'));
|
|
13
|
+
this.wabotDevConnection = wabotDevConnection;
|
|
14
|
+
}
|
|
15
|
+
async connect() {
|
|
16
|
+
const socket = await this.wabotDevConnection.getSocket();
|
|
17
|
+
socket.on(devListentEvent.DEV_WATSAPP_WEBHOOK, (payload) => {
|
|
18
|
+
this.handlePayload(payload);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
WhatsAppReceiverByDevConnection = __decorate([
|
|
23
|
+
singleton(),
|
|
24
|
+
__metadata("design:paramtypes", [WabotDevConnection])
|
|
25
|
+
], WhatsAppReceiverByDevConnection);
|
|
26
|
+
if (WabotDevConnection.isTokenAvailable()) {
|
|
27
|
+
container.register(WhatsAppReceiver, {
|
|
28
|
+
useClass: WhatsAppReceiverByDevConnection,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { WhatsAppReceiverByDevConnection };
|