@theokit/gateway-whatsapp 0.7.0 → 0.8.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 +6 -0
- package/README.md +30 -0
- package/dist/index.cjs +124 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +57 -1
- package/dist/index.d.ts +57 -1
- package/dist/index.js +124 -5
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.8.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 9d477da: Export a `ChannelDescriptor` with env bindings: `whatsappChannel` (variants `cloud` and `baileys`), `teamsChannel` and `googleChatChannel`, for `createChannels` and `configFromEnv`. The `@theokit/gateway` peer range is now `>=0.20.0 <1.0.0`.
|
|
8
|
+
|
|
3
9
|
## 0.7.0
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -211,3 +211,33 @@ it exists (B-001).
|
|
|
211
211
|
## Capabilities
|
|
212
212
|
|
|
213
213
|
`adapter.capabilities.maxMessageChars` is `4096` (the splitter limit) and `threads` is `false`. See `@theokit/gateway` for the field meanings.
|
|
214
|
+
|
|
215
|
+
## Channel descriptor
|
|
216
|
+
|
|
217
|
+
`whatsappChannel` describes the channel as data, for `createChannels` and `configFromEnv` from
|
|
218
|
+
`@theokit/gateway`. It has two variants, chosen with `selectVariant(whatsappChannel, name)`; the
|
|
219
|
+
descriptor itself is the `cloud` shape.
|
|
220
|
+
|
|
221
|
+
| Variant | Field | Env | Required | Secret |
|
|
222
|
+
| --- | --- | --- | --- | --- |
|
|
223
|
+
| `cloud` | `accessToken` | `WHATSAPP_ACCESS_TOKEN` | yes | yes |
|
|
224
|
+
| `cloud` | `phoneNumberId` | `WHATSAPP_PHONE_NUMBER_ID` | yes | no |
|
|
225
|
+
| `cloud` | `appSecret` | `WHATSAPP_APP_SECRET` | yes | yes |
|
|
226
|
+
| `cloud` | `verifyToken` | `WHATSAPP_VERIFY_TOKEN` | no | yes |
|
|
227
|
+
| `baileys` | `sessionDir` | `WHATSAPP_SESSION_DIR` | yes | no |
|
|
228
|
+
| `baileys` | `botPhoneId` | `WHATSAPP_BOT_PHONE_ID` | no | no |
|
|
229
|
+
| both | `requireMention` | `WHATSAPP_REQUIRE_MENTION` | no | no |
|
|
230
|
+
|
|
231
|
+
`verifyToken` is not an adapter option: it is kept so the webhook half can answer Meta's
|
|
232
|
+
subscription check.
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
import { configFromEnv, createChannels, selectVariant } from "@theokit/gateway";
|
|
236
|
+
import { whatsappChannel } from "@theokit/gateway-whatsapp";
|
|
237
|
+
|
|
238
|
+
const descriptor = selectVariant(whatsappChannel, "cloud");
|
|
239
|
+
const { config, missing } = configFromEnv(descriptor, process.env, "APP_");
|
|
240
|
+
const { adapters, rejected } = createChannels([{ descriptor, config }]);
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
`create` refuses a value of the wrong type with `ConfigurationError` (`invalid_config`, naming the key).
|
package/dist/index.cjs
CHANGED
|
@@ -1939,14 +1939,14 @@ var WhatsAppAdapter = class _WhatsAppAdapter extends gateway.BasePlatformAdapter
|
|
|
1939
1939
|
* @throws {ConfigurationError} when a required option is missing or empty.
|
|
1940
1940
|
* @public
|
|
1941
1941
|
*/
|
|
1942
|
-
static from(options,
|
|
1942
|
+
static from(options, common2 = {}) {
|
|
1943
1943
|
switch (options.backend) {
|
|
1944
1944
|
case "cloud":
|
|
1945
|
-
return _WhatsAppAdapter.fromCloud(options.cloud,
|
|
1945
|
+
return _WhatsAppAdapter.fromCloud(options.cloud, common2);
|
|
1946
1946
|
case "web":
|
|
1947
|
-
return _WhatsAppAdapter.fromWeb(options.web,
|
|
1947
|
+
return _WhatsAppAdapter.fromWeb(options.web, common2);
|
|
1948
1948
|
case "baileys":
|
|
1949
|
-
return _WhatsAppAdapter.fromBaileys(options.baileys,
|
|
1949
|
+
return _WhatsAppAdapter.fromBaileys(options.baileys, common2);
|
|
1950
1950
|
}
|
|
1951
1951
|
}
|
|
1952
1952
|
/**
|
|
@@ -2195,6 +2195,125 @@ var WhatsAppAdapter = class _WhatsAppAdapter extends gateway.BasePlatformAdapter
|
|
|
2195
2195
|
}
|
|
2196
2196
|
};
|
|
2197
2197
|
|
|
2198
|
+
// src/channel.ts
|
|
2199
|
+
function invalid(key, expected) {
|
|
2200
|
+
return new ConfigurationError({
|
|
2201
|
+
code: "invalid_config",
|
|
2202
|
+
message: `gateway-whatsapp: config.${key} must be ${expected}`,
|
|
2203
|
+
detail: key
|
|
2204
|
+
});
|
|
2205
|
+
}
|
|
2206
|
+
function read(config, key, kind) {
|
|
2207
|
+
const value = config[key];
|
|
2208
|
+
if (value === void 0 || value === null || value === "") return void 0;
|
|
2209
|
+
if (kind === "string" && typeof value === "string") return value;
|
|
2210
|
+
if (kind === "boolean" && typeof value === "boolean") return value;
|
|
2211
|
+
if (kind === "boolean" && (value === "true" || value === "false")) return value === "true";
|
|
2212
|
+
throw invalid(key, kind === "string" ? "a string" : 'a boolean or the string "true" or "false"');
|
|
2213
|
+
}
|
|
2214
|
+
function requiredString(config, key) {
|
|
2215
|
+
const value = config[key] === "" ? "" : read(config, key, "string");
|
|
2216
|
+
if (typeof value !== "string") throw invalid(key, "a string");
|
|
2217
|
+
return value;
|
|
2218
|
+
}
|
|
2219
|
+
var REQUIRE_MENTION = {
|
|
2220
|
+
key: "requireMention",
|
|
2221
|
+
required: false,
|
|
2222
|
+
secret: false,
|
|
2223
|
+
env: "WHATSAPP_REQUIRE_MENTION",
|
|
2224
|
+
description: "Whether group messages must mention the bot. Default true."
|
|
2225
|
+
};
|
|
2226
|
+
var CLOUD_CONFIG = [
|
|
2227
|
+
{
|
|
2228
|
+
key: "accessToken",
|
|
2229
|
+
required: true,
|
|
2230
|
+
secret: true,
|
|
2231
|
+
env: "WHATSAPP_ACCESS_TOKEN",
|
|
2232
|
+
description: "The Meta Cloud API access token."
|
|
2233
|
+
},
|
|
2234
|
+
{
|
|
2235
|
+
key: "phoneNumberId",
|
|
2236
|
+
required: true,
|
|
2237
|
+
secret: false,
|
|
2238
|
+
env: "WHATSAPP_PHONE_NUMBER_ID",
|
|
2239
|
+
description: "The Cloud API phone number id the adapter answers for."
|
|
2240
|
+
},
|
|
2241
|
+
{
|
|
2242
|
+
key: "appSecret",
|
|
2243
|
+
required: true,
|
|
2244
|
+
secret: true,
|
|
2245
|
+
env: "WHATSAPP_APP_SECRET",
|
|
2246
|
+
description: "The Meta app secret that signs inbound webhooks."
|
|
2247
|
+
},
|
|
2248
|
+
{
|
|
2249
|
+
key: "verifyToken",
|
|
2250
|
+
required: false,
|
|
2251
|
+
secret: true,
|
|
2252
|
+
env: "WHATSAPP_VERIFY_TOKEN",
|
|
2253
|
+
description: "The token Meta's webhook subscription check sends; not an adapter option, kept so the webhook half can answer it."
|
|
2254
|
+
},
|
|
2255
|
+
REQUIRE_MENTION
|
|
2256
|
+
];
|
|
2257
|
+
var BAILEYS_CONFIG = [
|
|
2258
|
+
{
|
|
2259
|
+
key: "sessionDir",
|
|
2260
|
+
required: true,
|
|
2261
|
+
secret: false,
|
|
2262
|
+
env: "WHATSAPP_SESSION_DIR",
|
|
2263
|
+
description: "Directory where the Baileys session (credentials) is stored."
|
|
2264
|
+
},
|
|
2265
|
+
{
|
|
2266
|
+
key: "botPhoneId",
|
|
2267
|
+
required: false,
|
|
2268
|
+
secret: false,
|
|
2269
|
+
env: "WHATSAPP_BOT_PHONE_ID",
|
|
2270
|
+
description: "The bot's own number; with requireMention (default), a group message must mention it or is dropped."
|
|
2271
|
+
},
|
|
2272
|
+
REQUIRE_MENTION
|
|
2273
|
+
];
|
|
2274
|
+
function common(config) {
|
|
2275
|
+
const requireMention = read(config, "requireMention", "boolean");
|
|
2276
|
+
const botPhoneId = read(config, "botPhoneId", "string");
|
|
2277
|
+
return {
|
|
2278
|
+
...requireMention === void 0 ? {} : { requireMention },
|
|
2279
|
+
...botPhoneId === void 0 ? {} : { botPhoneId }
|
|
2280
|
+
};
|
|
2281
|
+
}
|
|
2282
|
+
function createCloud(config) {
|
|
2283
|
+
const accessToken = requiredString(config, "accessToken");
|
|
2284
|
+
const phoneNumberId = requiredString(config, "phoneNumberId");
|
|
2285
|
+
const appSecret = requiredString(config, "appSecret");
|
|
2286
|
+
read(config, "verifyToken", "string");
|
|
2287
|
+
const opts = common(config);
|
|
2288
|
+
return WhatsAppAdapter.from(
|
|
2289
|
+
{ backend: "cloud", cloud: { accessToken, phoneNumberId, appSecret } },
|
|
2290
|
+
opts
|
|
2291
|
+
);
|
|
2292
|
+
}
|
|
2293
|
+
function createBaileys(config) {
|
|
2294
|
+
const sessionDir = requiredString(config, "sessionDir");
|
|
2295
|
+
const opts = common(config);
|
|
2296
|
+
return WhatsAppAdapter.from({ backend: "baileys", baileys: { sessionDir } }, opts);
|
|
2297
|
+
}
|
|
2298
|
+
var cloudChannel = {
|
|
2299
|
+
platform: "whatsapp",
|
|
2300
|
+
transport: "socket",
|
|
2301
|
+
senderIdentity: "authenticated",
|
|
2302
|
+
config: CLOUD_CONFIG,
|
|
2303
|
+
create: createCloud
|
|
2304
|
+
};
|
|
2305
|
+
var baileysChannel = {
|
|
2306
|
+
platform: "whatsapp",
|
|
2307
|
+
transport: "socket",
|
|
2308
|
+
senderIdentity: "authenticated",
|
|
2309
|
+
config: BAILEYS_CONFIG,
|
|
2310
|
+
create: createBaileys
|
|
2311
|
+
};
|
|
2312
|
+
var whatsappChannel = {
|
|
2313
|
+
...cloudChannel,
|
|
2314
|
+
variants: { cloud: cloudChannel, baileys: baileysChannel }
|
|
2315
|
+
};
|
|
2316
|
+
|
|
2198
2317
|
exports.ConfigurationError = ConfigurationError;
|
|
2199
2318
|
exports.LineBuffer = LineBuffer;
|
|
2200
2319
|
exports.WhatsAppAdapter = WhatsAppAdapter;
|
|
@@ -2216,5 +2335,6 @@ exports.parseWebhookPayload = parseWebhookPayload;
|
|
|
2216
2335
|
exports.splitForWhatsApp = splitForWhatsApp;
|
|
2217
2336
|
exports.verifyWebhookSignature = verifyWebhookSignature;
|
|
2218
2337
|
exports.verifyWebhookSubscription = verifyWebhookSubscription;
|
|
2338
|
+
exports.whatsappChannel = whatsappChannel;
|
|
2219
2339
|
//# sourceMappingURL=index.cjs.map
|
|
2220
2340
|
//# sourceMappingURL=index.cjs.map
|