@wabot-dev/framework 0.9.0 → 0.9.2

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.
Files changed (56) hide show
  1. package/dist/src/addon/async/in-memory/InMemoryCronJobRepository.js +45 -0
  2. package/dist/src/addon/async/in-memory/InMemoryJobRepository.js +75 -0
  3. package/dist/src/addon/async/in-memory/index.js +2 -0
  4. package/dist/src/addon/async/pg/PgJobRepository.js +1 -0
  5. package/dist/src/addon/async/pg/PgTransactionAdapter.js +25 -0
  6. package/dist/src/addon/async/pg/index.js +3 -0
  7. package/dist/src/addon/chat-bot/{ram/RamChatMemory.js → in-memory/InMemoryChatMemory.js} +2 -2
  8. package/dist/src/addon/chat-bot/{ram/RamChatRepository.js → in-memory/InMemoryChatRepository.js} +6 -6
  9. package/dist/src/addon/chat-bot/in-memory/index.js +2 -0
  10. package/dist/src/addon/chat-bot/pg/index.js +2 -0
  11. package/dist/src/addon/chat-controller/socket/@socket.js +3 -1
  12. package/dist/src/addon/chat-controller/socket/SocketChannelConfig.js +4 -4
  13. package/dist/src/addon/chat-controller/whatsapp/WhatsAppSender.js +2 -72
  14. package/dist/src/addon/chat-controller/whatsapp/{WhatsAppReceiver.js → cloud-api/WhatsAppApiReceiver.js} +2 -2
  15. package/dist/src/addon/chat-controller/whatsapp/cloud-api/WhatsAppApiSender.js +67 -0
  16. package/dist/src/addon/chat-controller/whatsapp/cloud-api/WhatsAppReceiverByCloudApi.js +5 -7
  17. package/dist/src/addon/chat-controller/whatsapp/wasender/@wasender.js +23 -0
  18. package/dist/src/addon/chat-controller/{wasender/WhatsAppByWasenderChannel.js → whatsapp/wasender/WasenderChannel.js} +20 -20
  19. package/dist/src/addon/chat-controller/{wasender/WhatsAppByWasenderChannelConfig.js → whatsapp/wasender/WasenderChannelConfig.js} +3 -3
  20. package/dist/src/addon/chat-controller/whatsapp/wasender/WasenderChannelName.js +3 -0
  21. package/dist/src/addon/chat-controller/{wasender/WhatsAppReceiverByWasender.js → whatsapp/wasender/WasenderReceiver.js} +7 -7
  22. package/dist/src/addon/chat-controller/{wasender/WhatsAppSenderByWasender.js → whatsapp/wasender/WasenderSender.js} +8 -5
  23. package/dist/src/addon/chat-controller/{wasender → whatsapp/wasender}/WasenderWebhookController.js +4 -4
  24. package/dist/src/addon/lock/InMemoryLockKey.js +45 -0
  25. package/dist/src/addon/lock/InMemoryLocker.js +15 -0
  26. package/dist/src/addon/lock/index.js +2 -0
  27. package/dist/src/core/config/resolver.js +19 -1
  28. package/dist/src/feature/async/{@cron.js → @cronHandler.js} +2 -2
  29. package/dist/src/feature/async/@transaction.js +22 -0
  30. package/dist/src/feature/async/AsyncMetadataStore.js +6 -0
  31. package/dist/src/feature/async/TransactionMetadataStore.js +28 -0
  32. package/dist/src/feature/chat-controller/metadata/ControllerMetadataStore.js +3 -0
  33. package/dist/src/feature/pg/index.js +13 -0
  34. package/dist/src/feature/project-runner/ProjectRunner.js +253 -0
  35. package/dist/src/feature/rest-controller/metadata/RestControllerMetadataStore.js +3 -0
  36. package/dist/src/feature/socket-controller/metadata/SocketControllerMetadataStore.js +3 -0
  37. package/dist/src/index.d.ts +195 -261
  38. package/dist/src/index.js +23 -29
  39. package/package.json +6 -6
  40. package/dist/src/addon/chat-controller/wasender/@whatsAppByWasender.js +0 -20
  41. package/dist/src/addon/chat-controller/wasender/whatsAppByWasenderChannelName.js +0 -3
  42. package/dist/src/addon/chat-controller/whatsapp/@whatsApp.js +0 -20
  43. package/dist/src/addon/chat-controller/whatsapp/EnvWhatsAppRepository.js +0 -49
  44. package/dist/src/addon/chat-controller/whatsapp/PgWhatsAppRepository.js +0 -48
  45. package/dist/src/addon/chat-controller/whatsapp/WhatsApp.js +0 -30
  46. package/dist/src/addon/chat-controller/whatsapp/WhatsAppChannel.js +0 -58
  47. package/dist/src/addon/chat-controller/whatsapp/WhatsAppChannelConfig.js +0 -8
  48. package/dist/src/addon/chat-controller/whatsapp/WhatsAppRepository.js +0 -10
  49. package/dist/src/addon/chat-controller/whatsapp/cloud-api/WhatsAppSenderByCloudApi.js +0 -133
  50. package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppProxyContracts.js +0 -5
  51. package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppReceiverByWabotProxy.js +0 -67
  52. package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppSenderByWabotProxy.js +0 -63
  53. package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppWabotProxyConnection.js +0 -45
  54. package/dist/src/core/config/decorators.js +0 -22
  55. /package/dist/src/addon/chat-controller/whatsapp/{whatsAppChannelName.js → cloud-api/whatsAppChannelName.js} +0 -0
  56. /package/dist/src/addon/chat-controller/{wasender → whatsapp/wasender}/extractNumberFromWasenderKey.js +0 -0
package/dist/src/index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  export { Auth } from './core/auth/Auth.js';
2
2
  export { bool, boolArr, num, numArr, obj, str, strArr } from './core/config/tag-functions.js';
3
- export { ConfigResolver } from './core/config/resolver.js';
4
- export { resolveConfigReferences } from './core/config/decorators.js';
3
+ export { ConfigResolver, resolveConfigReferences } from './core/config/resolver.js';
5
4
  export { Entity, Persistent } from './core/entity/Entity.js';
6
5
  export { Env } from './core/env/Env.js';
7
6
  export { CustomError, errorToPlainObject } from './core/error/CustomError.js';
@@ -45,7 +44,8 @@ export { DescriptionMetadataStore } from './core/description/metadata/Descriptio
45
44
  export { Locker } from './core/lock/Locker.js';
46
45
  export { command } from './feature/async/@command.js';
47
46
  export { commandHandler } from './feature/async/@commandHandler.js';
48
- export { cron } from './feature/async/@cron.js';
47
+ export { cronHandler } from './feature/async/@cronHandler.js';
48
+ export { transaction } from './feature/async/@transaction.js';
49
49
  export { Async } from './feature/async/Async.js';
50
50
  export { AsyncMetadataStore } from './feature/async/AsyncMetadataStore.js';
51
51
  export { CronJob } from './feature/async/CronJob.js';
@@ -55,6 +55,8 @@ export { JobRepository } from './feature/async/JobRepository.js';
55
55
  export { JobRunner } from './feature/async/JobRunner.js';
56
56
  export { runCommandHandlers, stopCommandHandlers } from './feature/async/runCommandHandlers.js';
57
57
  export { runCronHandlers, stopCronHandlers } from './feature/async/runCronHandlers.js';
58
+ export { TransactionMetadataStore } from './feature/async/TransactionMetadataStore.js';
59
+ export { ProjectRunner, run } from './feature/project-runner/ProjectRunner.js';
58
60
  export { Chat } from './feature/chat-bot/Chat.js';
59
61
  export { ChatAdapter } from './feature/chat-bot/ChatAdapter.js';
60
62
  export { ChatAdapterRegistry } from './feature/chat-bot/ChatAdapterRegistry.js';
@@ -117,8 +119,11 @@ export { socketController } from './feature/socket-controller/metadata/@socketCo
117
119
  export { onSocketEvent } from './feature/socket-controller/metadata/@onSocketEvent.js';
118
120
  export { SocketControllerMetadataStore } from './feature/socket-controller/metadata/SocketControllerMetadataStore.js';
119
121
  export { runSocketControllers } from './feature/socket-controller/runSocketControllers.js';
120
- export { PgJobRepository } from './addon/async/pg/PgJobRepository.js';
121
122
  export { PgCronJobRepository } from './addon/async/pg/PgCronJobRepository.js';
123
+ export { PgJobRepository } from './addon/async/pg/PgJobRepository.js';
124
+ export { PgTransactionAdapter } from './addon/async/pg/PgTransactionAdapter.js';
125
+ export { InMemoryJobRepository } from './addon/async/in-memory/InMemoryJobRepository.js';
126
+ export { InMemoryCronJobRepository } from './addon/async/in-memory/InMemoryCronJobRepository.js';
122
127
  export { apiKeyHandshakeGuard } from './addon/auth/api-key/@apiKeyHandshakeGuard.js';
123
128
  export { apiKeyGuard } from './addon/auth/api-key/@apiKeyGuard.js';
124
129
  export { ApiKey } from './addon/auth/api-key/ApiKey.js';
@@ -146,8 +151,8 @@ export { OpenaiChatAdapter } from './addon/chat-bot/openia/OpenaiChatAdapter.js'
146
151
  export { OpenRouterChatAdapter } from './addon/chat-bot/openrouter/OpenRouterChatAdapter.js';
147
152
  export { PgChatRepository } from './addon/chat-bot/pg/PgChatRepository.js';
148
153
  export { PgChatMemory } from './addon/chat-bot/pg/PgChatMemory.js';
149
- export { RamChatMemory } from './addon/chat-bot/ram/RamChatMemory.js';
150
- export { RamChatRepository } from './addon/chat-bot/ram/RamChatRepository.js';
154
+ export { InMemoryChatMemory } from './addon/chat-bot/in-memory/InMemoryChatMemory.js';
155
+ export { InMemoryChatRepository } from './addon/chat-bot/in-memory/InMemoryChatRepository.js';
151
156
  export { WabotChatAdapter } from './addon/chat-bot/wabot/WabotChatAdapter.js';
152
157
  export { cmd } from './addon/chat-controller/cmd/@cmd.js';
153
158
  export { CmdChannel, readJsonFromFile, writeJsonToFile } from './addon/chat-controller/cmd/CmdChannel.js';
@@ -160,29 +165,18 @@ export { telegram } from './addon/chat-controller/telegram/@telegram.js';
160
165
  export { TelegramChannelConfig } from './addon/chat-controller/telegram/TelegramChannelConfig.js';
161
166
  export { TelegramChannel } from './addon/chat-controller/telegram/TelegramChannel.js';
162
167
  export { telegramChannelName } from './addon/chat-controller/telegram/telegramChannelName.js';
163
- export { whatsApp } from './addon/chat-controller/whatsapp/@whatsApp.js';
164
- export { EnvWhatsAppRepository } from './addon/chat-controller/whatsapp/EnvWhatsAppRepository.js';
165
- export { PgWhatsAppRepository } from './addon/chat-controller/whatsapp/PgWhatsAppRepository.js';
166
- export { WhatsApp } from './addon/chat-controller/whatsapp/WhatsApp.js';
167
- export { WhatsAppChannel } from './addon/chat-controller/whatsapp/WhatsAppChannel.js';
168
- export { WhatsappChannelConfig } from './addon/chat-controller/whatsapp/WhatsAppChannelConfig.js';
169
- export { WhatsAppReceiver } from './addon/chat-controller/whatsapp/WhatsAppReceiver.js';
170
- export { WhatsAppRepository } from './addon/chat-controller/whatsapp/WhatsAppRepository.js';
171
- export { WhatsAppSender } from './addon/chat-controller/whatsapp/WhatsAppSender.js';
172
- export { whatsAppChannelName } from './addon/chat-controller/whatsapp/whatsAppChannelName.js';
173
168
  export { WhatsAppReceiverByCloudApi } from './addon/chat-controller/whatsapp/cloud-api/WhatsAppReceiverByCloudApi.js';
174
- export { WhatsAppSenderByCloudApi } from './addon/chat-controller/whatsapp/cloud-api/WhatsAppSenderByCloudApi.js';
175
- export { WHATSAPP_MESSAGE_EVENT, WHATSAPP_PROXY_LISTEN_MESSAGE_EVENT, WHATSAPP_PROXY_SEND_MESSAGE_EVENT } from './addon/chat-controller/whatsapp/proxy/WhatsAppProxyContracts.js';
176
- export { WhatsAppReceiverByWabotProxy } from './addon/chat-controller/whatsapp/proxy/WhatsAppReceiverByWabotProxy.js';
177
- export { WhatsAppSenderByWabotProxy } from './addon/chat-controller/whatsapp/proxy/WhatsAppSenderByWabotProxy.js';
178
- export { WhatsAppWabotProxyConnection } from './addon/chat-controller/whatsapp/proxy/WhatsAppWabotProxyConnection.js';
179
- export { whatsAppByWasender } from './addon/chat-controller/wasender/@whatsAppByWasender.js';
180
- export { WhatsAppByWasenderChannel } from './addon/chat-controller/wasender/WhatsAppByWasenderChannel.js';
181
- export { WhatsAppByWasenderChannelConfig } from './addon/chat-controller/wasender/WhatsAppByWasenderChannelConfig.js';
182
- export { WhatsAppReceiverByWasender } from './addon/chat-controller/wasender/WhatsAppReceiverByWasender.js';
183
- export { WhatsAppSenderByWasender } from './addon/chat-controller/wasender/WhatsAppSenderByWasender.js';
184
- export { WasenderWebhookController } from './addon/chat-controller/wasender/WasenderWebhookController.js';
185
- export { extractNumberFromWasenderMessageKey } from './addon/chat-controller/wasender/extractNumberFromWasenderKey.js';
186
- export { whatsAppByWasenderChannelName } from './addon/chat-controller/wasender/whatsAppByWasenderChannelName.js';
169
+ export { WhatsAppApiSender } from './addon/chat-controller/whatsapp/cloud-api/WhatsAppApiSender.js';
170
+ export { wasender } from './addon/chat-controller/whatsapp/wasender/@wasender.js';
171
+ export { WasenderChannel } from './addon/chat-controller/whatsapp/wasender/WasenderChannel.js';
172
+ export { WasenderChannelConfig } from './addon/chat-controller/whatsapp/wasender/WasenderChannelConfig.js';
173
+ export { WasenderReceiver } from './addon/chat-controller/whatsapp/wasender/WasenderReceiver.js';
174
+ export { WasenderSender } from './addon/chat-controller/whatsapp/wasender/WasenderSender.js';
175
+ export { WasenderWebhookController } from './addon/chat-controller/whatsapp/wasender/WasenderWebhookController.js';
176
+ export { extractNumberFromWasenderMessageKey } from './addon/chat-controller/whatsapp/wasender/extractNumberFromWasenderKey.js';
177
+ export { wasenderChannelName } from './addon/chat-controller/whatsapp/wasender/WasenderChannelName.js';
178
+ export { WhatsAppSender } from './addon/chat-controller/whatsapp/WhatsAppSender.js';
179
+ export { InMemoryLockKey } from './addon/lock/InMemoryLockKey.js';
180
+ export { InMemoryLocker } from './addon/lock/InMemoryLocker.js';
187
181
  export { HtmlModule } from './addon/mindset/html/HtmlModule.js';
188
182
  export { Container } from './core/injection/Container.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wabot-dev/framework",
3
- "version": "0.9.0",
3
+ "version": "0.9.2",
4
4
  "description": "Framework for IA Chat Bots",
5
5
  "type": "module",
6
6
  "main": "dist/src/index.js",
@@ -57,10 +57,7 @@
57
57
  "@rollup/plugin-json": "6.1.0",
58
58
  "@rollup/plugin-node-resolve": "16.0.1",
59
59
  "@rollup/plugin-typescript": "12.1.2",
60
- "prettier": "^3.5.3",
61
- "rollup": "^4.60.0",
62
- "tsup": "^8.4.0",
63
- "typescript": "^6.0.3"
60
+ "rollup": "^4.60.0"
64
61
  },
65
62
  "peerDependencies": {
66
63
  "@anthropic-ai/sdk": "^0.60.0",
@@ -92,6 +89,9 @@
92
89
  "tslib": "^2.8.1",
93
90
  "tsyringe": "^4.9.1",
94
91
  "uuid": "^14.0.0",
95
- "wasenderapi": "^0.1.5"
92
+ "wasenderapi": "^0.1.5",
93
+ "tsup": "^8.4.0",
94
+ "typescript": "^6.0.3",
95
+ "prettier": "^3.5.3"
96
96
  }
97
97
  }
@@ -1,20 +0,0 @@
1
- import { container } from '../../../core/injection/index.js';
2
- import { WhatsAppByWasenderChannelConfig } from './WhatsAppByWasenderChannelConfig.js';
3
- import { ControllerMetadataStore } from '../../../feature/chat-controller/metadata/ControllerMetadataStore.js';
4
- import '../../../feature/chat-controller/ChatResolver.js';
5
- import '../../../feature/chat-controller/runChatControllers.js';
6
- import { WhatsAppByWasenderChannel } from './WhatsAppByWasenderChannel.js';
7
-
8
- function whatsAppByWasender(config) {
9
- return function (target, propertyKey) {
10
- const store = container.resolve(ControllerMetadataStore);
11
- store.saveChannelMetadata({
12
- channelConstructor: WhatsAppByWasenderChannel,
13
- functionName: propertyKey.toString(),
14
- controllerConstructor: target.constructor,
15
- channelConfig: new WhatsAppByWasenderChannelConfig(config ?? {}),
16
- });
17
- };
18
- }
19
-
20
- export { whatsAppByWasender };
@@ -1,3 +0,0 @@
1
- const whatsAppByWasenderChannelName = 'WhatsAppByWasenderChannel';
2
-
3
- export { whatsAppByWasenderChannelName };
@@ -1,20 +0,0 @@
1
- import { container } from '../../../core/injection/index.js';
2
- import { WhatsappChannelConfig } from './WhatsAppChannelConfig.js';
3
- import { ControllerMetadataStore } from '../../../feature/chat-controller/metadata/ControllerMetadataStore.js';
4
- import '../../../feature/chat-controller/ChatResolver.js';
5
- import '../../../feature/chat-controller/runChatControllers.js';
6
- import { WhatsAppChannel } from './WhatsAppChannel.js';
7
-
8
- function whatsApp(config) {
9
- return function (target, propertyKey) {
10
- const store = container.resolve(ControllerMetadataStore);
11
- store.saveChannelMetadata({
12
- channelConstructor: WhatsAppChannel,
13
- functionName: propertyKey.toString(),
14
- controllerConstructor: target.constructor,
15
- channelConfig: new WhatsappChannelConfig(typeof config === 'string' ? config : config.number),
16
- });
17
- };
18
- }
19
-
20
- export { whatsApp };
@@ -1,49 +0,0 @@
1
- import { __decorate, __metadata } from 'tslib';
2
- import { singleton } from '../../../core/injection/index.js';
3
- import { WhatsApp } from './WhatsApp.js';
4
- import { Env } from '../../../core/env/Env.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", [Env])
47
- ], EnvWhatsAppRepository);
48
-
49
- export { EnvWhatsAppRepository };
@@ -1,48 +0,0 @@
1
- import { __decorate, __metadata } from 'tslib';
2
- import { Pool } from 'pg';
3
- import { WhatsApp } from './WhatsApp.js';
4
- import { singleton } from '../../../core/injection/index.js';
5
- import { PgCrudRepository } from '../../../feature/pg/PgCrudRepository.js';
6
- import '../../../feature/pg/PgLocker.js';
7
- import 'debug';
8
- import 'node:crypto';
9
- import '../../../feature/pg/withPgClient.js';
10
- import '../../../feature/pg/pgStorage.js';
11
- import '../../../feature/pg/query/PgJsonRepository.js';
12
- import '../../../feature/pg/query/PgRepositoryMetadataStore.js';
13
-
14
- let PgWhatsAppRepository = class PgWhatsAppRepository extends PgCrudRepository {
15
- constructor(pool) {
16
- super(pool, {
17
- table: 'whatsapp',
18
- constructor: WhatsApp,
19
- schema: 'wabot',
20
- });
21
- }
22
- async findBySlug(slug) {
23
- const sql = `
24
- SELECT ${this.columns}
25
- FROM ${this.table}
26
- WHERE data @> $1
27
- LIMIT 1
28
- `;
29
- const items = await this.query(sql, [JSON.stringify({ slug })]);
30
- return items.at(0) ?? null;
31
- }
32
- async findByBusinessNumber(number) {
33
- const sql = `
34
- SELECT ${this.columns}
35
- FROM ${this.table}
36
- WHERE data->'businessNumbers' @> $1
37
- LIMIT 1
38
- `;
39
- const items = await this.query(sql, [JSON.stringify([{ number }])]);
40
- return items.at(0) ?? null;
41
- }
42
- };
43
- PgWhatsAppRepository = __decorate([
44
- singleton(),
45
- __metadata("design:paramtypes", [Pool])
46
- ], PgWhatsAppRepository);
47
-
48
- export { PgWhatsAppRepository };
@@ -1,30 +0,0 @@
1
- import { Entity } from '../../../core/entity/Entity.js';
2
-
3
- class WhatsApp extends Entity {
4
- getVerifyToken() {
5
- return this.data.verifyToken;
6
- }
7
- getSlug() {
8
- return this.data.slug;
9
- }
10
- getBusinessAccount() {
11
- return this.data.businessAccount;
12
- }
13
- getAppSecret() {
14
- return this.data.appSecret;
15
- }
16
- getBusinessNumbers() {
17
- return this.data.businessNumbers;
18
- }
19
- getAccessToken() {
20
- return this.data.accessToken;
21
- }
22
- hasBusinessNumber(number) {
23
- return this.data.businessNumbers.some((x) => x.number === number);
24
- }
25
- getBussinessNumber(number) {
26
- return this.data.businessNumbers.find((x) => x.number === number) ?? null;
27
- }
28
- }
29
-
30
- export { WhatsApp };
@@ -1,58 +0,0 @@
1
- import { __decorate, __metadata } from 'tslib';
2
- import { injectable } from '../../../core/injection/index.js';
3
- import { Logger } from '../../../core/logger/Logger.js';
4
- import { WhatsappChannelConfig } from './WhatsAppChannelConfig.js';
5
- import { WhatsAppReceiver } from './WhatsAppReceiver.js';
6
- import { WhatsAppSender } from './WhatsAppSender.js';
7
- import { whatsAppChannelName } from './whatsAppChannelName.js';
8
-
9
- let WhatsAppChannel = class WhatsAppChannel {
10
- config;
11
- sender;
12
- receiver;
13
- static channelName = whatsAppChannelName;
14
- logger = new Logger('wabot:whatsapp-channel');
15
- constructor(config, sender, receiver) {
16
- this.config = config;
17
- this.sender = sender;
18
- this.receiver = receiver;
19
- }
20
- listen(callback) {
21
- this.receiver.listenMessage({
22
- to: this.config.number,
23
- listener: async (message) => {
24
- try {
25
- await callback({
26
- channel: whatsAppChannelName,
27
- chatConnection: message.chatConnection,
28
- message: message.message,
29
- reply: async (replyMessage) => {
30
- await this.sender.sendWhatsApp({
31
- from: this.config.number,
32
- to: message.chatConnection.id,
33
- message: replyMessage,
34
- });
35
- },
36
- });
37
- }
38
- catch (err) {
39
- this.logger.error('Failed to handle WhatsApp message', err);
40
- }
41
- },
42
- });
43
- }
44
- connect() {
45
- this.receiver.connect();
46
- }
47
- disconnect() {
48
- this.receiver.disconnect();
49
- }
50
- };
51
- WhatsAppChannel = __decorate([
52
- injectable(),
53
- __metadata("design:paramtypes", [WhatsappChannelConfig,
54
- WhatsAppSender,
55
- WhatsAppReceiver])
56
- ], WhatsAppChannel);
57
-
58
- export { WhatsAppChannel };
@@ -1,8 +0,0 @@
1
- class WhatsappChannelConfig {
2
- number;
3
- constructor(number) {
4
- this.number = number;
5
- }
6
- }
7
-
8
- export { WhatsappChannelConfig };
@@ -1,10 +0,0 @@
1
- class WhatsAppRepository {
2
- findBySlug(slug) {
3
- throw new Error('Method not implemented.');
4
- }
5
- findByBusinessNumber(number) {
6
- throw new Error('Method not implemented.');
7
- }
8
- }
9
-
10
- export { WhatsAppRepository };
@@ -1,133 +0,0 @@
1
- import { __decorate, __metadata } from 'tslib';
2
- import { Logger } from '../../../../core/logger/Logger.js';
3
- import { WhatsAppSender } from '../WhatsAppSender.js';
4
- import { singleton } from '../../../../core/injection/index.js';
5
- import '../../../../feature/chat-controller/metadata/ControllerMetadataStore.js';
6
- import { ChatResolver } from '../../../../feature/chat-controller/ChatResolver.js';
7
- import '../../../../feature/chat-controller/runChatControllers.js';
8
- import '../../../../feature/chat-bot/ChatAdapterRegistry.js';
9
- import '../../../../feature/chat-bot/ChatBot.js';
10
- import '../../../../feature/chat-bot/ChatOperator.js';
11
- import { ChatRepository } from '../../../../feature/chat-bot/ChatRepository.js';
12
- import '../../../../feature/chat-bot/UnionChatAdapter.js';
13
- import '../../../../feature/chat-bot/metadata/ChatAdapterMetadataStore.js';
14
- import 'uuid';
15
- import '../../../../feature/chat-bot/metadata/ChatBotMetadataStore.js';
16
- import '../../../../core/error/setupErrorHandlers.js';
17
- import { WhatsAppRepository } from '../WhatsAppRepository.js';
18
-
19
- let WhatsAppSenderByCloudApi = class WhatsAppSenderByCloudApi extends WhatsAppSender {
20
- logger = new Logger('wabot:whatsapp-sender-by-cloud-api');
21
- constructor(chatRepository, chatResolver, whatsAppRepository) {
22
- super(chatRepository, chatResolver, whatsAppRepository);
23
- }
24
- async sendWhatsApp(request, options) {
25
- const whatsApp = await this.whatsAppRepository.findByBusinessNumber(request.from);
26
- if (!whatsApp) {
27
- throw new Error(`not found WhatsApp with bussiness number '${request.from}'`);
28
- }
29
- const businessNumber = whatsApp.getBussinessNumber(request.from);
30
- const url = `https://graph.facebook.com/v23.0/${businessNumber.id}/messages`;
31
- const payload = {
32
- messaging_product: 'whatsapp',
33
- recipient_type: 'individual',
34
- to: request.to,
35
- type: 'text',
36
- text: {
37
- preview_url: false,
38
- body: request.message.text,
39
- },
40
- };
41
- const response = await fetch(url, {
42
- method: 'POST',
43
- headers: {
44
- Authorization: `Bearer ${whatsApp.getAccessToken()}`,
45
- 'Content-Type': 'application/json',
46
- },
47
- body: JSON.stringify(payload),
48
- });
49
- const data = await response.json();
50
- if (!response.ok) {
51
- throw new Error(JSON.stringify(data));
52
- }
53
- if (options?.writeChatMemory) {
54
- await this.writePrivateChatMemory(request.message, request.to);
55
- }
56
- }
57
- async sendWhatsAppTemplate(request, options) {
58
- const whatsApp = await this.whatsAppRepository.findByBusinessNumber(request.from);
59
- if (!whatsApp) {
60
- throw new Error(`not found WhatsApp with bussiness number '${request.from}'`);
61
- }
62
- const businessNumber = whatsApp.getBussinessNumber(request.from);
63
- const payload = {
64
- messaging_product: 'whatsapp',
65
- recipient_type: 'individual',
66
- to: request.to,
67
- type: 'template',
68
- template: {
69
- name: request.templateMessage.templateName,
70
- language: {
71
- code: request.templateMessage.languageCode,
72
- },
73
- components: [
74
- {
75
- type: 'body',
76
- parameters: request.templateMessage.parameters,
77
- },
78
- ],
79
- },
80
- };
81
- const response = await fetch(`https://graph.facebook.com/v23.0/${businessNumber.id}/messages`, {
82
- method: 'POST',
83
- headers: {
84
- Authorization: `Bearer ${whatsApp.getAccessToken()}`,
85
- 'Content-Type': 'application/json',
86
- },
87
- body: JSON.stringify(payload),
88
- });
89
- const data = await response.json();
90
- if (!response.ok) {
91
- throw new Error(JSON.stringify(data));
92
- }
93
- if (options?.writeChatMemory) {
94
- const message = await this.mapTemplateToChatMessage(request);
95
- await this.writePrivateChatMemory(message, request.to);
96
- }
97
- }
98
- async getWhatsAppTemplate(request) {
99
- const whatsApp = await this.whatsAppRepository.findByBusinessNumber(request.from);
100
- if (!whatsApp) {
101
- throw new Error(`not found WhatsApp with bussiness number '${request.from}'`);
102
- }
103
- const businessAccount = whatsApp.getBusinessAccount();
104
- try {
105
- // Make API call to WhatsApp Cloud API to get templates
106
- const response = await fetch(`https://graph.facebook.com/v23.0/${businessAccount.id}/message_templates?name=${request.templateName}&language=${request.languageCode}&limit=1`, {
107
- method: 'GET',
108
- headers: {
109
- Authorization: `Bearer ${whatsApp.getAccessToken()}`,
110
- 'Content-Type': 'application/json',
111
- },
112
- });
113
- const data = (await response.json());
114
- if (!response.ok) {
115
- throw new Error(JSON.stringify(data));
116
- }
117
- const template = data.data[0] ?? null;
118
- return template;
119
- }
120
- catch (error) {
121
- this.logger.error('Failed to fetch WhatsApp template', error);
122
- throw error;
123
- }
124
- }
125
- };
126
- WhatsAppSenderByCloudApi = __decorate([
127
- singleton(),
128
- __metadata("design:paramtypes", [ChatRepository,
129
- ChatResolver,
130
- WhatsAppRepository])
131
- ], WhatsAppSenderByCloudApi);
132
-
133
- export { WhatsAppSenderByCloudApi };
@@ -1,5 +0,0 @@
1
- const WHATSAPP_PROXY_SEND_MESSAGE_EVENT = 'sendMessage';
2
- const WHATSAPP_PROXY_LISTEN_MESSAGE_EVENT = 'listenMessage';
3
- const WHATSAPP_MESSAGE_EVENT = 'message';
4
-
5
- export { WHATSAPP_MESSAGE_EVENT, WHATSAPP_PROXY_LISTEN_MESSAGE_EVENT, WHATSAPP_PROXY_SEND_MESSAGE_EVENT };
@@ -1,67 +0,0 @@
1
- import { __decorate, __metadata } from 'tslib';
2
- import { WhatsAppReceiver } from '../WhatsAppReceiver.js';
3
- import { whatsAppChannelName } from '../whatsAppChannelName.js';
4
- import { injectable } from '../../../../core/injection/index.js';
5
- import { Logger } from '../../../../core/logger/Logger.js';
6
- import { WHATSAPP_MESSAGE_EVENT } from './WhatsAppProxyContracts.js';
7
- import { WhatsAppWabotProxyConnection } from './WhatsAppWabotProxyConnection.js';
8
-
9
- let WhatsAppReceiverByWabotProxy = class WhatsAppReceiverByWabotProxy extends WhatsAppReceiver {
10
- connection;
11
- loger = new Logger('wabot:whats-app-receiver-by-wabot-proxy');
12
- constructor(connection) {
13
- super();
14
- this.connection = connection;
15
- }
16
- async connect() {
17
- // Nothing
18
- }
19
- listenMessage(request) {
20
- this.connection.getSocket().then(async (socket) => {
21
- const req = {
22
- event: 'listenMessage',
23
- data: {
24
- to: request.to,
25
- },
26
- };
27
- const response = await socket.emitWithAck(req.event, req.data);
28
- if (response && typeof response == 'object' && response['error']) {
29
- this.loger.error(response.error);
30
- return;
31
- }
32
- else if (!response ||
33
- typeof response !== 'object' ||
34
- !Array.isArray(response.from) ||
35
- typeof response.to !== 'string') {
36
- this.loger.error('unknown response');
37
- return;
38
- }
39
- this.loger.trace(`succes add whats-app proxy listener for messages from [${response.from.join(',')}] to ${response.to}`);
40
- socket.on(WHATSAPP_MESSAGE_EVENT, (data, callback) => {
41
- if (data.to !== request.to) {
42
- throw new Error(`expecting message to '${request.to}' but received to='${data.to}'`);
43
- }
44
- callback({ success: true });
45
- this.loger.trace(`success receive message from '${data.from}' to '${data.to}'`);
46
- request.listener({
47
- chatConnection: {
48
- chatType: 'PRIVATE',
49
- channelName: whatsAppChannelName,
50
- id: data.from,
51
- },
52
- message: {
53
- text: data.content.text,
54
- senderName: data.senderName,
55
- senderId: data.from,
56
- },
57
- });
58
- });
59
- });
60
- }
61
- };
62
- WhatsAppReceiverByWabotProxy = __decorate([
63
- injectable(),
64
- __metadata("design:paramtypes", [WhatsAppWabotProxyConnection])
65
- ], WhatsAppReceiverByWabotProxy);
66
-
67
- export { WhatsAppReceiverByWabotProxy };
@@ -1,63 +0,0 @@
1
- import { __decorate, __metadata } from 'tslib';
2
- import { WhatsAppSender } from '../WhatsAppSender.js';
3
- import { singleton } from '../../../../core/injection/index.js';
4
- import { Logger } from '../../../../core/logger/Logger.js';
5
- import '../../../../feature/chat-bot/ChatAdapterRegistry.js';
6
- import '../../../../feature/chat-bot/ChatBot.js';
7
- import '../../../../feature/chat-bot/ChatOperator.js';
8
- import { ChatRepository } from '../../../../feature/chat-bot/ChatRepository.js';
9
- import '../../../../feature/chat-bot/UnionChatAdapter.js';
10
- import '../../../../feature/chat-bot/metadata/ChatAdapterMetadataStore.js';
11
- import 'uuid';
12
- import '../../../../feature/chat-bot/metadata/ChatBotMetadataStore.js';
13
- import '../../../../core/error/setupErrorHandlers.js';
14
- import '../../../../feature/chat-controller/metadata/ControllerMetadataStore.js';
15
- import { ChatResolver } from '../../../../feature/chat-controller/ChatResolver.js';
16
- import '../../../../feature/chat-controller/runChatControllers.js';
17
- import { WhatsAppRepository } from '../WhatsAppRepository.js';
18
- import { WhatsAppWabotProxyConnection } from './WhatsAppWabotProxyConnection.js';
19
-
20
- let WhatsAppSenderByWabotProxy = class WhatsAppSenderByWabotProxy extends WhatsAppSender {
21
- wabotDevConnection;
22
- logger = new Logger('wabot:whatsapp-sender-by-wabot-proxy');
23
- constructor(wabotDevConnection, chatRepository, chatResolver, whatsAppRepository) {
24
- super(chatRepository, chatResolver, whatsAppRepository);
25
- this.wabotDevConnection = wabotDevConnection;
26
- }
27
- async sendWhatsApp(request, options) {
28
- try {
29
- const socket = await this.wabotDevConnection.getSocket();
30
- const req = {
31
- event: 'sendMessage',
32
- data: {
33
- from: request.from,
34
- to: request.to,
35
- content: {
36
- text: request.message.text ?? 'No Text',
37
- },
38
- },
39
- };
40
- const ack = await socket.timeout(5000).emitWithAck(req.event, req.data);
41
- if (!ack || !ack.success) {
42
- throw new Error(`not success ack, when send whatsapp from '${request.from}' to '${request.to}'`);
43
- }
44
- this.logger.trace(`success send whatsapp from '${request.from}' to '${request.to}'`);
45
- if (options?.writeChatMemory) {
46
- await this.writePrivateChatMemory(request.message, request.to);
47
- }
48
- }
49
- catch (err) {
50
- this.logger.error(`Failed to send WhatsApp from '${request.from}' to '${request.to}'`, err);
51
- throw new Error(undefined, { cause: err });
52
- }
53
- }
54
- };
55
- WhatsAppSenderByWabotProxy = __decorate([
56
- singleton(),
57
- __metadata("design:paramtypes", [WhatsAppWabotProxyConnection,
58
- ChatRepository,
59
- ChatResolver,
60
- WhatsAppRepository])
61
- ], WhatsAppSenderByWabotProxy);
62
-
63
- export { WhatsAppSenderByWabotProxy };