@theokit/gateway-whatsapp 0.7.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/CHANGELOG.md +12 -0
- package/README.md +47 -0
- package/dist/index.cjs +246 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +82 -1
- package/dist/index.d.ts +82 -1
- package/dist/index.js +247 -5
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.d.cts
CHANGED
|
@@ -1289,6 +1289,87 @@ declare class LineBuffer {
|
|
|
1289
1289
|
get pending(): string;
|
|
1290
1290
|
}
|
|
1291
1291
|
|
|
1292
|
+
/**
|
|
1293
|
+
* The Cloud half of the WhatsApp descriptor's webhook: Meta's GET subscription check and the signed
|
|
1294
|
+
* POST, answered through `ChannelWebhook.handle` so a host mounts it with `createWebhookHandler`.
|
|
1295
|
+
*
|
|
1296
|
+
* The credentials the route needs (`appSecret`, `verifyToken`) are not adapter options, so the
|
|
1297
|
+
* descriptor's `create` records them here, keyed by the adapter it built, never on a public field.
|
|
1298
|
+
* An adapter not built by the descriptor has none and every request to it is refused.
|
|
1299
|
+
*
|
|
1300
|
+
* No secret, signature or body text is logged or answered.
|
|
1301
|
+
*/
|
|
1302
|
+
|
|
1303
|
+
/** The request shape `handle` receives (structural; the core type is not imported). */
|
|
1304
|
+
interface WebhookRequestShape {
|
|
1305
|
+
readonly rawBody: string;
|
|
1306
|
+
readonly headers: Readonly<Record<string, string | undefined>>;
|
|
1307
|
+
readonly method?: string;
|
|
1308
|
+
readonly url?: string;
|
|
1309
|
+
}
|
|
1310
|
+
/** What `handle` answers. */
|
|
1311
|
+
interface WebhookAnswerShape {
|
|
1312
|
+
readonly status: number;
|
|
1313
|
+
readonly body?: string;
|
|
1314
|
+
readonly contentType?: string;
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
type Config = Readonly<Record<string, unknown>>;
|
|
1318
|
+
/** One configuration field, shaped like the core's `ChannelConfigField` (typed locally). */
|
|
1319
|
+
interface ConfigField {
|
|
1320
|
+
readonly key: string;
|
|
1321
|
+
readonly required: boolean;
|
|
1322
|
+
readonly secret: boolean;
|
|
1323
|
+
readonly description: string;
|
|
1324
|
+
readonly env?: string;
|
|
1325
|
+
}
|
|
1326
|
+
declare function createCloud(config: Config): WhatsAppAdapter;
|
|
1327
|
+
declare function createBaileys(config: Config): WhatsAppAdapter;
|
|
1328
|
+
/**
|
|
1329
|
+
* The WhatsApp channel. The descriptor itself is the `cloud` shape; `variants.cloud` and
|
|
1330
|
+
* `variants.baileys` are the two backends, chosen with `selectVariant`.
|
|
1331
|
+
*
|
|
1332
|
+
* `create` refuses a value of the wrong type with this package's `ConfigurationError`
|
|
1333
|
+
* (`invalid_config`) and performs no network call.
|
|
1334
|
+
*
|
|
1335
|
+
* @public
|
|
1336
|
+
*/
|
|
1337
|
+
declare const whatsappChannel: {
|
|
1338
|
+
variants: {
|
|
1339
|
+
cloud: {
|
|
1340
|
+
platform: "whatsapp";
|
|
1341
|
+
transport: "webhook";
|
|
1342
|
+
webhook: {
|
|
1343
|
+
methods: readonly ["GET", "POST"];
|
|
1344
|
+
verify(adapter: WhatsAppAdapter, request: WebhookRequestShape): boolean;
|
|
1345
|
+
ingest(adapter: WhatsAppAdapter, payload: unknown): Promise<"delivered" | "malformed" | "no_handler">;
|
|
1346
|
+
handle(adapter: WhatsAppAdapter, request: WebhookRequestShape): Promise<WebhookAnswerShape>;
|
|
1347
|
+
};
|
|
1348
|
+
senderIdentity: "authenticated";
|
|
1349
|
+
config: readonly ConfigField[];
|
|
1350
|
+
create: typeof createCloud;
|
|
1351
|
+
};
|
|
1352
|
+
baileys: {
|
|
1353
|
+
platform: "whatsapp";
|
|
1354
|
+
transport: "socket";
|
|
1355
|
+
senderIdentity: "authenticated";
|
|
1356
|
+
config: readonly ConfigField[];
|
|
1357
|
+
create: typeof createBaileys;
|
|
1358
|
+
};
|
|
1359
|
+
};
|
|
1360
|
+
platform: "whatsapp";
|
|
1361
|
+
transport: "webhook";
|
|
1362
|
+
webhook: {
|
|
1363
|
+
methods: readonly ["GET", "POST"];
|
|
1364
|
+
verify(adapter: WhatsAppAdapter, request: WebhookRequestShape): boolean;
|
|
1365
|
+
ingest(adapter: WhatsAppAdapter, payload: unknown): Promise<"delivered" | "malformed" | "no_handler">;
|
|
1366
|
+
handle(adapter: WhatsAppAdapter, request: WebhookRequestShape): Promise<WebhookAnswerShape>;
|
|
1367
|
+
};
|
|
1368
|
+
senderIdentity: "authenticated";
|
|
1369
|
+
config: readonly ConfigField[];
|
|
1370
|
+
create: typeof createCloud;
|
|
1371
|
+
};
|
|
1372
|
+
|
|
1292
1373
|
/**
|
|
1293
1374
|
* Per-backend HTTP/IPC error → canonical `WhatsAppSendResult.error` shape.
|
|
1294
1375
|
*
|
|
@@ -1365,4 +1446,4 @@ declare function mapWhatsAppWebError(ipcError: string | undefined): ErrorPayload
|
|
|
1365
1446
|
/** Split `text` into chunks of at most `WHATSAPP_MAX_TEXT` characters. */
|
|
1366
1447
|
declare function splitForWhatsApp(text: string): string[];
|
|
1367
1448
|
|
|
1368
|
-
export { type BaileysSocketFactory, type BaileysSocketLike, type BaileysSocketOptions, ConfigurationError, type ConfigurationErrorOptions, type IpcCommand, type IpcEvent, LineBuffer, type MetaTemplateComponent, type MetaWebhookEnvelope, WhatsAppAdapter, type WhatsAppAdapterCommonOptions, type WhatsAppAdapterOptions, type WhatsAppBackend, WhatsAppBaileysBackend, type WhatsAppBaileysBackendOptions, type WhatsAppBaileysConfig, WhatsAppBridgeError, WhatsAppCloudBackend, type WhatsAppCloudBackendOptions, type WhatsAppCloudConfig, WhatsAppConnectTimeoutError, type WhatsAppInboundEvent, type WhatsAppOutboundMessage, type WhatsAppPairingState, type WhatsAppSendResult, type WhatsAppStatusReceipt, WhatsAppWebBackend, type WhatsAppWebBackendOptions, type WhatsAppWebConfig, createBaileysSocket, defaultBridgeScriptPath, digitsOnly, formatCommand, mapWhatsAppCloudError, mapWhatsAppWebError, normalizeInboundMessages, normalizeStatusReceipts, parseEvent, parseWebhookPayload, splitForWhatsApp, verifyWebhookSignature, verifyWebhookSubscription };
|
|
1449
|
+
export { type BaileysSocketFactory, type BaileysSocketLike, type BaileysSocketOptions, ConfigurationError, type ConfigurationErrorOptions, type IpcCommand, type IpcEvent, LineBuffer, type MetaTemplateComponent, type MetaWebhookEnvelope, WhatsAppAdapter, type WhatsAppAdapterCommonOptions, type WhatsAppAdapterOptions, type WhatsAppBackend, WhatsAppBaileysBackend, type WhatsAppBaileysBackendOptions, type WhatsAppBaileysConfig, WhatsAppBridgeError, WhatsAppCloudBackend, type WhatsAppCloudBackendOptions, type WhatsAppCloudConfig, WhatsAppConnectTimeoutError, type WhatsAppInboundEvent, type WhatsAppOutboundMessage, type WhatsAppPairingState, type WhatsAppSendResult, type WhatsAppStatusReceipt, WhatsAppWebBackend, type WhatsAppWebBackendOptions, type WhatsAppWebConfig, createBaileysSocket, defaultBridgeScriptPath, digitsOnly, formatCommand, mapWhatsAppCloudError, mapWhatsAppWebError, normalizeInboundMessages, normalizeStatusReceipts, parseEvent, parseWebhookPayload, splitForWhatsApp, verifyWebhookSignature, verifyWebhookSubscription, whatsappChannel };
|
package/dist/index.d.ts
CHANGED
|
@@ -1289,6 +1289,87 @@ declare class LineBuffer {
|
|
|
1289
1289
|
get pending(): string;
|
|
1290
1290
|
}
|
|
1291
1291
|
|
|
1292
|
+
/**
|
|
1293
|
+
* The Cloud half of the WhatsApp descriptor's webhook: Meta's GET subscription check and the signed
|
|
1294
|
+
* POST, answered through `ChannelWebhook.handle` so a host mounts it with `createWebhookHandler`.
|
|
1295
|
+
*
|
|
1296
|
+
* The credentials the route needs (`appSecret`, `verifyToken`) are not adapter options, so the
|
|
1297
|
+
* descriptor's `create` records them here, keyed by the adapter it built, never on a public field.
|
|
1298
|
+
* An adapter not built by the descriptor has none and every request to it is refused.
|
|
1299
|
+
*
|
|
1300
|
+
* No secret, signature or body text is logged or answered.
|
|
1301
|
+
*/
|
|
1302
|
+
|
|
1303
|
+
/** The request shape `handle` receives (structural; the core type is not imported). */
|
|
1304
|
+
interface WebhookRequestShape {
|
|
1305
|
+
readonly rawBody: string;
|
|
1306
|
+
readonly headers: Readonly<Record<string, string | undefined>>;
|
|
1307
|
+
readonly method?: string;
|
|
1308
|
+
readonly url?: string;
|
|
1309
|
+
}
|
|
1310
|
+
/** What `handle` answers. */
|
|
1311
|
+
interface WebhookAnswerShape {
|
|
1312
|
+
readonly status: number;
|
|
1313
|
+
readonly body?: string;
|
|
1314
|
+
readonly contentType?: string;
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
type Config = Readonly<Record<string, unknown>>;
|
|
1318
|
+
/** One configuration field, shaped like the core's `ChannelConfigField` (typed locally). */
|
|
1319
|
+
interface ConfigField {
|
|
1320
|
+
readonly key: string;
|
|
1321
|
+
readonly required: boolean;
|
|
1322
|
+
readonly secret: boolean;
|
|
1323
|
+
readonly description: string;
|
|
1324
|
+
readonly env?: string;
|
|
1325
|
+
}
|
|
1326
|
+
declare function createCloud(config: Config): WhatsAppAdapter;
|
|
1327
|
+
declare function createBaileys(config: Config): WhatsAppAdapter;
|
|
1328
|
+
/**
|
|
1329
|
+
* The WhatsApp channel. The descriptor itself is the `cloud` shape; `variants.cloud` and
|
|
1330
|
+
* `variants.baileys` are the two backends, chosen with `selectVariant`.
|
|
1331
|
+
*
|
|
1332
|
+
* `create` refuses a value of the wrong type with this package's `ConfigurationError`
|
|
1333
|
+
* (`invalid_config`) and performs no network call.
|
|
1334
|
+
*
|
|
1335
|
+
* @public
|
|
1336
|
+
*/
|
|
1337
|
+
declare const whatsappChannel: {
|
|
1338
|
+
variants: {
|
|
1339
|
+
cloud: {
|
|
1340
|
+
platform: "whatsapp";
|
|
1341
|
+
transport: "webhook";
|
|
1342
|
+
webhook: {
|
|
1343
|
+
methods: readonly ["GET", "POST"];
|
|
1344
|
+
verify(adapter: WhatsAppAdapter, request: WebhookRequestShape): boolean;
|
|
1345
|
+
ingest(adapter: WhatsAppAdapter, payload: unknown): Promise<"delivered" | "malformed" | "no_handler">;
|
|
1346
|
+
handle(adapter: WhatsAppAdapter, request: WebhookRequestShape): Promise<WebhookAnswerShape>;
|
|
1347
|
+
};
|
|
1348
|
+
senderIdentity: "authenticated";
|
|
1349
|
+
config: readonly ConfigField[];
|
|
1350
|
+
create: typeof createCloud;
|
|
1351
|
+
};
|
|
1352
|
+
baileys: {
|
|
1353
|
+
platform: "whatsapp";
|
|
1354
|
+
transport: "socket";
|
|
1355
|
+
senderIdentity: "authenticated";
|
|
1356
|
+
config: readonly ConfigField[];
|
|
1357
|
+
create: typeof createBaileys;
|
|
1358
|
+
};
|
|
1359
|
+
};
|
|
1360
|
+
platform: "whatsapp";
|
|
1361
|
+
transport: "webhook";
|
|
1362
|
+
webhook: {
|
|
1363
|
+
methods: readonly ["GET", "POST"];
|
|
1364
|
+
verify(adapter: WhatsAppAdapter, request: WebhookRequestShape): boolean;
|
|
1365
|
+
ingest(adapter: WhatsAppAdapter, payload: unknown): Promise<"delivered" | "malformed" | "no_handler">;
|
|
1366
|
+
handle(adapter: WhatsAppAdapter, request: WebhookRequestShape): Promise<WebhookAnswerShape>;
|
|
1367
|
+
};
|
|
1368
|
+
senderIdentity: "authenticated";
|
|
1369
|
+
config: readonly ConfigField[];
|
|
1370
|
+
create: typeof createCloud;
|
|
1371
|
+
};
|
|
1372
|
+
|
|
1292
1373
|
/**
|
|
1293
1374
|
* Per-backend HTTP/IPC error → canonical `WhatsAppSendResult.error` shape.
|
|
1294
1375
|
*
|
|
@@ -1365,4 +1446,4 @@ declare function mapWhatsAppWebError(ipcError: string | undefined): ErrorPayload
|
|
|
1365
1446
|
/** Split `text` into chunks of at most `WHATSAPP_MAX_TEXT` characters. */
|
|
1366
1447
|
declare function splitForWhatsApp(text: string): string[];
|
|
1367
1448
|
|
|
1368
|
-
export { type BaileysSocketFactory, type BaileysSocketLike, type BaileysSocketOptions, ConfigurationError, type ConfigurationErrorOptions, type IpcCommand, type IpcEvent, LineBuffer, type MetaTemplateComponent, type MetaWebhookEnvelope, WhatsAppAdapter, type WhatsAppAdapterCommonOptions, type WhatsAppAdapterOptions, type WhatsAppBackend, WhatsAppBaileysBackend, type WhatsAppBaileysBackendOptions, type WhatsAppBaileysConfig, WhatsAppBridgeError, WhatsAppCloudBackend, type WhatsAppCloudBackendOptions, type WhatsAppCloudConfig, WhatsAppConnectTimeoutError, type WhatsAppInboundEvent, type WhatsAppOutboundMessage, type WhatsAppPairingState, type WhatsAppSendResult, type WhatsAppStatusReceipt, WhatsAppWebBackend, type WhatsAppWebBackendOptions, type WhatsAppWebConfig, createBaileysSocket, defaultBridgeScriptPath, digitsOnly, formatCommand, mapWhatsAppCloudError, mapWhatsAppWebError, normalizeInboundMessages, normalizeStatusReceipts, parseEvent, parseWebhookPayload, splitForWhatsApp, verifyWebhookSignature, verifyWebhookSubscription };
|
|
1449
|
+
export { type BaileysSocketFactory, type BaileysSocketLike, type BaileysSocketOptions, ConfigurationError, type ConfigurationErrorOptions, type IpcCommand, type IpcEvent, LineBuffer, type MetaTemplateComponent, type MetaWebhookEnvelope, WhatsAppAdapter, type WhatsAppAdapterCommonOptions, type WhatsAppAdapterOptions, type WhatsAppBackend, WhatsAppBaileysBackend, type WhatsAppBaileysBackendOptions, type WhatsAppBaileysConfig, WhatsAppBridgeError, WhatsAppCloudBackend, type WhatsAppCloudBackendOptions, type WhatsAppCloudConfig, WhatsAppConnectTimeoutError, type WhatsAppInboundEvent, type WhatsAppOutboundMessage, type WhatsAppPairingState, type WhatsAppSendResult, type WhatsAppStatusReceipt, WhatsAppWebBackend, type WhatsAppWebBackendOptions, type WhatsAppWebConfig, createBaileysSocket, defaultBridgeScriptPath, digitsOnly, formatCommand, mapWhatsAppCloudError, mapWhatsAppWebError, normalizeInboundMessages, normalizeStatusReceipts, parseEvent, parseWebhookPayload, splitForWhatsApp, verifyWebhookSignature, verifyWebhookSubscription, whatsappChannel };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { GatewayConfigurationError, chunkText, BasePlatformAdapter } from '@theokit/gateway';
|
|
2
2
|
import * as crypto from 'crypto';
|
|
3
|
+
import { createHash, timingSafeEqual } from 'crypto';
|
|
3
4
|
import * as fs from 'fs';
|
|
4
5
|
import { existsSync } from 'fs';
|
|
5
6
|
import * as path2 from 'path';
|
|
@@ -1915,14 +1916,14 @@ var WhatsAppAdapter = class _WhatsAppAdapter extends BasePlatformAdapter {
|
|
|
1915
1916
|
* @throws {ConfigurationError} when a required option is missing or empty.
|
|
1916
1917
|
* @public
|
|
1917
1918
|
*/
|
|
1918
|
-
static from(options,
|
|
1919
|
+
static from(options, common2 = {}) {
|
|
1919
1920
|
switch (options.backend) {
|
|
1920
1921
|
case "cloud":
|
|
1921
|
-
return _WhatsAppAdapter.fromCloud(options.cloud,
|
|
1922
|
+
return _WhatsAppAdapter.fromCloud(options.cloud, common2);
|
|
1922
1923
|
case "web":
|
|
1923
|
-
return _WhatsAppAdapter.fromWeb(options.web,
|
|
1924
|
+
return _WhatsAppAdapter.fromWeb(options.web, common2);
|
|
1924
1925
|
case "baileys":
|
|
1925
|
-
return _WhatsAppAdapter.fromBaileys(options.baileys,
|
|
1926
|
+
return _WhatsAppAdapter.fromBaileys(options.baileys, common2);
|
|
1926
1927
|
}
|
|
1927
1928
|
}
|
|
1928
1929
|
/**
|
|
@@ -2169,8 +2170,249 @@ var WhatsAppAdapter = class _WhatsAppAdapter extends BasePlatformAdapter {
|
|
|
2169
2170
|
async deliver(event) {
|
|
2170
2171
|
return this.runHandler(this.handler, event);
|
|
2171
2172
|
}
|
|
2173
|
+
/**
|
|
2174
|
+
* Deliver like {@link deliver}, but a throwing handler is logged by name only: its message can
|
|
2175
|
+
* carry the inbound text, which the webhook route must not write to a log line.
|
|
2176
|
+
*
|
|
2177
|
+
* @internal
|
|
2178
|
+
*/
|
|
2179
|
+
async deliverQuietly(event) {
|
|
2180
|
+
const handler = this.handler;
|
|
2181
|
+
if (handler === void 0) return "no_handler";
|
|
2182
|
+
try {
|
|
2183
|
+
await handler(event);
|
|
2184
|
+
} catch {
|
|
2185
|
+
process.stderr.write(`[whatsapp] handler threw
|
|
2186
|
+
`);
|
|
2187
|
+
return "handler_threw";
|
|
2188
|
+
}
|
|
2189
|
+
return "ok";
|
|
2190
|
+
}
|
|
2191
|
+
};
|
|
2192
|
+
var configs = /* @__PURE__ */ new WeakMap();
|
|
2193
|
+
function rememberWebhookConfig(adapter, config) {
|
|
2194
|
+
configs.set(adapter, config);
|
|
2195
|
+
}
|
|
2196
|
+
function headerValue(headers, name) {
|
|
2197
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
2198
|
+
if (key.toLowerCase() === name) return value;
|
|
2199
|
+
}
|
|
2200
|
+
return void 0;
|
|
2201
|
+
}
|
|
2202
|
+
function safeEqual(a, b) {
|
|
2203
|
+
const ha = createHash("sha256").update(a).digest();
|
|
2204
|
+
const hb = createHash("sha256").update(b).digest();
|
|
2205
|
+
return timingSafeEqual(ha, hb);
|
|
2206
|
+
}
|
|
2207
|
+
function statusFor(outcomes) {
|
|
2208
|
+
let status = 200;
|
|
2209
|
+
for (const outcome of outcomes) {
|
|
2210
|
+
if (outcome === "handler_threw") status = 500;
|
|
2211
|
+
else if (outcome === "no_handler" && status === 200) status = 503;
|
|
2212
|
+
}
|
|
2213
|
+
return status;
|
|
2214
|
+
}
|
|
2215
|
+
function signatureValid(adapter, request) {
|
|
2216
|
+
const config = configs.get(adapter);
|
|
2217
|
+
if (config === void 0 || config.appSecret.trim().length === 0) return false;
|
|
2218
|
+
const header = headerValue(request.headers, "x-hub-signature-256");
|
|
2219
|
+
return verifyWebhookSignature(request.rawBody, header, config.appSecret);
|
|
2220
|
+
}
|
|
2221
|
+
function subscriptionChallenge(adapter, request) {
|
|
2222
|
+
const expected = configs.get(adapter)?.verifyToken;
|
|
2223
|
+
if (expected === void 0 || expected.length === 0 || request.url === void 0) {
|
|
2224
|
+
return void 0;
|
|
2225
|
+
}
|
|
2226
|
+
let params;
|
|
2227
|
+
try {
|
|
2228
|
+
params = new URL(request.url).searchParams;
|
|
2229
|
+
} catch {
|
|
2230
|
+
return void 0;
|
|
2231
|
+
}
|
|
2232
|
+
const token = params.get("hub.verify_token");
|
|
2233
|
+
const challenge = params.get("hub.challenge");
|
|
2234
|
+
if (params.get("hub.mode") !== "subscribe" || token === null || !safeEqual(token, expected)) {
|
|
2235
|
+
return void 0;
|
|
2236
|
+
}
|
|
2237
|
+
return challenge === null || challenge.length === 0 ? void 0 : challenge;
|
|
2238
|
+
}
|
|
2239
|
+
function requestValid(adapter, request) {
|
|
2240
|
+
const method = request.method?.toUpperCase();
|
|
2241
|
+
if (method === "GET") return subscriptionChallenge(adapter, request) !== void 0;
|
|
2242
|
+
if (method === "POST") return signatureValid(adapter, request);
|
|
2243
|
+
return false;
|
|
2244
|
+
}
|
|
2245
|
+
function isEnvelope(payload) {
|
|
2246
|
+
return payload !== null && typeof payload === "object" && !Array.isArray(payload);
|
|
2247
|
+
}
|
|
2248
|
+
async function deliverEnvelope(adapter, payload) {
|
|
2249
|
+
const outcomes = await deliverAll(adapter, payload);
|
|
2250
|
+
if (outcomes === void 0) return "malformed";
|
|
2251
|
+
return statusFor(outcomes) === 200 ? "delivered" : "no_handler";
|
|
2252
|
+
}
|
|
2253
|
+
async function deliverAll(adapter, payload) {
|
|
2254
|
+
if (!isEnvelope(payload)) return void 0;
|
|
2255
|
+
const outcomes = [];
|
|
2256
|
+
for (const event of adapter.toDeliverableEvents(payload)) {
|
|
2257
|
+
outcomes.push(await adapter.deliverQuietly(event));
|
|
2258
|
+
}
|
|
2259
|
+
return outcomes;
|
|
2260
|
+
}
|
|
2261
|
+
var cloudWebhook = {
|
|
2262
|
+
methods: ["GET", "POST"],
|
|
2263
|
+
verify(adapter, request) {
|
|
2264
|
+
return requestValid(adapter, request);
|
|
2265
|
+
},
|
|
2266
|
+
async ingest(adapter, payload) {
|
|
2267
|
+
return deliverEnvelope(adapter, payload);
|
|
2268
|
+
},
|
|
2269
|
+
async handle(adapter, request) {
|
|
2270
|
+
const isGet = request.method?.toUpperCase() === "GET";
|
|
2271
|
+
if (!requestValid(adapter, request)) return { status: isGet ? 403 : 401 };
|
|
2272
|
+
if (isGet) {
|
|
2273
|
+
return {
|
|
2274
|
+
status: 200,
|
|
2275
|
+
body: subscriptionChallenge(adapter, request),
|
|
2276
|
+
contentType: "text/plain"
|
|
2277
|
+
};
|
|
2278
|
+
}
|
|
2279
|
+
let payload;
|
|
2280
|
+
try {
|
|
2281
|
+
payload = JSON.parse(request.rawBody);
|
|
2282
|
+
} catch {
|
|
2283
|
+
return { status: 400 };
|
|
2284
|
+
}
|
|
2285
|
+
const outcomes = await deliverAll(adapter, payload);
|
|
2286
|
+
if (outcomes === void 0) return { status: 400 };
|
|
2287
|
+
return { status: statusFor(outcomes) };
|
|
2288
|
+
}
|
|
2289
|
+
};
|
|
2290
|
+
|
|
2291
|
+
// src/channel.ts
|
|
2292
|
+
function invalid(key, expected) {
|
|
2293
|
+
return new ConfigurationError({
|
|
2294
|
+
code: "invalid_config",
|
|
2295
|
+
message: `gateway-whatsapp: config.${key} must be ${expected}`,
|
|
2296
|
+
detail: key
|
|
2297
|
+
});
|
|
2298
|
+
}
|
|
2299
|
+
function read(config, key, kind) {
|
|
2300
|
+
const value = config[key];
|
|
2301
|
+
if (value === void 0 || value === null || value === "") return void 0;
|
|
2302
|
+
if (kind === "string" && typeof value === "string") return value;
|
|
2303
|
+
if (kind === "boolean" && typeof value === "boolean") return value;
|
|
2304
|
+
if (kind === "boolean" && (value === "true" || value === "false")) return value === "true";
|
|
2305
|
+
throw invalid(key, kind === "string" ? "a string" : 'a boolean or the string "true" or "false"');
|
|
2306
|
+
}
|
|
2307
|
+
function requiredString(config, key) {
|
|
2308
|
+
const value = config[key] === "" ? "" : read(config, key, "string");
|
|
2309
|
+
if (typeof value !== "string") throw invalid(key, "a string");
|
|
2310
|
+
return value;
|
|
2311
|
+
}
|
|
2312
|
+
var REQUIRE_MENTION = {
|
|
2313
|
+
key: "requireMention",
|
|
2314
|
+
required: false,
|
|
2315
|
+
secret: false,
|
|
2316
|
+
env: "WHATSAPP_REQUIRE_MENTION",
|
|
2317
|
+
description: "Whether group messages must mention the bot. Default true."
|
|
2318
|
+
};
|
|
2319
|
+
var CLOUD_CONFIG = [
|
|
2320
|
+
{
|
|
2321
|
+
key: "accessToken",
|
|
2322
|
+
required: true,
|
|
2323
|
+
secret: true,
|
|
2324
|
+
env: "WHATSAPP_ACCESS_TOKEN",
|
|
2325
|
+
description: "The Meta Cloud API access token."
|
|
2326
|
+
},
|
|
2327
|
+
{
|
|
2328
|
+
key: "phoneNumberId",
|
|
2329
|
+
required: true,
|
|
2330
|
+
secret: false,
|
|
2331
|
+
env: "WHATSAPP_PHONE_NUMBER_ID",
|
|
2332
|
+
description: "The Cloud API phone number id the adapter answers for."
|
|
2333
|
+
},
|
|
2334
|
+
{
|
|
2335
|
+
key: "appSecret",
|
|
2336
|
+
required: true,
|
|
2337
|
+
secret: true,
|
|
2338
|
+
env: "WHATSAPP_APP_SECRET",
|
|
2339
|
+
description: "The Meta app secret that signs inbound webhooks."
|
|
2340
|
+
},
|
|
2341
|
+
{
|
|
2342
|
+
key: "verifyToken",
|
|
2343
|
+
required: false,
|
|
2344
|
+
secret: true,
|
|
2345
|
+
env: "WHATSAPP_VERIFY_TOKEN",
|
|
2346
|
+
description: "The token Meta's webhook subscription check sends; the webhook's GET answers 403 without it."
|
|
2347
|
+
},
|
|
2348
|
+
REQUIRE_MENTION
|
|
2349
|
+
];
|
|
2350
|
+
var BAILEYS_CONFIG = [
|
|
2351
|
+
{
|
|
2352
|
+
key: "sessionDir",
|
|
2353
|
+
required: true,
|
|
2354
|
+
secret: false,
|
|
2355
|
+
env: "WHATSAPP_SESSION_DIR",
|
|
2356
|
+
description: "Directory where the Baileys session (credentials) is stored."
|
|
2357
|
+
},
|
|
2358
|
+
{
|
|
2359
|
+
key: "botPhoneId",
|
|
2360
|
+
required: false,
|
|
2361
|
+
secret: false,
|
|
2362
|
+
env: "WHATSAPP_BOT_PHONE_ID",
|
|
2363
|
+
description: "The bot's own number; with requireMention (default), a group message must mention it or is dropped."
|
|
2364
|
+
},
|
|
2365
|
+
REQUIRE_MENTION
|
|
2366
|
+
];
|
|
2367
|
+
function common(config) {
|
|
2368
|
+
const requireMention = read(config, "requireMention", "boolean");
|
|
2369
|
+
const botPhoneId = read(config, "botPhoneId", "string");
|
|
2370
|
+
return {
|
|
2371
|
+
...requireMention === void 0 ? {} : { requireMention },
|
|
2372
|
+
...botPhoneId === void 0 ? {} : { botPhoneId }
|
|
2373
|
+
};
|
|
2374
|
+
}
|
|
2375
|
+
function createCloud(config) {
|
|
2376
|
+
const accessToken = requiredString(config, "accessToken");
|
|
2377
|
+
const phoneNumberId = requiredString(config, "phoneNumberId");
|
|
2378
|
+
const appSecret = requiredString(config, "appSecret");
|
|
2379
|
+
const verifyToken = read(config, "verifyToken", "string");
|
|
2380
|
+
const opts = common(config);
|
|
2381
|
+
const adapter = WhatsAppAdapter.from(
|
|
2382
|
+
{ backend: "cloud", cloud: { accessToken, phoneNumberId, appSecret } },
|
|
2383
|
+
opts
|
|
2384
|
+
);
|
|
2385
|
+
rememberWebhookConfig(adapter, {
|
|
2386
|
+
appSecret,
|
|
2387
|
+
...verifyToken === void 0 ? {} : { verifyToken }
|
|
2388
|
+
});
|
|
2389
|
+
return adapter;
|
|
2390
|
+
}
|
|
2391
|
+
function createBaileys(config) {
|
|
2392
|
+
const sessionDir = requiredString(config, "sessionDir");
|
|
2393
|
+
const opts = common(config);
|
|
2394
|
+
return WhatsAppAdapter.from({ backend: "baileys", baileys: { sessionDir } }, opts);
|
|
2395
|
+
}
|
|
2396
|
+
var cloudChannel = {
|
|
2397
|
+
platform: "whatsapp",
|
|
2398
|
+
transport: "webhook",
|
|
2399
|
+
webhook: cloudWebhook,
|
|
2400
|
+
senderIdentity: "authenticated",
|
|
2401
|
+
config: CLOUD_CONFIG,
|
|
2402
|
+
create: createCloud
|
|
2403
|
+
};
|
|
2404
|
+
var baileysChannel = {
|
|
2405
|
+
platform: "whatsapp",
|
|
2406
|
+
transport: "socket",
|
|
2407
|
+
senderIdentity: "authenticated",
|
|
2408
|
+
config: BAILEYS_CONFIG,
|
|
2409
|
+
create: createBaileys
|
|
2410
|
+
};
|
|
2411
|
+
var whatsappChannel = {
|
|
2412
|
+
...cloudChannel,
|
|
2413
|
+
variants: { cloud: cloudChannel, baileys: baileysChannel }
|
|
2172
2414
|
};
|
|
2173
2415
|
|
|
2174
|
-
export { ConfigurationError, LineBuffer, WhatsAppAdapter, WhatsAppBaileysBackend, WhatsAppBridgeError, WhatsAppCloudBackend, WhatsAppConnectTimeoutError, WhatsAppWebBackend, createBaileysSocket, defaultBridgeScriptPath, digitsOnly, formatCommand, mapWhatsAppCloudError, mapWhatsAppWebError, normalizeInboundMessages, normalizeStatusReceipts, parseEvent, parseWebhookPayload, splitForWhatsApp, verifyWebhookSignature, verifyWebhookSubscription };
|
|
2416
|
+
export { ConfigurationError, LineBuffer, WhatsAppAdapter, WhatsAppBaileysBackend, WhatsAppBridgeError, WhatsAppCloudBackend, WhatsAppConnectTimeoutError, WhatsAppWebBackend, createBaileysSocket, defaultBridgeScriptPath, digitsOnly, formatCommand, mapWhatsAppCloudError, mapWhatsAppWebError, normalizeInboundMessages, normalizeStatusReceipts, parseEvent, parseWebhookPayload, splitForWhatsApp, verifyWebhookSignature, verifyWebhookSubscription, whatsappChannel };
|
|
2175
2417
|
//# sourceMappingURL=index.js.map
|
|
2176
2418
|
//# sourceMappingURL=index.js.map
|