@sockethub/platform-whatsapp 1.0.0-beta.3 → 1.0.0-beta.5
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/index.js +6 -6
- package/dist/index.js.map +4 -4
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
var package_default = {
|
|
3
3
|
name: "@sockethub/platform-whatsapp",
|
|
4
4
|
description: "Outgoing text messages through the WhatsApp Business Cloud API",
|
|
5
|
-
version: "1.0.0-beta.
|
|
5
|
+
version: "1.0.0-beta.5",
|
|
6
6
|
type: "module",
|
|
7
7
|
private: false,
|
|
8
8
|
license: "LGPL-3.0+",
|
|
@@ -31,20 +31,20 @@ var package_default = {
|
|
|
31
31
|
test: "bun test src/"
|
|
32
32
|
},
|
|
33
33
|
dependencies: {
|
|
34
|
-
"@sockethub/schemas": "^3.0.0-beta.
|
|
34
|
+
"@sockethub/schemas": "^3.0.0-beta.5"
|
|
35
35
|
},
|
|
36
36
|
devDependencies: {
|
|
37
37
|
"@types/bun": "latest"
|
|
38
38
|
},
|
|
39
39
|
peerDependencies: {
|
|
40
|
-
"@sockethub/server": "^5.0.0-beta.
|
|
40
|
+
"@sockethub/server": "^5.0.0-beta.5"
|
|
41
41
|
},
|
|
42
42
|
peerDependenciesMeta: {
|
|
43
43
|
"@sockethub/server": {
|
|
44
44
|
optional: true
|
|
45
45
|
}
|
|
46
46
|
},
|
|
47
|
-
gitHead: "
|
|
47
|
+
gitHead: "cc4ffdd9714ebbd8372a88c4f2d795974e04029e"
|
|
48
48
|
};
|
|
49
49
|
|
|
50
50
|
// src/schema.ts
|
|
@@ -166,7 +166,7 @@ class WhatsApp {
|
|
|
166
166
|
body: JSON.stringify({
|
|
167
167
|
messaging_product: "whatsapp",
|
|
168
168
|
recipient_type: "individual",
|
|
169
|
-
to: job.target.id
|
|
169
|
+
to: job.target.id.startsWith("+") ? job.target.id : `+${job.target.id}`,
|
|
170
170
|
type: "text",
|
|
171
171
|
text: { body: job.object.content, preview_url: false }
|
|
172
172
|
}),
|
|
@@ -209,4 +209,4 @@ export {
|
|
|
209
209
|
WhatsApp as default
|
|
210
210
|
};
|
|
211
211
|
|
|
212
|
-
//# debugId=
|
|
212
|
+
//# debugId=C22874587E17191664756E2164756E21
|
package/dist/index.js.map
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/schema.ts", "../src/index.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"import packageJson from \"../package.json\" with { type: \"json\" };\n\n// actor: WhatsApp Business phone number ID (Meta-assigned, numeric).\nconst actor = {\n type: \"object\",\n required: [\"id\", \"type\"],\n additionalProperties: false,\n properties: {\n id: { type: \"string\", pattern: \"^[0-9]+$\", maxLength: 128 },\n type: { const: \"person\" },\n },\n};\n\n// target: recipient phone number in international format (E.164
|
|
6
|
-
"import type {\n ActivityStream,\n CredentialsObject,\n Logger,\n PlatformCallback,\n PlatformInterface,\n PlatformSession,\n StatelessPlatformConfig,\n} from \"@sockethub/schemas\";\nimport { PlatformWhatsAppSchema } from \"./schema.js\";\n\nexport interface WhatsAppCredentials extends CredentialsObject {\n object: { type: \"credentials\"; token: string; apiVersion: string };\n}\n\nexport default class WhatsApp implements PlatformInterface {\n private readonly log: Logger;\n config: StatelessPlatformConfig = {\n persist: false,\n requireCredentials: [\"send\"],\n };\n\n constructor(session: PlatformSession) {\n this.log = session.log;\n }\n\n get schema() {\n return PlatformWhatsAppSchema;\n }\n\n isInitialized(): boolean {\n return true;\n }\n\n cleanup(done: PlatformCallback): void {\n done();\n }\n\n async send(\n job: ActivityStream,\n credentials: WhatsAppCredentials,\n done: PlatformCallback,\n ): Promise<void> {\n const { token, apiVersion } = credentials.object;\n let result: ActivityStream;\n try {\n const response = await fetch(\n `https://graph.facebook.com/${apiVersion}/${encodeURIComponent(job.actor.id)}/messages`,\n {\n method: \"POST\",\n redirect: \"error\",\n headers: {\n authorization: `Bearer ${token}`,\n \"content-type\": \"application/json\",\n },\n body: JSON.stringify({\n messaging_product: \"whatsapp\",\n recipient_type: \"individual\",\n to: job.target.id
|
|
5
|
+
"import packageJson from \"../package.json\" with { type: \"json\" };\n\n// actor: WhatsApp Business phone number ID (Meta-assigned, numeric).\nconst actor = {\n type: \"object\",\n required: [\"id\", \"type\"],\n additionalProperties: false,\n properties: {\n id: { type: \"string\", pattern: \"^[0-9]+$\", maxLength: 128 },\n type: { const: \"person\" },\n },\n};\n\n// target: recipient phone number in international format (E.164).\n// A leading \"+\" is optional here; send() adds it. Meta treats a missing\n// \"+\" as a national number and prepends the business country code.\nconst target = {\n type: \"object\",\n required: [\"id\", \"type\"],\n additionalProperties: false,\n properties: {\n id: { type: \"string\", pattern: \"^\\\\+?[1-9][0-9]{6,14}$\" },\n type: { const: \"person\" },\n },\n};\n\nexport const PlatformWhatsAppSchema = {\n name: \"whatsapp\",\n version: packageJson.version,\n contextUrl: \"https://sockethub.org/ns/context/platform/whatsapp/v1.jsonld\",\n contextVersion: \"1\",\n schemaVersion: \"1\",\n messages: {\n required: [\"type\", \"actor\", \"target\", \"object\"],\n properties: {\n type: { enum: [\"send\"] },\n actor,\n target,\n object: {\n type: \"object\",\n required: [\"type\", \"content\"],\n additionalProperties: false,\n properties: {\n type: { const: \"message\" },\n content: { type: \"string\", minLength: 1, maxLength: 4096 },\n },\n },\n },\n },\n credentials: {\n required: [\"object\"],\n properties: {\n object: {\n type: \"object\",\n required: [\"type\", \"token\", \"apiVersion\"],\n additionalProperties: false,\n properties: {\n type: { const: \"credentials\" },\n token: {\n type: \"string\",\n minLength: 1,\n maxLength: 8192,\n pattern: \"^[^\\\\r\\\\n]+$\",\n },\n apiVersion: {\n type: \"string\",\n pattern: \"^v[0-9]{1,3}\\\\.[0-9]{1,2}$\",\n },\n },\n },\n },\n },\n responses: {\n type: \"object\",\n required: [\"@context\", \"type\", \"actor\", \"target\", \"object\"],\n properties: {\n \"@context\": { type: \"array\", items: { type: \"string\" } },\n type: { const: \"send\" },\n actor,\n target,\n object: {\n type: \"object\",\n required: [\"type\", \"id\", \"content\"],\n properties: {\n type: { const: \"message\" },\n id: { type: \"string\", minLength: 1 },\n content: { type: \"string\" },\n },\n },\n },\n },\n};\n",
|
|
6
|
+
"import type {\n ActivityStream,\n CredentialsObject,\n Logger,\n PlatformCallback,\n PlatformInterface,\n PlatformSession,\n StatelessPlatformConfig,\n} from \"@sockethub/schemas\";\nimport { PlatformWhatsAppSchema } from \"./schema.js\";\n\nexport interface WhatsAppCredentials extends CredentialsObject {\n object: { type: \"credentials\"; token: string; apiVersion: string };\n}\n\nexport default class WhatsApp implements PlatformInterface {\n private readonly log: Logger;\n config: StatelessPlatformConfig = {\n persist: false,\n requireCredentials: [\"send\"],\n };\n\n constructor(session: PlatformSession) {\n this.log = session.log;\n }\n\n get schema() {\n return PlatformWhatsAppSchema;\n }\n\n isInitialized(): boolean {\n return true;\n }\n\n cleanup(done: PlatformCallback): void {\n done();\n }\n\n async send(\n job: ActivityStream,\n credentials: WhatsAppCredentials,\n done: PlatformCallback,\n ): Promise<void> {\n const { token, apiVersion } = credentials.object;\n let result: ActivityStream;\n try {\n const response = await fetch(\n `https://graph.facebook.com/${apiVersion}/${encodeURIComponent(job.actor.id)}/messages`,\n {\n method: \"POST\",\n redirect: \"error\",\n headers: {\n authorization: `Bearer ${token}`,\n \"content-type\": \"application/json\",\n },\n body: JSON.stringify({\n messaging_product: \"whatsapp\",\n recipient_type: \"individual\",\n // Digits-only E.164 is valid input, but Meta prepends the\n // business number's country calling code when `to` has no\n // leading \"+\", which misdelivers the message.\n to: job.target.id.startsWith(\"+\")\n ? job.target.id\n : `+${job.target.id}`,\n type: \"text\",\n text: { body: job.object.content, preview_url: false },\n }),\n signal: AbortSignal.timeout(15_000),\n },\n );\n const data: unknown = await response.json();\n if (!data || typeof data !== \"object\") {\n throw new Error(\"whatsapp: invalid API response\");\n }\n if (\"error\" in data || !response.ok) {\n const error = \"error\" in data ? data.error : undefined;\n let detail = `HTTP ${response.status}`;\n if (error && typeof error === \"object\") {\n if (\"code\" in error && typeof error.code === \"number\") {\n detail += `, code ${error.code}`;\n }\n if (\n \"message\" in error &&\n typeof error.message === \"string\"\n ) {\n detail += `: ${error.message.split(token).join(\"[redacted]\")}`;\n }\n }\n // 131047: more than 24 hours since the recipient last messaged.\n const windowClosed =\n error &&\n typeof error === \"object\" &&\n \"code\" in error &&\n error.code === 131047;\n const reason = windowClosed\n ? \"meta:messaging-window-closed\"\n : \"whatsapp: send rejected\";\n throw new Error(`${reason} (${detail})`);\n }\n const id =\n \"messages\" in data &&\n Array.isArray(data.messages) &&\n data.messages[0] &&\n typeof data.messages[0] === \"object\" &&\n \"id\" in data.messages[0]\n ? data.messages[0].id\n : undefined;\n if (typeof id !== \"string\" || !id) {\n throw new Error(\"whatsapp: API response missing message id\");\n }\n result = { ...job, object: { ...job.object, id } };\n } catch (error) {\n // Network errors may contain request details; never expose tokens.\n const failure =\n error instanceof Error &&\n (error.message.startsWith(\"whatsapp:\") ||\n error.message.startsWith(\"meta:messaging-window-closed\"))\n ? new Error(error.message.split(token).join(\"[redacted]\"))\n : new Error(\n \"whatsapp: send failed (network error, timeout, or invalid JSON); delivery is unknown\",\n );\n this.log.warn(`send failed for actor ${job.actor.id} on whatsapp`);\n done(failure);\n return;\n }\n done(null, result);\n }\n}\n"
|
|
7
7
|
],
|
|
8
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,IAAM,QAAQ;AAAA,EACV,MAAM;AAAA,EACN,UAAU,CAAC,MAAM,MAAM;AAAA,EACvB,sBAAsB;AAAA,EACtB,YAAY;AAAA,IACR,IAAI,EAAE,MAAM,UAAU,SAAS,YAAY,WAAW,IAAI;AAAA,IAC1D,MAAM,EAAE,OAAO,SAAS;AAAA,EAC5B;AACJ;
|
|
9
|
-
"debugId": "
|
|
8
|
+
"mappings": ";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3B,KAAK,SAAS,MACd,OAAO,KAAK,SAAS,OAAO,YAC5B,QAAQ,KAAK,SAAS,KAChB,KAAK,SAAS,GAAG,KACjB;AAAA,MACV,IAAI,OAAO,OAAO,YAAY,CAAC,IAAI;AAAA,QAC/B,MAAM,IAAI,MAAM,2CAA2C;AAAA,MAC/D;AAAA,MACA,SAAS,KAAK,KAAK,QAAQ,KAAK,IAAI,QAAQ,GAAG,EAAE;AAAA,MACnD,OAAO,OAAO;AAAA,MAEZ,MAAM,UACF,iBAAiB,UAChB,MAAM,QAAQ,WAAW,WAAW,KACjC,MAAM,QAAQ,WAAW,8BAA8B,KACrD,IAAI,MAAM,MAAM,QAAQ,MAAM,KAAK,EAAE,KAAK,YAAY,CAAC,IACvD,IAAI,MACA,sFACJ;AAAA,MACV,KAAK,IAAI,KAAK,yBAAyB,IAAI,MAAM,gBAAgB;AAAA,MACjE,KAAK,OAAO;AAAA,MACZ;AAAA;AAAA,IAEJ,KAAK,MAAM,MAAM;AAAA;AAEzB;",
|
|
9
|
+
"debugId": "C22874587E17191664756E2164756E21",
|
|
10
10
|
"names": []
|
|
11
11
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sockethub/platform-whatsapp",
|
|
3
3
|
"description": "Outgoing text messages through the WhatsApp Business Cloud API",
|
|
4
|
-
"version": "1.0.0-beta.
|
|
4
|
+
"version": "1.0.0-beta.5",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
7
7
|
"license": "LGPL-3.0+",
|
|
@@ -30,18 +30,18 @@
|
|
|
30
30
|
"test": "bun test src/"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@sockethub/schemas": "^3.0.0-beta.
|
|
33
|
+
"@sockethub/schemas": "^3.0.0-beta.5"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@types/bun": "latest"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
|
-
"@sockethub/server": "^5.0.0-beta.
|
|
39
|
+
"@sockethub/server": "^5.0.0-beta.5"
|
|
40
40
|
},
|
|
41
41
|
"peerDependenciesMeta": {
|
|
42
42
|
"@sockethub/server": {
|
|
43
43
|
"optional": true
|
|
44
44
|
}
|
|
45
45
|
},
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "2dffe4cc1bea5375e38531b1012b56bcbab93569"
|
|
47
47
|
}
|