@adatechnology/meta-whatsapp-module 0.8.0 → 0.9.0
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/README.md +137 -0
- package/dist/index.cjs +341 -162
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +79 -23
- package/dist/index.d.ts +79 -23
- package/dist/index.js +304 -127
- package/dist/index.js.map +1 -1
- package/dist/migrations-journal/0000_freezing_switch.sql +41 -0
- package/dist/migrations-journal/0001_military_leper_queen.sql +15 -0
- package/dist/migrations-journal/0002_illegal_black_tarantula.sql +5 -0
- package/dist/migrations-journal/0003_normal_viper.sql +10 -0
- package/dist/migrations-journal/0004_widen_message_type.sql +1 -0
- package/dist/migrations-journal/0005_message_moderation.sql +3 -0
- package/dist/migrations-journal/0006_conversation_documents.sql +19 -0
- package/dist/migrations-journal/0007_flow_media.sql +18 -0
- package/dist/migrations-journal/0008_message_transcription.sql +5 -0
- package/dist/migrations-journal/0009_settings_transcription_policy.sql +2 -0
- package/dist/migrations-journal/0010_flow_media_meta_ids.sql +10 -0
- package/dist/migrations-journal/meta/_journal.json +83 -0
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -1010,8 +1010,9 @@ var LogMessageUseCase = class {
|
|
|
1010
1010
|
};
|
|
1011
1011
|
|
|
1012
1012
|
// src/use-cases/SendMessage.use-case.ts
|
|
1013
|
-
import { WindowExpiredError } from "@adatechnology/meta-whatsapp-contracts";
|
|
1013
|
+
import { WindowExpiredError, assertInteractiveWithinLimits } from "@adatechnology/meta-whatsapp-contracts";
|
|
1014
1014
|
var WHATSAPP_WINDOW_HOURS = 24;
|
|
1015
|
+
var DEFAULT_FALLBACK_LIST_LABEL = "Op\xE7\xF5es";
|
|
1015
1016
|
var SendMessageUseCase = class {
|
|
1016
1017
|
static {
|
|
1017
1018
|
__name(this, "SendMessageUseCase");
|
|
@@ -1096,6 +1097,54 @@ var SendMessageUseCase = class {
|
|
|
1096
1097
|
}
|
|
1097
1098
|
return saved;
|
|
1098
1099
|
}
|
|
1100
|
+
/**
|
|
1101
|
+
* Botões ou lista, pelo mesmo caminho do `sendText`: janela, envio, transcript (idempotente pelo
|
|
1102
|
+
* `waMessageId`). Os limites da Meta são conferidos ANTES de tudo — mensagem fora do teto a Graph
|
|
1103
|
+
* API recusa inteira, e o erro local (`InteractiveLimitExceededError`) diz qual campo corrigir.
|
|
1104
|
+
*
|
|
1105
|
+
* O transcript guarda `type: 'interactive'`, o corpo em `content` e a mensagem inteira (ids e
|
|
1106
|
+
* títulos das opções) em `payload.interactive`, para o histórico mostrar o que foi oferecido.
|
|
1107
|
+
*/
|
|
1108
|
+
async sendInteractive(params) {
|
|
1109
|
+
assertInteractiveWithinLimits(params.interactive);
|
|
1110
|
+
await this.assertWithinWindow(params.companyId, params.whatsappNumber);
|
|
1111
|
+
const { externalMessageId } = await this.sendInteractiveThroughChannel(params);
|
|
1112
|
+
return this.logMessage.execute({
|
|
1113
|
+
companyId: params.companyId,
|
|
1114
|
+
whatsappNumber: params.whatsappNumber,
|
|
1115
|
+
direction: "outbound",
|
|
1116
|
+
sender: params.sender,
|
|
1117
|
+
agentUserId: params.agentUserId ?? null,
|
|
1118
|
+
type: "interactive",
|
|
1119
|
+
content: params.interactive.body,
|
|
1120
|
+
payload: {
|
|
1121
|
+
interactive: params.interactive
|
|
1122
|
+
},
|
|
1123
|
+
waMessageId: externalMessageId,
|
|
1124
|
+
status: "sent",
|
|
1125
|
+
startState: params.startState
|
|
1126
|
+
});
|
|
1127
|
+
}
|
|
1128
|
+
sendInteractiveThroughChannel(params) {
|
|
1129
|
+
const interactive = params.interactive;
|
|
1130
|
+
const to = params.whatsappNumber;
|
|
1131
|
+
if (interactive.kind === "list") {
|
|
1132
|
+
return this.channel.sendInteractiveList({
|
|
1133
|
+
to,
|
|
1134
|
+
body: interactive.body,
|
|
1135
|
+
buttonLabel: interactive.buttonLabel,
|
|
1136
|
+
sections: interactive.sections
|
|
1137
|
+
});
|
|
1138
|
+
}
|
|
1139
|
+
if (this.channel.sendInteractiveButtons) {
|
|
1140
|
+
return this.channel.sendInteractiveButtons({
|
|
1141
|
+
to,
|
|
1142
|
+
body: interactive.body,
|
|
1143
|
+
buttons: interactive.buttons
|
|
1144
|
+
});
|
|
1145
|
+
}
|
|
1146
|
+
return this.channel.sendInteractiveList(buttonsAsList(interactive, to, params.fallbackListLabel));
|
|
1147
|
+
}
|
|
1099
1148
|
// Template é o único envio que ignora a janela — é justamente o mecanismo que a Meta oferece
|
|
1100
1149
|
// para reabri-la.
|
|
1101
1150
|
async sendTemplate(params) {
|
|
@@ -1123,6 +1172,15 @@ var SendMessageUseCase = class {
|
|
|
1123
1172
|
});
|
|
1124
1173
|
}
|
|
1125
1174
|
};
|
|
1175
|
+
function buttonsAsList(interactive, to, label) {
|
|
1176
|
+
return {
|
|
1177
|
+
to,
|
|
1178
|
+
body: interactive.body,
|
|
1179
|
+
buttonLabel: label ?? DEFAULT_FALLBACK_LIST_LABEL,
|
|
1180
|
+
rows: interactive.buttons
|
|
1181
|
+
};
|
|
1182
|
+
}
|
|
1183
|
+
__name(buttonsAsList, "buttonsAsList");
|
|
1126
1184
|
function mediaTypeFor(mimeType) {
|
|
1127
1185
|
if (mimeType.startsWith("image/")) return "image";
|
|
1128
1186
|
if (mimeType.startsWith("audio/")) return "audio";
|
|
@@ -1138,9 +1196,11 @@ var TakeoverConversationUseCase = class {
|
|
|
1138
1196
|
}
|
|
1139
1197
|
sessionRepository;
|
|
1140
1198
|
realtime;
|
|
1141
|
-
|
|
1199
|
+
hooks;
|
|
1200
|
+
constructor(sessionRepository, realtime, hooks) {
|
|
1142
1201
|
this.sessionRepository = sessionRepository;
|
|
1143
1202
|
this.realtime = realtime;
|
|
1203
|
+
this.hooks = hooks;
|
|
1144
1204
|
}
|
|
1145
1205
|
async execute(params) {
|
|
1146
1206
|
await this.sessionRepository.takeover(params.companyId, params.whatsappNumber, params.agentUserId);
|
|
@@ -1149,6 +1209,15 @@ var TakeoverConversationUseCase = class {
|
|
|
1149
1209
|
assignedUserId: params.agentUserId
|
|
1150
1210
|
});
|
|
1151
1211
|
this.realtime?.emit("global", "data-changed", {});
|
|
1212
|
+
await this.hooks?.onConversationModeChanged?.({
|
|
1213
|
+
companyId: params.companyId,
|
|
1214
|
+
whatsappNumber: params.whatsappNumber,
|
|
1215
|
+
kind: "takeover",
|
|
1216
|
+
mode: "human",
|
|
1217
|
+
assignedUserId: params.agentUserId,
|
|
1218
|
+
actorId: params.actorId ?? params.agentUserId,
|
|
1219
|
+
occurredAt: /* @__PURE__ */ new Date()
|
|
1220
|
+
});
|
|
1152
1221
|
}
|
|
1153
1222
|
};
|
|
1154
1223
|
|
|
@@ -1159,9 +1228,11 @@ var ReleaseConversationUseCase = class {
|
|
|
1159
1228
|
}
|
|
1160
1229
|
sessionRepository;
|
|
1161
1230
|
realtime;
|
|
1162
|
-
|
|
1231
|
+
hooks;
|
|
1232
|
+
constructor(sessionRepository, realtime, hooks) {
|
|
1163
1233
|
this.sessionRepository = sessionRepository;
|
|
1164
1234
|
this.realtime = realtime;
|
|
1235
|
+
this.hooks = hooks;
|
|
1165
1236
|
}
|
|
1166
1237
|
async execute(params) {
|
|
1167
1238
|
await this.sessionRepository.release(params.companyId, params.whatsappNumber);
|
|
@@ -1170,6 +1241,166 @@ var ReleaseConversationUseCase = class {
|
|
|
1170
1241
|
assignedUserId: null
|
|
1171
1242
|
});
|
|
1172
1243
|
this.realtime?.emit("global", "data-changed", {});
|
|
1244
|
+
await this.hooks?.onConversationModeChanged?.({
|
|
1245
|
+
companyId: params.companyId,
|
|
1246
|
+
whatsappNumber: params.whatsappNumber,
|
|
1247
|
+
kind: "release",
|
|
1248
|
+
mode: "bot",
|
|
1249
|
+
assignedUserId: null,
|
|
1250
|
+
actorId: params.actorId ?? null,
|
|
1251
|
+
occurredAt: /* @__PURE__ */ new Date()
|
|
1252
|
+
});
|
|
1253
|
+
}
|
|
1254
|
+
};
|
|
1255
|
+
|
|
1256
|
+
// src/use-cases/RequestHumanConversation.use-case.ts
|
|
1257
|
+
import { SessionNotFoundError } from "@adatechnology/meta-whatsapp-contracts";
|
|
1258
|
+
|
|
1259
|
+
// src/channel/inboundDispatch.ts
|
|
1260
|
+
import { isDeterministicMetaRejection } from "@adatechnology/meta-graph-core";
|
|
1261
|
+
function buildInboundJobId(job) {
|
|
1262
|
+
return job.kind === "message" ? `wa-inbound-message:${job.message.id}` : `wa-inbound-status:${job.status.id}:${job.status.status}`;
|
|
1263
|
+
}
|
|
1264
|
+
__name(buildInboundJobId, "buildInboundJobId");
|
|
1265
|
+
function toSessionContract(row) {
|
|
1266
|
+
return {
|
|
1267
|
+
id: row.id,
|
|
1268
|
+
companyId: row.companyId,
|
|
1269
|
+
whatsappNumber: row.whatsappNumber,
|
|
1270
|
+
currentState: row.currentState,
|
|
1271
|
+
flowKey: row.flowKey,
|
|
1272
|
+
currentNodeId: row.currentNodeId,
|
|
1273
|
+
context: row.context,
|
|
1274
|
+
mode: row.mode,
|
|
1275
|
+
assignedUserId: row.assignedUserId,
|
|
1276
|
+
humanRequestedAt: row.humanRequestedAt?.toISOString() ?? null,
|
|
1277
|
+
lastInboundAt: row.lastInboundAt?.toISOString() ?? null,
|
|
1278
|
+
lastAgentReadAt: row.lastAgentReadAt?.toISOString() ?? null,
|
|
1279
|
+
lastActivity: row.lastActivity.toISOString(),
|
|
1280
|
+
createdAt: row.createdAt.toISOString(),
|
|
1281
|
+
updatedAt: row.updatedAt.toISOString()
|
|
1282
|
+
};
|
|
1283
|
+
}
|
|
1284
|
+
__name(toSessionContract, "toSessionContract");
|
|
1285
|
+
function extractAnswer(message) {
|
|
1286
|
+
const interactive = message.interactive;
|
|
1287
|
+
if (interactive?.button_reply) return interactive.button_reply.id;
|
|
1288
|
+
if (interactive?.list_reply) return interactive.list_reply.id;
|
|
1289
|
+
return message.text?.body;
|
|
1290
|
+
}
|
|
1291
|
+
__name(extractAnswer, "extractAnswer");
|
|
1292
|
+
var InboundEffectsDispatcher = class {
|
|
1293
|
+
static {
|
|
1294
|
+
__name(this, "InboundEffectsDispatcher");
|
|
1295
|
+
}
|
|
1296
|
+
params;
|
|
1297
|
+
constructor(params) {
|
|
1298
|
+
this.params = params;
|
|
1299
|
+
}
|
|
1300
|
+
async run(job) {
|
|
1301
|
+
if (job.kind === "message") return this.runMessageEffects(job);
|
|
1302
|
+
return this.runStatusEffects(job);
|
|
1303
|
+
}
|
|
1304
|
+
async runMessageEffects(job) {
|
|
1305
|
+
const { companyId, message, savedMessageId, media } = job;
|
|
1306
|
+
if (media) {
|
|
1307
|
+
await this.params.hooks?.onMediaReceived?.({
|
|
1308
|
+
companyId,
|
|
1309
|
+
messageId: savedMessageId,
|
|
1310
|
+
whatsappNumber: message.from,
|
|
1311
|
+
sourceMediaId: media.sourceMediaId,
|
|
1312
|
+
mimeType: media.mimeType,
|
|
1313
|
+
...media.filename ? {
|
|
1314
|
+
filename: media.filename
|
|
1315
|
+
} : {}
|
|
1316
|
+
});
|
|
1317
|
+
}
|
|
1318
|
+
const sessionRow = await this.params.sessionRepository.getContext(companyId, message.from);
|
|
1319
|
+
if (!sessionRow) return;
|
|
1320
|
+
if (sessionRow.mode === "human") return;
|
|
1321
|
+
const outcome = await this.params.hooks?.onMessageReceived?.(message, toSessionContract(sessionRow), job.profileName === void 0 ? {} : {
|
|
1322
|
+
profileName: job.profileName
|
|
1323
|
+
});
|
|
1324
|
+
if (outcome?.outcome === "handled") return;
|
|
1325
|
+
void extractAnswer(message);
|
|
1326
|
+
}
|
|
1327
|
+
async runStatusEffects(job) {
|
|
1328
|
+
const sessionRow = await this.params.sessionRepository.getContext(job.companyId, job.whatsappNumber);
|
|
1329
|
+
await this.params.hooks?.onStatusUpdate?.(job.status, sessionRow ? toSessionContract(sessionRow) : null);
|
|
1330
|
+
}
|
|
1331
|
+
};
|
|
1332
|
+
var ProcessInboundDispatchUseCase = class {
|
|
1333
|
+
static {
|
|
1334
|
+
__name(this, "ProcessInboundDispatchUseCase");
|
|
1335
|
+
}
|
|
1336
|
+
dispatcher;
|
|
1337
|
+
hooks;
|
|
1338
|
+
constructor(dispatcher, hooks) {
|
|
1339
|
+
this.dispatcher = dispatcher;
|
|
1340
|
+
this.hooks = hooks;
|
|
1341
|
+
}
|
|
1342
|
+
async execute(job) {
|
|
1343
|
+
await runInboundEffects({
|
|
1344
|
+
dispatcher: this.dispatcher,
|
|
1345
|
+
job,
|
|
1346
|
+
...this.hooks ? {
|
|
1347
|
+
hooks: this.hooks
|
|
1348
|
+
} : {}
|
|
1349
|
+
});
|
|
1350
|
+
}
|
|
1351
|
+
};
|
|
1352
|
+
async function runInboundEffects(params) {
|
|
1353
|
+
const { job } = params;
|
|
1354
|
+
try {
|
|
1355
|
+
await params.dispatcher.run(job);
|
|
1356
|
+
} catch (error) {
|
|
1357
|
+
if (!isDeterministicMetaRejection(error)) throw error;
|
|
1358
|
+
await params.hooks?.onInboundEffectRejected?.({
|
|
1359
|
+
companyId: job.companyId,
|
|
1360
|
+
kind: job.kind,
|
|
1361
|
+
whatsappNumber: job.kind === "message" ? job.message.from : job.whatsappNumber,
|
|
1362
|
+
code: error.code,
|
|
1363
|
+
error
|
|
1364
|
+
});
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
__name(runInboundEffects, "runInboundEffects");
|
|
1368
|
+
|
|
1369
|
+
// src/use-cases/RequestHumanConversation.use-case.ts
|
|
1370
|
+
var RequestHumanConversationUseCase = class {
|
|
1371
|
+
static {
|
|
1372
|
+
__name(this, "RequestHumanConversationUseCase");
|
|
1373
|
+
}
|
|
1374
|
+
sessionRepository;
|
|
1375
|
+
realtime;
|
|
1376
|
+
hooks;
|
|
1377
|
+
constructor(sessionRepository, realtime, hooks) {
|
|
1378
|
+
this.sessionRepository = sessionRepository;
|
|
1379
|
+
this.realtime = realtime;
|
|
1380
|
+
this.hooks = hooks;
|
|
1381
|
+
}
|
|
1382
|
+
async execute(params) {
|
|
1383
|
+
await this.sessionRepository.requestHuman(params.companyId, params.whatsappNumber);
|
|
1384
|
+
const session = await this.sessionRepository.getContext(params.companyId, params.whatsappNumber);
|
|
1385
|
+
if (!session) throw new SessionNotFoundError(params.whatsappNumber);
|
|
1386
|
+
this.realtime?.emit(`conv:${params.whatsappNumber}`, "human-requested", {
|
|
1387
|
+
humanRequestedAt: session.humanRequestedAt?.toISOString() ?? null
|
|
1388
|
+
});
|
|
1389
|
+
this.realtime?.emit("global", "data-changed", {});
|
|
1390
|
+
const outcomes = await Promise.allSettled([
|
|
1391
|
+
(async () => this.hooks?.onHumanRequested?.(toSessionContract(session)))(),
|
|
1392
|
+
(async () => this.hooks?.onConversationModeChanged?.({
|
|
1393
|
+
companyId: params.companyId,
|
|
1394
|
+
whatsappNumber: params.whatsappNumber,
|
|
1395
|
+
kind: "human-requested",
|
|
1396
|
+
mode: session.mode,
|
|
1397
|
+
assignedUserId: session.assignedUserId,
|
|
1398
|
+
actorId: params.actorId ?? null,
|
|
1399
|
+
occurredAt: /* @__PURE__ */ new Date()
|
|
1400
|
+
}))()
|
|
1401
|
+
]);
|
|
1402
|
+
const failure = outcomes.find((outcome) => outcome.status === "rejected");
|
|
1403
|
+
if (failure) throw failure.reason;
|
|
1173
1404
|
}
|
|
1174
1405
|
};
|
|
1175
1406
|
|
|
@@ -1188,7 +1419,7 @@ var ListConversationsUseCase = class {
|
|
|
1188
1419
|
};
|
|
1189
1420
|
|
|
1190
1421
|
// src/use-cases/ListMessages.use-case.ts
|
|
1191
|
-
import { SessionNotFoundError } from "@adatechnology/meta-whatsapp-contracts";
|
|
1422
|
+
import { SessionNotFoundError as SessionNotFoundError2 } from "@adatechnology/meta-whatsapp-contracts";
|
|
1192
1423
|
var ListMessagesUseCase = class {
|
|
1193
1424
|
static {
|
|
1194
1425
|
__name(this, "ListMessagesUseCase");
|
|
@@ -1201,7 +1432,7 @@ var ListMessagesUseCase = class {
|
|
|
1201
1432
|
}
|
|
1202
1433
|
async execute(params) {
|
|
1203
1434
|
const session = await this.sessionRepository.getContext(params.companyId, params.whatsappNumber);
|
|
1204
|
-
if (!session) throw new
|
|
1435
|
+
if (!session) throw new SessionNotFoundError2(params.whatsappNumber);
|
|
1205
1436
|
const repoParams = {
|
|
1206
1437
|
companyId: params.companyId,
|
|
1207
1438
|
sessionId: session.id,
|
|
@@ -1213,7 +1444,7 @@ var ListMessagesUseCase = class {
|
|
|
1213
1444
|
};
|
|
1214
1445
|
|
|
1215
1446
|
// src/use-cases/ListConversationDocuments.use-case.ts
|
|
1216
|
-
import { SessionNotFoundError as
|
|
1447
|
+
import { SessionNotFoundError as SessionNotFoundError3 } from "@adatechnology/meta-whatsapp-contracts";
|
|
1217
1448
|
var ListConversationDocumentsUseCase = class {
|
|
1218
1449
|
static {
|
|
1219
1450
|
__name(this, "ListConversationDocumentsUseCase");
|
|
@@ -1226,7 +1457,7 @@ var ListConversationDocumentsUseCase = class {
|
|
|
1226
1457
|
}
|
|
1227
1458
|
async execute(params) {
|
|
1228
1459
|
const session = await this.sessionRepository.getContext(params.companyId, params.whatsappNumber);
|
|
1229
|
-
if (!session) throw new
|
|
1460
|
+
if (!session) throw new SessionNotFoundError3(params.whatsappNumber);
|
|
1230
1461
|
const { rows, total } = await this.documentRepository.listByConversation({
|
|
1231
1462
|
companyId: params.companyId,
|
|
1232
1463
|
sessionId: session.id,
|
|
@@ -1304,7 +1535,7 @@ var ListCompanyDocumentsUseCase = class {
|
|
|
1304
1535
|
};
|
|
1305
1536
|
|
|
1306
1537
|
// src/use-cases/DeleteConversation.use-case.ts
|
|
1307
|
-
import { SessionNotFoundError as
|
|
1538
|
+
import { SessionNotFoundError as SessionNotFoundError4 } from "@adatechnology/meta-whatsapp-contracts";
|
|
1308
1539
|
var DeleteConversationUseCase = class {
|
|
1309
1540
|
static {
|
|
1310
1541
|
__name(this, "DeleteConversationUseCase");
|
|
@@ -1319,7 +1550,7 @@ var DeleteConversationUseCase = class {
|
|
|
1319
1550
|
}
|
|
1320
1551
|
async execute(params) {
|
|
1321
1552
|
const session = await this.sessionRepository.getContext(params.companyId, params.whatsappNumber);
|
|
1322
|
-
if (!session) throw new
|
|
1553
|
+
if (!session) throw new SessionNotFoundError4(params.whatsappNumber);
|
|
1323
1554
|
const uploadIds = await this.documentRepository.listUploadIdsBySession(params.companyId, session.id);
|
|
1324
1555
|
if (uploadIds.length > 0 && !this.objectStorage?.delete) {
|
|
1325
1556
|
throw new Error("storage_delete_unsupported: a conversa tem arquivos e o storage injetado n\xE3o implementa delete");
|
|
@@ -1565,7 +1796,7 @@ var DocumentRepository = class {
|
|
|
1565
1796
|
};
|
|
1566
1797
|
|
|
1567
1798
|
// src/use-cases/ExportConversation.use-case.ts
|
|
1568
|
-
import { SessionNotFoundError as
|
|
1799
|
+
import { SessionNotFoundError as SessionNotFoundError5 } from "@adatechnology/meta-whatsapp-contracts";
|
|
1569
1800
|
var ExportConversationUseCase = class {
|
|
1570
1801
|
static {
|
|
1571
1802
|
__name(this, "ExportConversationUseCase");
|
|
@@ -1576,7 +1807,7 @@ var ExportConversationUseCase = class {
|
|
|
1576
1807
|
}
|
|
1577
1808
|
async execute(params) {
|
|
1578
1809
|
const result = await this.sessionRepository.exportConversation(params.companyId, params.whatsappNumber);
|
|
1579
|
-
if (!result) throw new
|
|
1810
|
+
if (!result) throw new SessionNotFoundError5(params.whatsappNumber);
|
|
1580
1811
|
return result;
|
|
1581
1812
|
}
|
|
1582
1813
|
};
|
|
@@ -2147,16 +2378,25 @@ var WhatsAppChannelAdapter = class {
|
|
|
2147
2378
|
};
|
|
2148
2379
|
}
|
|
2149
2380
|
async sendInteractiveList(params) {
|
|
2381
|
+
const sections = (params.sections ?? [
|
|
2382
|
+
{
|
|
2383
|
+
rows: params.rows ?? []
|
|
2384
|
+
}
|
|
2385
|
+
]).map((section) => ({
|
|
2386
|
+
title: section.title ?? params.buttonLabel,
|
|
2387
|
+
rows: section.rows.map((row) => ({
|
|
2388
|
+
id: row.id,
|
|
2389
|
+
title: row.title,
|
|
2390
|
+
...row.description ? {
|
|
2391
|
+
description: row.description
|
|
2392
|
+
} : {}
|
|
2393
|
+
}))
|
|
2394
|
+
}));
|
|
2150
2395
|
const result = await this.translateErrors(() => this.messages.sendInteractiveList({
|
|
2151
2396
|
to: params.to,
|
|
2152
2397
|
bodyText: params.body,
|
|
2153
2398
|
buttonText: params.buttonLabel,
|
|
2154
|
-
sections
|
|
2155
|
-
{
|
|
2156
|
-
title: params.buttonLabel,
|
|
2157
|
-
rows: params.rows
|
|
2158
|
-
}
|
|
2159
|
-
]
|
|
2399
|
+
sections
|
|
2160
2400
|
}));
|
|
2161
2401
|
return {
|
|
2162
2402
|
externalMessageId: result.waMessageId
|
|
@@ -2207,7 +2447,6 @@ var WhatsAppChannelAdapter = class {
|
|
|
2207
2447
|
|
|
2208
2448
|
// src/channel/ReceiveWebhook.use-case.ts
|
|
2209
2449
|
import { whatsAppWebhookPayloadSchema, resolveContactProfileName, whatsAppTemplateStatusUpdateSchema, whatsAppPhoneNumberQualityUpdateSchema, WHATSAPP_WEBHOOK_FIELDS } from "@adatechnology/meta-whatsapp-contracts";
|
|
2210
|
-
import { isDeterministicMetaRejection } from "@adatechnology/meta-graph-core";
|
|
2211
2450
|
|
|
2212
2451
|
// src/channel/webhookSecurity.ts
|
|
2213
2452
|
import { WEBHOOK_CLAIM_TTL_SECONDS, WEBHOOK_NONCE_TTL_SECONDS, buildWebhookDeliveryKey, isValidWebhookChallenge, isValidWebhookSignature } from "@adatechnology/meta-graph-core";
|
|
@@ -2557,91 +2796,6 @@ function extractMediaDescriptor(message) {
|
|
|
2557
2796
|
}
|
|
2558
2797
|
__name(extractMediaDescriptor, "extractMediaDescriptor");
|
|
2559
2798
|
|
|
2560
|
-
// src/channel/inboundDispatch.ts
|
|
2561
|
-
function buildInboundJobId(job) {
|
|
2562
|
-
return job.kind === "message" ? `wa-inbound-message:${job.message.id}` : `wa-inbound-status:${job.status.id}:${job.status.status}`;
|
|
2563
|
-
}
|
|
2564
|
-
__name(buildInboundJobId, "buildInboundJobId");
|
|
2565
|
-
function toSessionContract(row) {
|
|
2566
|
-
return {
|
|
2567
|
-
id: row.id,
|
|
2568
|
-
companyId: row.companyId,
|
|
2569
|
-
whatsappNumber: row.whatsappNumber,
|
|
2570
|
-
currentState: row.currentState,
|
|
2571
|
-
flowKey: row.flowKey,
|
|
2572
|
-
currentNodeId: row.currentNodeId,
|
|
2573
|
-
context: row.context,
|
|
2574
|
-
mode: row.mode,
|
|
2575
|
-
assignedUserId: row.assignedUserId,
|
|
2576
|
-
humanRequestedAt: row.humanRequestedAt?.toISOString() ?? null,
|
|
2577
|
-
lastInboundAt: row.lastInboundAt?.toISOString() ?? null,
|
|
2578
|
-
lastAgentReadAt: row.lastAgentReadAt?.toISOString() ?? null,
|
|
2579
|
-
lastActivity: row.lastActivity.toISOString(),
|
|
2580
|
-
createdAt: row.createdAt.toISOString(),
|
|
2581
|
-
updatedAt: row.updatedAt.toISOString()
|
|
2582
|
-
};
|
|
2583
|
-
}
|
|
2584
|
-
__name(toSessionContract, "toSessionContract");
|
|
2585
|
-
function extractAnswer(message) {
|
|
2586
|
-
const interactive = message.interactive;
|
|
2587
|
-
if (interactive?.button_reply) return interactive.button_reply.id;
|
|
2588
|
-
if (interactive?.list_reply) return interactive.list_reply.id;
|
|
2589
|
-
return message.text?.body;
|
|
2590
|
-
}
|
|
2591
|
-
__name(extractAnswer, "extractAnswer");
|
|
2592
|
-
var InboundEffectsDispatcher = class {
|
|
2593
|
-
static {
|
|
2594
|
-
__name(this, "InboundEffectsDispatcher");
|
|
2595
|
-
}
|
|
2596
|
-
params;
|
|
2597
|
-
constructor(params) {
|
|
2598
|
-
this.params = params;
|
|
2599
|
-
}
|
|
2600
|
-
async run(job) {
|
|
2601
|
-
if (job.kind === "message") return this.runMessageEffects(job);
|
|
2602
|
-
return this.runStatusEffects(job);
|
|
2603
|
-
}
|
|
2604
|
-
async runMessageEffects(job) {
|
|
2605
|
-
const { companyId, message, savedMessageId, media } = job;
|
|
2606
|
-
if (media) {
|
|
2607
|
-
await this.params.hooks?.onMediaReceived?.({
|
|
2608
|
-
companyId,
|
|
2609
|
-
messageId: savedMessageId,
|
|
2610
|
-
whatsappNumber: message.from,
|
|
2611
|
-
sourceMediaId: media.sourceMediaId,
|
|
2612
|
-
mimeType: media.mimeType,
|
|
2613
|
-
...media.filename ? {
|
|
2614
|
-
filename: media.filename
|
|
2615
|
-
} : {}
|
|
2616
|
-
});
|
|
2617
|
-
}
|
|
2618
|
-
const sessionRow = await this.params.sessionRepository.getContext(companyId, message.from);
|
|
2619
|
-
if (!sessionRow) return;
|
|
2620
|
-
if (sessionRow.mode === "human") return;
|
|
2621
|
-
const outcome = await this.params.hooks?.onMessageReceived?.(message, toSessionContract(sessionRow), job.profileName === void 0 ? {} : {
|
|
2622
|
-
profileName: job.profileName
|
|
2623
|
-
});
|
|
2624
|
-
if (outcome?.outcome === "handled") return;
|
|
2625
|
-
void extractAnswer(message);
|
|
2626
|
-
}
|
|
2627
|
-
async runStatusEffects(job) {
|
|
2628
|
-
const sessionRow = await this.params.sessionRepository.getContext(job.companyId, job.whatsappNumber);
|
|
2629
|
-
await this.params.hooks?.onStatusUpdate?.(job.status, sessionRow ? toSessionContract(sessionRow) : null);
|
|
2630
|
-
}
|
|
2631
|
-
};
|
|
2632
|
-
var ProcessInboundDispatchUseCase = class {
|
|
2633
|
-
static {
|
|
2634
|
-
__name(this, "ProcessInboundDispatchUseCase");
|
|
2635
|
-
}
|
|
2636
|
-
dispatcher;
|
|
2637
|
-
constructor(dispatcher) {
|
|
2638
|
-
this.dispatcher = dispatcher;
|
|
2639
|
-
}
|
|
2640
|
-
async execute(job) {
|
|
2641
|
-
await this.dispatcher.run(job);
|
|
2642
|
-
}
|
|
2643
|
-
};
|
|
2644
|
-
|
|
2645
2799
|
// src/channel/ReceiveWebhook.use-case.ts
|
|
2646
2800
|
var EMPTY_RESULT = {
|
|
2647
2801
|
messagesProcessed: 0,
|
|
@@ -2891,24 +3045,15 @@ var ReceiveWebhookUseCase = class {
|
|
|
2891
3045
|
});
|
|
2892
3046
|
return;
|
|
2893
3047
|
}
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
whatsappNumber: resolveJobNumber(job),
|
|
2902
|
-
code: error.code,
|
|
2903
|
-
error
|
|
2904
|
-
});
|
|
2905
|
-
}
|
|
3048
|
+
await runInboundEffects({
|
|
3049
|
+
dispatcher: this.dispatcher,
|
|
3050
|
+
job,
|
|
3051
|
+
...this.params.hooks ? {
|
|
3052
|
+
hooks: this.params.hooks
|
|
3053
|
+
} : {}
|
|
3054
|
+
});
|
|
2906
3055
|
}
|
|
2907
3056
|
};
|
|
2908
|
-
function resolveJobNumber(job) {
|
|
2909
|
-
return job.kind === "message" ? job.message.from : job.whatsappNumber;
|
|
2910
|
-
}
|
|
2911
|
-
__name(resolveJobNumber, "resolveJobNumber");
|
|
2912
3057
|
|
|
2913
3058
|
// src/use-cases/resolveTranscriptionPolicy.ts
|
|
2914
3059
|
function createTranscriptionPolicyResolver(dependencies) {
|
|
@@ -2992,6 +3137,9 @@ function createMetaWhatsAppModule(params) {
|
|
|
2992
3137
|
startState,
|
|
2993
3138
|
hooks,
|
|
2994
3139
|
realtime: providers.realtime,
|
|
3140
|
+
...providers.inboundQueue ? {
|
|
3141
|
+
inboundQueue: providers.inboundQueue
|
|
3142
|
+
} : {},
|
|
2995
3143
|
redactInboundLocation: params.features?.redactInboundLocation ?? false
|
|
2996
3144
|
});
|
|
2997
3145
|
const flowInterpreter = flowEngineEnabled ? new FlowInterpreter() : void 0;
|
|
@@ -3062,8 +3210,10 @@ function createMetaWhatsAppModule(params) {
|
|
|
3062
3210
|
conversations: {
|
|
3063
3211
|
log: logMessage,
|
|
3064
3212
|
send: sendMessage,
|
|
3065
|
-
takeover: new TakeoverConversationUseCase(sessionRepository, providers.realtime),
|
|
3066
|
-
release: new ReleaseConversationUseCase(sessionRepository, providers.realtime),
|
|
3213
|
+
takeover: new TakeoverConversationUseCase(sessionRepository, providers.realtime, hooks),
|
|
3214
|
+
release: new ReleaseConversationUseCase(sessionRepository, providers.realtime, hooks),
|
|
3215
|
+
// Marca o pedido de atendimento (fila "aguardando atendimento") e dispara `onHumanRequested`.
|
|
3216
|
+
requestHuman: new RequestHumanConversationUseCase(sessionRepository, providers.realtime, hooks),
|
|
3067
3217
|
list: new ListConversationsUseCase(sessionRepository),
|
|
3068
3218
|
listMessages: new ListMessagesUseCase(sessionRepository, messageRepository),
|
|
3069
3219
|
listDocuments,
|
|
@@ -3105,7 +3255,18 @@ function createMetaWhatsAppModule(params) {
|
|
|
3105
3255
|
verifyChallenge: /* @__PURE__ */ __name((query) => verifyWebhookChallenge({
|
|
3106
3256
|
...query,
|
|
3107
3257
|
expectedToken: config.webhookVerifyToken
|
|
3108
|
-
}), "verifyChallenge")
|
|
3258
|
+
}), "verifyChallenge"),
|
|
3259
|
+
// Lado do worker de `providers.inboundQueue`: roda os efeitos de um job já enfileirado. Existe
|
|
3260
|
+
// mesmo sem fila configurada — um worker de outra instância pode consumir a fila desta.
|
|
3261
|
+
processInboundDispatch: new ProcessInboundDispatchUseCase(new InboundEffectsDispatcher({
|
|
3262
|
+
sessionRepository,
|
|
3263
|
+
...hooks ? {
|
|
3264
|
+
hooks
|
|
3265
|
+
} : {},
|
|
3266
|
+
...providers.realtime ? {
|
|
3267
|
+
realtime: providers.realtime
|
|
3268
|
+
} : {}
|
|
3269
|
+
}), hooks)
|
|
3109
3270
|
},
|
|
3110
3271
|
// undefined quando features.flowEngine === false.
|
|
3111
3272
|
flows: flowInterpreter ? {
|
|
@@ -3131,15 +3292,29 @@ __name(createMetaWhatsAppModule, "createMetaWhatsAppModule");
|
|
|
3131
3292
|
// src/runMigrations.ts
|
|
3132
3293
|
import { join } from "path";
|
|
3133
3294
|
var META_WHATSAPP_MIGRATIONS_TABLE = "meta_whatsapp_migrations";
|
|
3134
|
-
|
|
3135
|
-
|
|
3295
|
+
var LAYOUT_DIRECTORY = {
|
|
3296
|
+
v1: "migrations",
|
|
3297
|
+
journal: "migrations-journal"
|
|
3298
|
+
};
|
|
3299
|
+
function metaWhatsAppMigrationsFolder(params = {}) {
|
|
3300
|
+
return join(__dirname, LAYOUT_DIRECTORY[params.layout ?? "v1"]);
|
|
3136
3301
|
}
|
|
3137
3302
|
__name(metaWhatsAppMigrationsFolder, "metaWhatsAppMigrationsFolder");
|
|
3303
|
+
var MISSING_JOURNAL_REFUSAL = "meta/_journal.json";
|
|
3138
3304
|
async function runMetaWhatsAppMigrations(params) {
|
|
3139
|
-
|
|
3140
|
-
migrationsFolder: metaWhatsAppMigrationsFolder(
|
|
3305
|
+
const run = /* @__PURE__ */ __name((layout) => params.migrate(params.db, {
|
|
3306
|
+
migrationsFolder: metaWhatsAppMigrationsFolder({
|
|
3307
|
+
layout
|
|
3308
|
+
}),
|
|
3141
3309
|
migrationsTable: META_WHATSAPP_MIGRATIONS_TABLE
|
|
3142
|
-
});
|
|
3310
|
+
}), "run");
|
|
3311
|
+
if (params.layout) return run(params.layout);
|
|
3312
|
+
try {
|
|
3313
|
+
await run("v1");
|
|
3314
|
+
} catch (error) {
|
|
3315
|
+
if (!(error instanceof Error) || !error.message.includes(MISSING_JOURNAL_REFUSAL)) throw error;
|
|
3316
|
+
await run("journal");
|
|
3317
|
+
}
|
|
3143
3318
|
}
|
|
3144
3319
|
__name(runMetaWhatsAppMigrations, "runMetaWhatsAppMigrations");
|
|
3145
3320
|
|
|
@@ -3221,6 +3396,7 @@ __name(redeemSseTicket, "redeemSseTicket");
|
|
|
3221
3396
|
export {
|
|
3222
3397
|
CountInboundLocationsUseCase,
|
|
3223
3398
|
CreateFlowGraphUseCase,
|
|
3399
|
+
DEFAULT_FALLBACK_LIST_LABEL,
|
|
3224
3400
|
DEFAULT_FLOW_GRAPH_CACHE_TTL_SECONDS,
|
|
3225
3401
|
DeleteConversationUseCase,
|
|
3226
3402
|
DeleteFlowGraphUseCase,
|
|
@@ -3253,6 +3429,7 @@ export {
|
|
|
3253
3429
|
ReceiveWebhookUseCase,
|
|
3254
3430
|
RedactInboundLocationsUseCase,
|
|
3255
3431
|
ReleaseConversationUseCase,
|
|
3432
|
+
RequestHumanConversationUseCase,
|
|
3256
3433
|
SaveFlowGraphUseCase,
|
|
3257
3434
|
SendMessageUseCase,
|
|
3258
3435
|
SessionRepository,
|