@wabot-dev/framework 0.1.0-beta.9 → 0.2.0-beta.1
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/addon/async/pg/PgJobRepository.js +26 -0
- package/dist/src/addon/auth/api-key/@apiKeyConnectionGuard.js +16 -0
- package/dist/src/addon/auth/api-key/@apiKeyGuard.js +17 -0
- package/dist/src/addon/auth/api-key/ApiKey.js +45 -0
- package/dist/src/addon/auth/api-key/ApiKeyConnectionGuardMiddleware.js +57 -0
- package/dist/src/addon/auth/api-key/ApiKeyGuardMiddleware.js +45 -0
- package/dist/src/addon/auth/api-key/ApiKeyRepository.js +22 -0
- package/dist/src/addon/auth/api-key/PgApiKeyRepository.js +53 -0
- package/dist/src/addon/auth/api-key/RemoteApiKeyRepository.js +62 -0
- package/dist/src/addon/auth/jwt/@jwtConnectionGuard.js +16 -0
- package/dist/src/addon/auth/jwt/@jwtGuard.js +17 -0
- package/dist/src/addon/auth/jwt/Jwt.js +53 -0
- package/dist/src/addon/auth/jwt/JwtAccessAndRefreshTokenDto.js +20 -0
- package/dist/src/addon/auth/jwt/JwtConfig.js +28 -0
- package/dist/src/addon/auth/jwt/JwtConnectionGuardMiddleware.js +57 -0
- package/dist/src/addon/auth/jwt/JwtGuardMiddleware.js +45 -0
- package/dist/src/addon/auth/jwt/JwtRefreshToken.js +56 -0
- package/dist/src/addon/auth/jwt/JwtRefreshTokenRepository.js +25 -0
- package/dist/src/addon/auth/jwt/JwtSigner.js +36 -0
- package/dist/src/addon/auth/jwt/JwtTokenDto.js +22 -0
- package/dist/src/addon/auth/jwt/PgJwtRefreshTokenRepository.js +21 -0
- package/dist/src/addon/chat-bot/anthropic/AnthropicChatAdapter.js +135 -0
- package/dist/src/addon/chat-bot/deepseek/DeepSeekChatAdapter.js +137 -0
- package/dist/src/addon/chat-bot/google/GoogleChatAdapter.js +128 -0
- package/dist/src/addon/chat-bot/openia/OpenaiChatAdapter.js +117 -0
- package/dist/src/{pre-made/repository/chat → addon/chat-bot}/pg/PgChatMemory.js +6 -4
- package/dist/src/{pre-made/repository/chat → addon/chat-bot}/pg/PgChatRepository.js +6 -5
- package/dist/src/{pre-made/repository/chat → addon/chat-bot}/ram/RamChatRepository.js +2 -2
- package/dist/src/addon/chat-bot/wabot/WabotChatAdapter.js +41 -0
- package/dist/src/addon/chat-controller/cmd/@cmd.js +24 -0
- package/dist/src/addon/chat-controller/cmd/CmdChannel.js +91 -0
- package/dist/src/{channels → addon/chat-controller}/socket/@socket.js +10 -4
- package/dist/src/{channels → addon/chat-controller}/socket/SocketChannel.js +9 -23
- package/dist/src/{channels → addon/chat-controller}/socket/SocketChannelConfig.js +1 -1
- package/dist/src/{channels → addon/chat-controller}/telegram/@telegram.js +10 -4
- package/dist/src/{channels → addon/chat-controller}/telegram/TelegramChannel.js +4 -22
- package/dist/src/addon/chat-controller/whatsapp/@whatsApp.js +26 -0
- package/dist/src/{channels → addon/chat-controller}/whatsapp/EnvWhatsAppRepository.js +3 -3
- package/dist/src/{channels → addon/chat-controller}/whatsapp/PgWhatsAppRepository.js +2 -2
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsApp.js +2 -4
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppChannel.js +6 -18
- package/dist/src/addon/chat-controller/whatsapp/WhatsAppReceiver.js +10 -0
- package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppSender.js +15 -39
- package/dist/src/addon/chat-controller/whatsapp/cloud-api/WhatsAppReceiverByCloudApi.js +97 -0
- package/dist/src/{channels/whatsapp → addon/chat-controller/whatsapp/cloud-api}/WhatsAppSenderByCloudApi.js +27 -20
- package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppProxyContracts.js +5 -0
- package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppReceiverByWabotProxy.js +65 -0
- package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppSenderByWabotProxy.js +61 -0
- package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppWabotProxyConnection.js +45 -0
- package/dist/src/{pre-made/module → addon/mindset}/html/HtmlModule.js +7 -7
- package/dist/src/core/auth/Auth.js +33 -0
- package/dist/src/core/{Persistent.js → entity/Entity.js} +24 -12
- package/dist/src/core/env/Env.js +39 -0
- package/dist/src/core/error/CustomError.js +15 -0
- package/dist/src/core/injection/index.js +4 -0
- package/dist/src/core/mapper/Mapper.js +42 -0
- package/dist/src/core/password/Password.js +30 -0
- package/dist/src/core/random/Random.js +65 -0
- package/dist/src/core/storable/Storable.js +8 -0
- package/dist/src/core/validation/core/validateArray.js +51 -0
- package/dist/src/core/validation/core/validateModel.js +36 -0
- package/dist/src/{validation/metadata/@isNumber.js → core/validation/metadata/@isArray.js} +5 -4
- package/dist/src/{validation/metadata/@isDate.js → core/validation/metadata/@isModel.js} +5 -4
- package/dist/src/{validation → core/validation}/metadata/@isOptional.js +1 -1
- package/dist/src/core/validation/metadata/ValidationMetadataStore.js +98 -0
- package/dist/src/core/validation/modelInfo.js +9 -0
- package/dist/src/{validation/validateModel2.js → core/validation/validate.js} +3 -3
- package/dist/src/{validation/metadata → core/validation/validators/is-boolean}/@isBoolean.js +3 -3
- package/dist/src/core/validation/validators/is-date/@isDate.js +17 -0
- package/dist/src/core/validation/validators/is-in/@isIn.js +18 -0
- package/dist/src/core/validation/validators/is-in/validateIsIn.js +12 -0
- package/dist/src/{validation/metadata → core/validation/validators/is-not-empty}/@isNotEmpty.js +3 -3
- package/dist/src/core/validation/validators/is-number/@isNumber.js +17 -0
- package/dist/src/{validation/metadata → core/validation/validators/is-present}/@isPresent.js +3 -3
- package/dist/src/{validation/metadata → core/validation/validators/is-string}/@isString.js +3 -3
- package/dist/src/{validation/metadata → core/validation/validators/max}/@max.js +3 -3
- package/dist/src/{validation/metadata → core/validation/validators/min}/@min.js +3 -3
- package/dist/src/feature/async/@command.js +11 -0
- package/dist/src/feature/async/@commandHandler.js +12 -0
- package/dist/src/feature/async/Async.js +38 -0
- package/dist/src/feature/async/Command.js +9 -0
- package/dist/src/feature/async/CommandMetadataStore.js +38 -0
- package/dist/src/feature/async/Job.js +27 -0
- package/dist/src/feature/async/JobRepository.js +31 -0
- package/dist/src/feature/async/JobRunner.js +48 -0
- package/dist/src/feature/async/JobsEventsHub.js +36 -0
- package/dist/src/feature/async/runCommandHandlers.js +29 -0
- package/dist/src/{core/chat → feature/chat-bot}/Chat.js +2 -2
- package/dist/src/feature/chat-bot/ChatAdapter.js +7 -0
- package/dist/src/feature/chat-bot/ChatBot.js +73 -0
- package/dist/src/feature/chat-bot/ChatItem.js +24 -0
- package/dist/src/feature/chat-bot/ChatMemory.js +10 -0
- package/dist/src/{core/chat/repository/IChatRepository.js → feature/chat-bot/ChatRepository.js} +2 -8
- package/dist/src/feature/chat-bot/IChatItem.js +3 -0
- package/dist/src/{chatbot → feature/chat-bot}/metadata/@chatBot.js +1 -1
- package/dist/src/{chatbot → feature/chat-bot}/metadata/ChatBotMetadataStore.js +1 -1
- package/dist/src/{controller/channel → feature/chat-controller}/ChatResolver.js +6 -4
- package/dist/src/{controller → feature/chat-controller}/metadata/ControllerMetadataStore.js +1 -1
- package/dist/src/{controller → feature/chat-controller}/metadata/controller/@chatController.js +1 -1
- package/dist/src/feature/chat-controller/runChatControllers.js +83 -0
- package/dist/src/{channels → feature}/express/ExpressProvider.js +2 -4
- package/dist/src/{channels → feature}/http/HttpServerProvider.js +2 -2
- package/dist/src/{mindset → feature/mindset}/IMindset.js +6 -0
- package/dist/src/feature/mindset/MindsetOperator.js +180 -0
- package/dist/src/{mindset → feature/mindset}/metadata/MindsetMetadataStore.js +1 -1
- package/dist/src/{mindset → feature/mindset}/metadata/functions/@mindsetFunction.js +1 -1
- package/dist/src/{mindset → feature/mindset}/metadata/mindsets/@mindset.js +1 -1
- package/dist/src/{mindset → feature/mindset}/metadata/modules/@mindsetModule.js +1 -2
- package/dist/src/{mindset → feature/mindset}/metadata/params/@param.js +1 -1
- package/dist/src/feature/money/Money.js +61 -0
- package/dist/src/feature/money/MoneyDto.js +22 -0
- package/dist/src/{repository → feature}/pg/PgCrudRepository.js +24 -10
- package/dist/src/{repository → feature}/pg/PgRepositoryBase.js +2 -2
- package/dist/src/feature/rest-controller/injection-tokens.js +4 -0
- package/dist/src/{rest-controller/metadata/@post.js → feature/rest-controller/metadata/@middleware.js} +5 -8
- package/dist/src/feature/rest-controller/metadata/@onDelete.js +7 -0
- package/dist/src/feature/rest-controller/metadata/@onGet.js +7 -0
- package/dist/src/feature/rest-controller/metadata/@onPost.js +7 -0
- package/dist/src/feature/rest-controller/metadata/@onPut.js +7 -0
- package/dist/src/{rest-controller → feature/rest-controller}/metadata/@restController.js +2 -2
- package/dist/src/{rest-controller → feature/rest-controller}/metadata/RestControllerMetadataStore.js +14 -1
- package/dist/src/{rest-controller/metadata/@get.js → feature/rest-controller/metadata/methodDecorator.js} +5 -5
- package/dist/src/feature/rest-controller/runRestControllers.js +103 -0
- package/dist/src/{channels → feature}/socket/SocketServerProvider.js +2 -2
- package/dist/src/feature/socket-controller/metadata/@connectionMiddleware.js +16 -0
- package/dist/src/feature/socket-controller/metadata/@socketConnection.js +18 -0
- package/dist/src/feature/socket-controller/metadata/@socketController.js +15 -0
- package/dist/src/feature/socket-controller/metadata/@socketEvent.js +18 -0
- package/dist/src/feature/socket-controller/metadata/SocketControllerMetadataStore.js +65 -0
- package/dist/src/feature/socket-controller/runSocketControllers.js +99 -0
- package/dist/src/index.d.ts +1219 -718
- package/dist/src/index.js +147 -102
- package/package.json +8 -2
- package/dist/src/_virtual/_commonjsHelpers.js +0 -5
- package/dist/src/_virtual/cjs.js +0 -7
- package/dist/src/ai/deepseek/DeepSeekChatBotAdapter.js +0 -107
- package/dist/src/ai/openia/OpenaiChatBotAdapter.js +0 -88
- package/dist/src/channels/cmd/@cmd.js +0 -18
- package/dist/src/channels/cmd/CmdChannel.js +0 -73
- package/dist/src/channels/wabot/WabotDevConnection.js +0 -57
- package/dist/src/channels/wabot/WabotDevSocketContracts.js +0 -10
- package/dist/src/channels/whatsapp/@whatsapp.js +0 -20
- package/dist/src/channels/whatsapp/WhatsAppReceiver.js +0 -59
- package/dist/src/channels/whatsapp/WhatsAppReceiverByDevConnection.js +0 -32
- package/dist/src/channels/whatsapp/WhatsAppReceiverByWebHook.js +0 -63
- package/dist/src/channels/whatsapp/WhatsAppSenderByDevConnection.js +0 -61
- package/dist/src/chatbot/ChatBot.js +0 -51
- package/dist/src/chatbot/ChatBotAdapter.js +0 -72
- package/dist/src/controller/channel/UserResolver.js +0 -21
- package/dist/src/core/IMessageContext.js +0 -12
- package/dist/src/core/chat/ChatItem.js +0 -15
- package/dist/src/core/chat/repository/IChatMemory.js +0 -10
- package/dist/src/core/user/IUserRepository.js +0 -19
- package/dist/src/core/user/User.js +0 -26
- package/dist/src/env/WabotEnv.js +0 -27
- package/dist/src/injection/index.js +0 -4
- package/dist/src/mindset/MindsetOperator.js +0 -101
- package/dist/src/node_modules/@selderee/plugin-htmlparser2/lib/hp2-builder.js +0 -90
- package/dist/src/node_modules/deepmerge/dist/cjs.js +0 -142
- package/dist/src/node_modules/dom-serializer/lib/esm/foreignNames.js +0 -102
- package/dist/src/node_modules/dom-serializer/lib/esm/index.js +0 -186
- package/dist/src/node_modules/domelementtype/lib/esm/index.js +0 -53
- package/dist/src/node_modules/domhandler/lib/esm/index.js +0 -148
- package/dist/src/node_modules/domhandler/lib/esm/node.js +0 -334
- package/dist/src/node_modules/entities/lib/esm/decode.js +0 -458
- package/dist/src/node_modules/entities/lib/esm/decode_codepoint.js +0 -62
- package/dist/src/node_modules/entities/lib/esm/escape.js +0 -99
- package/dist/src/node_modules/entities/lib/esm/generated/decode-data-html.js +0 -8
- package/dist/src/node_modules/entities/lib/esm/generated/decode-data-xml.js +0 -8
- package/dist/src/node_modules/html-to-text/lib/html-to-text.js +0 -2147
- package/dist/src/node_modules/htmlparser2/lib/esm/Parser.js +0 -491
- package/dist/src/node_modules/htmlparser2/lib/esm/Tokenizer.js +0 -928
- package/dist/src/node_modules/htmlparser2/lib/esm/index.js +0 -18
- package/dist/src/node_modules/leac/lib/leac.js +0 -3
- package/dist/src/node_modules/parseley/lib/parseley.js +0 -270
- package/dist/src/node_modules/peberminta/lib/core.js +0 -171
- package/dist/src/node_modules/selderee/lib/selderee.js +0 -380
- package/dist/src/pre-made/module/authentication/AuthenticationModule.js +0 -97
- package/dist/src/pre-made/module/authentication/requests/SendOneTimePasswordRequest.js +0 -25
- package/dist/src/pre-made/module/authentication/requests/ValidateOneTimePasswordRequest.js +0 -25
- package/dist/src/pre-made/module/register-user/RegisterUserModule.js +0 -56
- package/dist/src/pre-made/module/register-user/requests/RegisterUserWithEmailRequest.js +0 -25
- package/dist/src/pre-made/repository/user/pg/PgUserRepository.js +0 -33
- package/dist/src/pre-made/repository/user/ram/RamUserRepository.js +0 -27
- package/dist/src/pre-made/service/EmailService.js +0 -13
- package/dist/src/pre-made/service/OtpService.js +0 -14
- package/dist/src/rest-controller/runRestControllers.js +0 -74
- package/dist/src/server/prepareChatContainer.js +0 -43
- package/dist/src/server/runChannel.js +0 -27
- package/dist/src/server/runServer.js +0 -40
- package/dist/src/validation/metadata/@validable.js +0 -14
- package/dist/src/validation/metadata/ValidationMetadataStore.js +0 -55
- package/dist/src/validation/validators/validateModel.js +0 -47
- /package/dist/src/{pre-made/repository/chat → addon/chat-bot}/ram/RamChatMemory.js +0 -0
- /package/dist/src/{channels → addon/chat-controller}/telegram/TelegramChannelConfig.js +0 -0
- /package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppChannelConfig.js +0 -0
- /package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppRepository.js +0 -0
- /package/dist/src/{injection → core/injection}/Container.js +0 -0
- /package/dist/src/{logger → core/logger}/Logger.js +0 -0
- /package/dist/src/{validation/validators → core/validation/core}/validateIsOptional.js +0 -0
- /package/dist/src/{validation/validators → core/validation/validators/is-boolean}/validateIsBoolean.js +0 -0
- /package/dist/src/{validation/validators → core/validation/validators/is-date}/validateIsDate.js +0 -0
- /package/dist/src/{validation/validators → core/validation/validators/is-not-empty}/validateIsNotEmpty.js +0 -0
- /package/dist/src/{validation/validators → core/validation/validators/is-number}/validateIsNumber.js +0 -0
- /package/dist/src/{validation/validators → core/validation/validators/is-present}/validateIsPresent.js +0 -0
- /package/dist/src/{validation/validators → core/validation/validators/is-string}/validateIsString.js +0 -0
- /package/dist/src/{validation/validators → core/validation/validators/max}/validateMax.js +0 -0
- /package/dist/src/{validation/validators → core/validation/validators/min}/validateMin.js +0 -0
- /package/dist/src/{mindset → feature/mindset}/metadata/functions/decoratorNames.js +0 -0
- /package/dist/src/{mindset → feature/mindset}/metadata/mindsets/decoratorNames.js +0 -0
- /package/dist/src/{mindset → feature/mindset}/metadata/modules/decoratorNames.js +0 -0
- /package/dist/src/{mindset → feature/mindset}/metadata/params/decoratorNames.js +0 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { Env } from '../../../core/env/Env.js';
|
|
3
|
+
import { CustomError } from '../../../core/error/CustomError.js';
|
|
4
|
+
import { singleton } from '../../../core/injection/index.js';
|
|
5
|
+
import { Logger } from '../../../core/logger/Logger.js';
|
|
6
|
+
|
|
7
|
+
let WabotChatAdapter = class WabotChatAdapter {
|
|
8
|
+
apiKey;
|
|
9
|
+
baseUrl;
|
|
10
|
+
logger = new Logger('wabot:wabot-chat-adapter');
|
|
11
|
+
constructor(env) {
|
|
12
|
+
this.apiKey = env.requireString('WABOT_API_KEY');
|
|
13
|
+
this.baseUrl = env.requireString('WABOT_LLM_URL');
|
|
14
|
+
while (this.baseUrl.endsWith('/')) {
|
|
15
|
+
this.baseUrl = this.baseUrl.substring(0, this.baseUrl.length - 1);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
async nextItem(req) {
|
|
19
|
+
const response = await fetch(this.baseUrl + '/chat-bot/next-item', {
|
|
20
|
+
method: 'post',
|
|
21
|
+
headers: {
|
|
22
|
+
Authorization: `Api-Key ${this.apiKey}`,
|
|
23
|
+
'Content-Type': 'application/json',
|
|
24
|
+
},
|
|
25
|
+
body: JSON.stringify(req),
|
|
26
|
+
});
|
|
27
|
+
const data = await response.json();
|
|
28
|
+
if (!response.ok) {
|
|
29
|
+
throw new CustomError({
|
|
30
|
+
message: (data?.error && JSON.stringify(data.error)) ?? 'error calling wabot llm api',
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
return data;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
WabotChatAdapter = __decorate([
|
|
37
|
+
singleton(),
|
|
38
|
+
__metadata("design:paramtypes", [Env])
|
|
39
|
+
], WabotChatAdapter);
|
|
40
|
+
|
|
41
|
+
export { WabotChatAdapter };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { container } from '../../../core/injection/index.js';
|
|
2
|
+
import { ControllerMetadataStore } from '../../../feature/chat-controller/metadata/ControllerMetadataStore.js';
|
|
3
|
+
import '../../../feature/chat-controller/ChatResolver.js';
|
|
4
|
+
import '../../../core/auth/Auth.js';
|
|
5
|
+
import '../../../feature/chat-bot/ChatBot.js';
|
|
6
|
+
import 'uuid';
|
|
7
|
+
import '../../../feature/chat-bot/metadata/ChatBotMetadataStore.js';
|
|
8
|
+
import 'reflect-metadata';
|
|
9
|
+
import '../../../feature/mindset/metadata/MindsetMetadataStore.js';
|
|
10
|
+
import '../../../feature/mindset/MindsetOperator.js';
|
|
11
|
+
import { CmdChannel } from './CmdChannel.js';
|
|
12
|
+
|
|
13
|
+
function cmd() {
|
|
14
|
+
return function (target, propertyKey) {
|
|
15
|
+
const store = container.resolve(ControllerMetadataStore);
|
|
16
|
+
store.saveChannelMetadata({
|
|
17
|
+
channelConstructor: CmdChannel,
|
|
18
|
+
functionName: propertyKey.toString(),
|
|
19
|
+
controllerConstructor: target.constructor,
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { cmd };
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { __decorate } from 'tslib';
|
|
2
|
+
import { injectable } from '../../../core/injection/index.js';
|
|
3
|
+
import * as readline from 'readline';
|
|
4
|
+
import * as fs from 'fs';
|
|
5
|
+
import * as path from 'path';
|
|
6
|
+
import { Random } from '../../../core/random/Random.js';
|
|
7
|
+
|
|
8
|
+
var CmdChannel_1;
|
|
9
|
+
const chatIdPath = '.cmd-channel/id.json';
|
|
10
|
+
const authInfoPath = '.cmd-channel/auth-info.json';
|
|
11
|
+
let CmdChannel = CmdChannel_1 = class CmdChannel {
|
|
12
|
+
authInfo = undefined;
|
|
13
|
+
chatId = undefined;
|
|
14
|
+
rl = readline.createInterface({
|
|
15
|
+
input: process.stdin,
|
|
16
|
+
output: process.stdout,
|
|
17
|
+
});
|
|
18
|
+
callBack = null;
|
|
19
|
+
listen(callback) {
|
|
20
|
+
this.callBack = callback;
|
|
21
|
+
}
|
|
22
|
+
connect() {
|
|
23
|
+
this.rl.on('line', async (input) => {
|
|
24
|
+
const trimmedInput = input.trim();
|
|
25
|
+
if (!trimmedInput) {
|
|
26
|
+
this.rl.prompt();
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (trimmedInput.toLowerCase() === 'exit') {
|
|
30
|
+
this.rl.close();
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (this.chatId === undefined) {
|
|
34
|
+
this.chatId = readJsonFromFile(chatIdPath) ?? undefined;
|
|
35
|
+
if (!this.chatId) {
|
|
36
|
+
this.chatId = Random.alphaNumericLowerCase(10);
|
|
37
|
+
writeJsonToFile(chatIdPath, this.chatId);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const chatConnection = {
|
|
41
|
+
id: this.chatId,
|
|
42
|
+
chatType: 'PRIVATE',
|
|
43
|
+
channelName: CmdChannel_1.name,
|
|
44
|
+
};
|
|
45
|
+
if (!this.callBack)
|
|
46
|
+
return;
|
|
47
|
+
if (this.authInfo === undefined) {
|
|
48
|
+
this.authInfo = readJsonFromFile(authInfoPath);
|
|
49
|
+
}
|
|
50
|
+
this.callBack({
|
|
51
|
+
chatConnection,
|
|
52
|
+
message: {
|
|
53
|
+
text: trimmedInput,
|
|
54
|
+
},
|
|
55
|
+
reply: (message) => {
|
|
56
|
+
console.log(`\n[${message.senderName}]: ${message.text}\n`);
|
|
57
|
+
this.rl.prompt();
|
|
58
|
+
},
|
|
59
|
+
authInfo: this.authInfo || undefined,
|
|
60
|
+
setAuthInfo: (authInfo) => {
|
|
61
|
+
this.authInfo = authInfo || null;
|
|
62
|
+
writeJsonToFile(authInfoPath, this.authInfo);
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
CmdChannel = CmdChannel_1 = __decorate([
|
|
69
|
+
injectable()
|
|
70
|
+
], CmdChannel);
|
|
71
|
+
function writeJsonToFile(filename, data) {
|
|
72
|
+
const filePath = path.resolve(process.cwd(), filename);
|
|
73
|
+
const dir = path.dirname(filePath);
|
|
74
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
75
|
+
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf-8');
|
|
76
|
+
}
|
|
77
|
+
function readJsonFromFile(filename) {
|
|
78
|
+
const filePath = path.resolve(process.cwd(), filename);
|
|
79
|
+
if (!fs.existsSync(filePath)) {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
const jsonData = fs.readFileSync(filePath, 'utf-8');
|
|
84
|
+
return JSON.parse(jsonData);
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export { CmdChannel, readJsonFromFile, writeJsonToFile };
|
|
@@ -1,7 +1,13 @@
|
|
|
1
|
-
import '
|
|
2
|
-
import '
|
|
3
|
-
import
|
|
4
|
-
import
|
|
1
|
+
import { container } from '../../../core/injection/index.js';
|
|
2
|
+
import { ControllerMetadataStore } from '../../../feature/chat-controller/metadata/ControllerMetadataStore.js';
|
|
3
|
+
import '../../../feature/chat-controller/ChatResolver.js';
|
|
4
|
+
import '../../../core/auth/Auth.js';
|
|
5
|
+
import '../../../feature/chat-bot/ChatBot.js';
|
|
6
|
+
import 'uuid';
|
|
7
|
+
import '../../../feature/chat-bot/metadata/ChatBotMetadataStore.js';
|
|
8
|
+
import 'reflect-metadata';
|
|
9
|
+
import '../../../feature/mindset/metadata/MindsetMetadataStore.js';
|
|
10
|
+
import '../../../feature/mindset/MindsetOperator.js';
|
|
5
11
|
import { SocketChannel } from './SocketChannel.js';
|
|
6
12
|
import { SocketChannelConfig } from './SocketChannelConfig.js';
|
|
7
13
|
|
|
@@ -1,24 +1,17 @@
|
|
|
1
1
|
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { injectable } from '../../../core/injection/index.js';
|
|
3
|
+
import { SocketServerProvider } from '../../../feature/socket/SocketServerProvider.js';
|
|
2
4
|
import { SocketChannelConfig } from './SocketChannelConfig.js';
|
|
3
|
-
import { injectable } from '../../injection/index.js';
|
|
4
|
-
import { ChatResolver } from '../../controller/channel/ChatResolver.js';
|
|
5
|
-
import { UserResolver } from '../../controller/channel/UserResolver.js';
|
|
6
|
-
import '../../controller/metadata/ControllerMetadataStore.js';
|
|
7
|
-
import { SocketServerProvider } from './SocketServerProvider.js';
|
|
8
5
|
|
|
9
6
|
var SocketChannel_1;
|
|
10
7
|
let SocketChannel = SocketChannel_1 = class SocketChannel {
|
|
11
8
|
config;
|
|
12
9
|
socketServerProvider;
|
|
13
|
-
chatResolver;
|
|
14
|
-
userResolver;
|
|
15
10
|
callBack = null;
|
|
16
11
|
server;
|
|
17
|
-
constructor(config, socketServerProvider
|
|
12
|
+
constructor(config, socketServerProvider) {
|
|
18
13
|
this.config = config;
|
|
19
14
|
this.socketServerProvider = socketServerProvider;
|
|
20
|
-
this.chatResolver = chatResolver;
|
|
21
|
-
this.userResolver = userResolver;
|
|
22
15
|
this.server = this.socketServerProvider.getSocketServer();
|
|
23
16
|
}
|
|
24
17
|
listen(callback) {
|
|
@@ -42,26 +35,21 @@ let SocketChannel = SocketChannel_1 = class SocketChannel {
|
|
|
42
35
|
chatType: 'PRIVATE',
|
|
43
36
|
channelName: SocketChannel_1.name,
|
|
44
37
|
};
|
|
45
|
-
const chat = await this.chatResolver.resolve(chatConnection);
|
|
46
|
-
const userConnection = {
|
|
47
|
-
id: message.userId,
|
|
48
|
-
channelName: SocketChannel_1.name,
|
|
49
|
-
};
|
|
50
|
-
const user = await this.userResolver.resolve(userConnection);
|
|
51
38
|
if (!this.callBack)
|
|
52
39
|
return;
|
|
53
40
|
this.callBack({
|
|
54
|
-
|
|
55
|
-
user,
|
|
41
|
+
chatConnection,
|
|
56
42
|
message: {
|
|
57
|
-
chatConnection,
|
|
58
|
-
userConnection,
|
|
59
43
|
text: trimmedInput,
|
|
60
44
|
senderName: message.senderName,
|
|
61
45
|
},
|
|
62
46
|
reply: (message) => {
|
|
63
47
|
socket.emit(this.config.channel, message);
|
|
64
48
|
},
|
|
49
|
+
authInfo: socket.data.authInfo,
|
|
50
|
+
setAuthInfo: (authInfo) => {
|
|
51
|
+
socket.data.authInfo = authInfo;
|
|
52
|
+
},
|
|
65
53
|
});
|
|
66
54
|
});
|
|
67
55
|
});
|
|
@@ -71,9 +59,7 @@ let SocketChannel = SocketChannel_1 = class SocketChannel {
|
|
|
71
59
|
SocketChannel = SocketChannel_1 = __decorate([
|
|
72
60
|
injectable(),
|
|
73
61
|
__metadata("design:paramtypes", [SocketChannelConfig,
|
|
74
|
-
SocketServerProvider
|
|
75
|
-
ChatResolver,
|
|
76
|
-
UserResolver])
|
|
62
|
+
SocketServerProvider])
|
|
77
63
|
], SocketChannel);
|
|
78
64
|
|
|
79
65
|
export { SocketChannel };
|
|
@@ -1,7 +1,13 @@
|
|
|
1
|
-
import { container } from '
|
|
2
|
-
import '
|
|
3
|
-
import '
|
|
4
|
-
import
|
|
1
|
+
import { container } from '../../../core/injection/index.js';
|
|
2
|
+
import { ControllerMetadataStore } from '../../../feature/chat-controller/metadata/ControllerMetadataStore.js';
|
|
3
|
+
import '../../../feature/chat-controller/ChatResolver.js';
|
|
4
|
+
import '../../../core/auth/Auth.js';
|
|
5
|
+
import '../../../feature/chat-bot/ChatBot.js';
|
|
6
|
+
import 'uuid';
|
|
7
|
+
import '../../../feature/chat-bot/metadata/ChatBotMetadataStore.js';
|
|
8
|
+
import 'reflect-metadata';
|
|
9
|
+
import '../../../feature/mindset/metadata/MindsetMetadataStore.js';
|
|
10
|
+
import '../../../feature/mindset/MindsetOperator.js';
|
|
5
11
|
import { TelegramChannel } from './TelegramChannel.js';
|
|
6
12
|
import { TelegramChannelConfig } from './TelegramChannelConfig.js';
|
|
7
13
|
|
|
@@ -1,21 +1,14 @@
|
|
|
1
1
|
import { __decorate, __metadata } from 'tslib';
|
|
2
|
-
import { ChatResolver } from '../../controller/channel/ChatResolver.js';
|
|
3
|
-
import { UserResolver } from '../../controller/channel/UserResolver.js';
|
|
4
|
-
import { injectable } from '../../injection/index.js';
|
|
5
|
-
import '../../controller/metadata/ControllerMetadataStore.js';
|
|
6
2
|
import { Bot } from 'grammy';
|
|
7
3
|
import { TelegramChannelConfig } from './TelegramChannelConfig.js';
|
|
4
|
+
import { injectable } from '../../../core/injection/index.js';
|
|
8
5
|
|
|
9
6
|
var TelegramChannel_1;
|
|
10
7
|
let TelegramChannel = TelegramChannel_1 = class TelegramChannel {
|
|
11
8
|
config;
|
|
12
|
-
chatResolver;
|
|
13
|
-
userResolver;
|
|
14
9
|
bot;
|
|
15
|
-
constructor(config
|
|
10
|
+
constructor(config) {
|
|
16
11
|
this.config = config;
|
|
17
|
-
this.chatResolver = chatResolver;
|
|
18
|
-
this.userResolver = userResolver;
|
|
19
12
|
this.bot = new Bot(this.config.botToken);
|
|
20
13
|
}
|
|
21
14
|
listen(callback) {
|
|
@@ -28,18 +21,9 @@ let TelegramChannel = TelegramChannel_1 = class TelegramChannel {
|
|
|
28
21
|
chatType: ctx.message.chat.type === 'private' ? 'PRIVATE' : 'GROUP',
|
|
29
22
|
channelName: TelegramChannel_1.name,
|
|
30
23
|
};
|
|
31
|
-
const chat = await this.chatResolver.resolve(chatConnection);
|
|
32
|
-
const userConnection = {
|
|
33
|
-
id: ctx.from.id.toString(),
|
|
34
|
-
channelName: TelegramChannel_1.name,
|
|
35
|
-
};
|
|
36
|
-
const user = await this.userResolver.resolve(userConnection);
|
|
37
24
|
callback({
|
|
38
|
-
|
|
39
|
-
user,
|
|
25
|
+
chatConnection,
|
|
40
26
|
message: {
|
|
41
|
-
chatConnection,
|
|
42
|
-
userConnection,
|
|
43
27
|
senderName: ctx.from.first_name,
|
|
44
28
|
text: ctx.message.text,
|
|
45
29
|
},
|
|
@@ -55,9 +39,7 @@ let TelegramChannel = TelegramChannel_1 = class TelegramChannel {
|
|
|
55
39
|
};
|
|
56
40
|
TelegramChannel = TelegramChannel_1 = __decorate([
|
|
57
41
|
injectable(),
|
|
58
|
-
__metadata("design:paramtypes", [TelegramChannelConfig
|
|
59
|
-
ChatResolver,
|
|
60
|
-
UserResolver])
|
|
42
|
+
__metadata("design:paramtypes", [TelegramChannelConfig])
|
|
61
43
|
], TelegramChannel);
|
|
62
44
|
|
|
63
45
|
export { TelegramChannel };
|
|
@@ -0,0 +1,26 @@
|
|
|
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 '../../../core/auth/Auth.js';
|
|
6
|
+
import '../../../feature/chat-bot/ChatBot.js';
|
|
7
|
+
import 'uuid';
|
|
8
|
+
import '../../../feature/chat-bot/metadata/ChatBotMetadataStore.js';
|
|
9
|
+
import 'reflect-metadata';
|
|
10
|
+
import '../../../feature/mindset/metadata/MindsetMetadataStore.js';
|
|
11
|
+
import '../../../feature/mindset/MindsetOperator.js';
|
|
12
|
+
import { WhatsAppChannel } from './WhatsAppChannel.js';
|
|
13
|
+
|
|
14
|
+
function whatsApp(config) {
|
|
15
|
+
return function (target, propertyKey) {
|
|
16
|
+
const store = container.resolve(ControllerMetadataStore);
|
|
17
|
+
store.saveChannelMetadata({
|
|
18
|
+
channelConstructor: WhatsAppChannel,
|
|
19
|
+
functionName: propertyKey.toString(),
|
|
20
|
+
controllerConstructor: target.constructor,
|
|
21
|
+
channelConfig: new WhatsappChannelConfig(typeof config === 'string' ? config : config.number),
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { whatsApp };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { __decorate, __metadata } from 'tslib';
|
|
2
|
-
import { singleton } from '
|
|
2
|
+
import { singleton } from '../../../core/injection/index.js';
|
|
3
3
|
import { WhatsApp } from './WhatsApp.js';
|
|
4
|
-
import {
|
|
4
|
+
import { Env } from '../../../core/env/Env.js';
|
|
5
5
|
|
|
6
6
|
let EnvWhatsAppRepository = class EnvWhatsAppRepository {
|
|
7
7
|
env;
|
|
@@ -43,7 +43,7 @@ let EnvWhatsAppRepository = class EnvWhatsAppRepository {
|
|
|
43
43
|
};
|
|
44
44
|
EnvWhatsAppRepository = __decorate([
|
|
45
45
|
singleton(),
|
|
46
|
-
__metadata("design:paramtypes", [
|
|
46
|
+
__metadata("design:paramtypes", [Env])
|
|
47
47
|
], EnvWhatsAppRepository);
|
|
48
48
|
|
|
49
49
|
export { EnvWhatsAppRepository };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { __decorate, __metadata } from 'tslib';
|
|
2
2
|
import { Pool } from 'pg';
|
|
3
3
|
import { WhatsApp } from './WhatsApp.js';
|
|
4
|
-
import { singleton } from '
|
|
5
|
-
import { PgCrudRepository } from '
|
|
4
|
+
import { singleton } from '../../../core/injection/index.js';
|
|
5
|
+
import { PgCrudRepository } from '../../../feature/pg/PgCrudRepository.js';
|
|
6
6
|
|
|
7
7
|
let PgWhatsAppRepository = class PgWhatsAppRepository extends PgCrudRepository {
|
|
8
8
|
constructor(pool) {
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import '
|
|
2
|
-
import { Persistent } from '../../core/Persistent.js';
|
|
3
|
-
import '../../core/user/IUserRepository.js';
|
|
1
|
+
import { Entity } from '../../../core/entity/Entity.js';
|
|
4
2
|
|
|
5
|
-
class WhatsApp extends
|
|
3
|
+
class WhatsApp extends Entity {
|
|
6
4
|
getVerifyToken() {
|
|
7
5
|
return this.data.verifyToken;
|
|
8
6
|
}
|
|
@@ -1,24 +1,17 @@
|
|
|
1
1
|
import { __decorate, __metadata } from 'tslib';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { injectable } from '../../injection/index.js';
|
|
5
|
-
import '../../controller/metadata/ControllerMetadataStore.js';
|
|
6
|
-
import { Logger } from '../../logger/Logger.js';
|
|
2
|
+
import { injectable } from '../../../core/injection/index.js';
|
|
3
|
+
import { Logger } from '../../../core/logger/Logger.js';
|
|
7
4
|
import { WhatsappChannelConfig } from './WhatsAppChannelConfig.js';
|
|
8
5
|
import { WhatsAppReceiver } from './WhatsAppReceiver.js';
|
|
9
6
|
import { WhatsAppSender } from './WhatsAppSender.js';
|
|
10
7
|
|
|
11
8
|
let WhatsAppChannel = class WhatsAppChannel {
|
|
12
9
|
config;
|
|
13
|
-
chatResolver;
|
|
14
|
-
userResolver;
|
|
15
10
|
sender;
|
|
16
11
|
receiver;
|
|
17
12
|
logger = new Logger('wabot:whatsapp-channel');
|
|
18
|
-
constructor(config,
|
|
13
|
+
constructor(config, sender, receiver) {
|
|
19
14
|
this.config = config;
|
|
20
|
-
this.chatResolver = chatResolver;
|
|
21
|
-
this.userResolver = userResolver;
|
|
22
15
|
this.sender = sender;
|
|
23
16
|
this.receiver = receiver;
|
|
24
17
|
}
|
|
@@ -27,16 +20,13 @@ let WhatsAppChannel = class WhatsAppChannel {
|
|
|
27
20
|
to: this.config.number,
|
|
28
21
|
listener: async (message) => {
|
|
29
22
|
try {
|
|
30
|
-
const chat = await this.chatResolver.resolve(message.chatConnection);
|
|
31
|
-
const user = await this.userResolver.resolve(message.userConnection);
|
|
32
23
|
callback({
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
message,
|
|
24
|
+
chatConnection: message.chatConnection,
|
|
25
|
+
message: message.message,
|
|
36
26
|
reply: (replyMessage) => {
|
|
37
27
|
this.sender.sendWhatsApp({
|
|
38
28
|
from: this.config.number,
|
|
39
|
-
to: message.
|
|
29
|
+
to: message.chatConnection.id,
|
|
40
30
|
message: replyMessage,
|
|
41
31
|
});
|
|
42
32
|
},
|
|
@@ -55,8 +45,6 @@ let WhatsAppChannel = class WhatsAppChannel {
|
|
|
55
45
|
WhatsAppChannel = __decorate([
|
|
56
46
|
injectable(),
|
|
57
47
|
__metadata("design:paramtypes", [WhatsappChannelConfig,
|
|
58
|
-
ChatResolver,
|
|
59
|
-
UserResolver,
|
|
60
48
|
WhatsAppSender,
|
|
61
49
|
WhatsAppReceiver])
|
|
62
50
|
], WhatsAppChannel);
|
|
@@ -1,53 +1,29 @@
|
|
|
1
|
-
import '
|
|
2
|
-
import { ChatItem } from '
|
|
3
|
-
import '
|
|
1
|
+
import '../../../feature/chat-bot/ChatBot.js';
|
|
2
|
+
import { ChatItem } from '../../../feature/chat-bot/ChatItem.js';
|
|
3
|
+
import '../../../core/injection/index.js';
|
|
4
|
+
import 'uuid';
|
|
5
|
+
import '../../../feature/chat-bot/metadata/ChatBotMetadataStore.js';
|
|
4
6
|
|
|
5
7
|
class WhatsAppSender {
|
|
6
|
-
logger;
|
|
7
8
|
chatRepository;
|
|
8
9
|
chatResolver;
|
|
9
10
|
whatsAppRepository;
|
|
10
|
-
constructor(
|
|
11
|
-
this.logger = logger;
|
|
11
|
+
constructor(chatRepository, chatResolver, whatsAppRepository) {
|
|
12
12
|
this.chatRepository = chatRepository;
|
|
13
13
|
this.chatResolver = chatResolver;
|
|
14
14
|
this.whatsAppRepository = whatsAppRepository;
|
|
15
15
|
}
|
|
16
16
|
async sendWhatsApp(request, options) {
|
|
17
|
-
|
|
18
|
-
await this.handleSendRequest(request);
|
|
19
|
-
if (options?.writeChatMemory) {
|
|
20
|
-
await this.writePrivateChatMemory(request.message, request.to);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
catch (error) {
|
|
24
|
-
this.logger.error(`Error sending WhatsApp message: ${error}`);
|
|
25
|
-
}
|
|
17
|
+
throw new Error('Not implemented');
|
|
26
18
|
}
|
|
27
19
|
async sendWhatsAppTemplate(request, options) {
|
|
28
|
-
|
|
29
|
-
await this.handleSendTemplateRequest(request);
|
|
30
|
-
if (options?.writeChatMemory) {
|
|
31
|
-
const message = await this.resolveTemplateToChatMessage(request);
|
|
32
|
-
await this.writePrivateChatMemory(message, request.to);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
catch (error) {
|
|
36
|
-
this.logger.error(`Error sending WhatsApp message: ${error}`);
|
|
37
|
-
}
|
|
20
|
+
throw new Error('Not implemented');
|
|
38
21
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const template = await this.handleGetWhatsAppTemplate(request);
|
|
42
|
-
return template;
|
|
43
|
-
}
|
|
44
|
-
catch (error) {
|
|
45
|
-
this.logger.error(error);
|
|
46
|
-
throw new Error('Error getting WhatsApp template:', { cause: error });
|
|
47
|
-
}
|
|
22
|
+
getWhatsAppTemplate(request) {
|
|
23
|
+
throw new Error('Not implemented');
|
|
48
24
|
}
|
|
49
|
-
async
|
|
50
|
-
const template = await this.
|
|
25
|
+
async mapTemplateToChatMessage(request) {
|
|
26
|
+
const template = await this.getWhatsAppTemplate({
|
|
51
27
|
from: request.from,
|
|
52
28
|
templateName: request.templateMessage.templateName,
|
|
53
29
|
languageCode: request.templateMessage.languageCode,
|
|
@@ -71,10 +47,10 @@ class WhatsAppSender {
|
|
|
71
47
|
channelName: 'WhatsAppChannel',
|
|
72
48
|
};
|
|
73
49
|
const chat = await this.chatResolver.resolve(chatConnection);
|
|
74
|
-
const chatMemory = await this.chatRepository.findMemory(chat.
|
|
50
|
+
const chatMemory = await this.chatRepository.findMemory(chat.id);
|
|
75
51
|
const chatItem = new ChatItem({
|
|
76
|
-
type: '
|
|
77
|
-
|
|
52
|
+
type: 'botMessage',
|
|
53
|
+
botMessage: message,
|
|
78
54
|
});
|
|
79
55
|
await chatMemory.create(chatItem);
|
|
80
56
|
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Logger } from '../../../../core/logger/Logger.js';
|
|
2
|
+
import { json } from 'express';
|
|
3
|
+
import { WhatsAppReceiver } from '../WhatsAppReceiver.js';
|
|
4
|
+
|
|
5
|
+
class WhatsAppReceiverByCloudApi extends WhatsAppReceiver {
|
|
6
|
+
expressProvider;
|
|
7
|
+
whatsAppRepository;
|
|
8
|
+
listeners = new Map();
|
|
9
|
+
expressApp;
|
|
10
|
+
logger = new Logger('wabot:whatsapp-receiver-by-webhook');
|
|
11
|
+
webhookPath = '/whatsapp/web-hook/:slug';
|
|
12
|
+
constructor(expressProvider, whatsAppRepository) {
|
|
13
|
+
super();
|
|
14
|
+
this.expressProvider = expressProvider;
|
|
15
|
+
this.whatsAppRepository = whatsAppRepository;
|
|
16
|
+
this.expressApp = this.expressProvider.getExpress();
|
|
17
|
+
}
|
|
18
|
+
async connect() {
|
|
19
|
+
this.expressApp.get(this.webhookPath, json(), async (req, res) => {
|
|
20
|
+
try {
|
|
21
|
+
let mode = req.query['hub.mode'];
|
|
22
|
+
let token = req.query['hub.verify_token'];
|
|
23
|
+
let challenge = req.query['hub.challenge'];
|
|
24
|
+
if (!mode || !token || !challenge) {
|
|
25
|
+
res.sendStatus(400);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const whatsApp = await this.whatsAppRepository.findBySlug(req.params.slug);
|
|
29
|
+
if (!whatsApp || mode !== 'subscribe' || token !== whatsApp.getVerifyToken()) {
|
|
30
|
+
res.sendStatus(403);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
res.status(200).send(challenge);
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
this.logger.error(e);
|
|
37
|
+
res.sendStatus(500);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
this.expressApp.post(this.webhookPath, json(), (req, res) => {
|
|
42
|
+
const payload = req.body;
|
|
43
|
+
this.handlePayload(payload);
|
|
44
|
+
res.sendStatus(200);
|
|
45
|
+
});
|
|
46
|
+
this.expressProvider.listen();
|
|
47
|
+
}
|
|
48
|
+
listenMessage(request) {
|
|
49
|
+
this.listeners.set(request.to, request.listener);
|
|
50
|
+
}
|
|
51
|
+
async handlePayload(payload) {
|
|
52
|
+
try {
|
|
53
|
+
for (const entry of payload.entry) {
|
|
54
|
+
for (const change of entry.changes) {
|
|
55
|
+
if (change.field !== 'messages' || !change.value.messages || !change.value.contacts) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
for (const message of change.value.messages) {
|
|
59
|
+
const contact = change.value.contacts.find((x) => x.wa_id === message.from);
|
|
60
|
+
if (!contact) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
await this.emmitMessage(change.value.metadata, message, contact);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
this.logger.error(err);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async emmitMessage(metadata, message, contact) {
|
|
73
|
+
const listener = this.listeners.get(metadata.display_phone_number);
|
|
74
|
+
if (!listener) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (message.type !== 'text') {
|
|
78
|
+
this.logger.error(`message type ${message.type} is not supported yet`);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const channelName = 'WhatsAppChannel';
|
|
82
|
+
const chatConnection = {
|
|
83
|
+
id: contact.wa_id,
|
|
84
|
+
chatType: 'PRIVATE',
|
|
85
|
+
channelName,
|
|
86
|
+
};
|
|
87
|
+
await listener({
|
|
88
|
+
chatConnection,
|
|
89
|
+
message: {
|
|
90
|
+
senderName: contact.profile.name,
|
|
91
|
+
text: message.text.body,
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export { WhatsAppReceiverByCloudApi };
|