@im-more/dsh-whatsapp 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/cordis.patch.yml +12 -0
- package/lib/index.js +1694 -0
- package/lib/index.js.map +1 -0
- package/lib/types/bot.d.ts +66 -0
- package/lib/types/bot.d.ts.map +1 -0
- package/lib/types/bot.js +164 -0
- package/lib/types/bot.js.map +1 -0
- package/lib/types/config.d.ts +40 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/config.js +63 -0
- package/lib/types/config.js.map +1 -0
- package/lib/types/decode.d.ts +64 -0
- package/lib/types/decode.d.ts.map +1 -0
- package/lib/types/decode.js +244 -0
- package/lib/types/decode.js.map +1 -0
- package/lib/types/errors.d.ts +53 -0
- package/lib/types/errors.d.ts.map +1 -0
- package/lib/types/errors.js +149 -0
- package/lib/types/errors.js.map +1 -0
- package/lib/types/identity.d.ts +48 -0
- package/lib/types/identity.d.ts.map +1 -0
- package/lib/types/identity.js +52 -0
- package/lib/types/identity.js.map +1 -0
- package/lib/types/index.d.ts +16 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +15 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/interactive.d.ts +45 -0
- package/lib/types/interactive.d.ts.map +1 -0
- package/lib/types/interactive.js +111 -0
- package/lib/types/interactive.js.map +1 -0
- package/lib/types/internal.d.ts +28 -0
- package/lib/types/internal.d.ts.map +1 -0
- package/lib/types/internal.js +35 -0
- package/lib/types/internal.js.map +1 -0
- package/lib/types/media.d.ts +17 -0
- package/lib/types/media.d.ts.map +1 -0
- package/lib/types/media.js +49 -0
- package/lib/types/media.js.map +1 -0
- package/lib/types/modes/shared.d.ts +112 -0
- package/lib/types/modes/shared.d.ts.map +1 -0
- package/lib/types/modes/shared.js +285 -0
- package/lib/types/modes/shared.js.map +1 -0
- package/lib/types/modes/text.d.ts +19 -0
- package/lib/types/modes/text.d.ts.map +1 -0
- package/lib/types/modes/text.js +126 -0
- package/lib/types/modes/text.js.map +1 -0
- package/lib/types/request.d.ts +61 -0
- package/lib/types/request.d.ts.map +1 -0
- package/lib/types/request.js +193 -0
- package/lib/types/request.js.map +1 -0
- package/lib/types/send.d.ts +47 -0
- package/lib/types/send.d.ts.map +1 -0
- package/lib/types/send.js +74 -0
- package/lib/types/send.js.map +1 -0
- package/lib/types/signature.d.ts +33 -0
- package/lib/types/signature.d.ts.map +1 -0
- package/lib/types/signature.js +46 -0
- package/lib/types/signature.js.map +1 -0
- package/lib/types/transport/webhook.d.ts +50 -0
- package/lib/types/transport/webhook.d.ts.map +1 -0
- package/lib/types/transport/webhook.js +197 -0
- package/lib/types/transport/webhook.js.map +1 -0
- package/lib/types/types.d.ts +198 -0
- package/lib/types/types.d.ts.map +1 -0
- package/lib/types/types.js +2 -0
- package/lib/types/types.js.map +1 -0
- package/package.json +61 -3
- package/src/bot.ts +233 -0
- package/src/config.ts +98 -0
- package/src/decode.ts +335 -0
- package/src/errors.ts +189 -0
- package/src/identity.ts +76 -0
- package/src/index.ts +100 -0
- package/src/interactive.ts +135 -0
- package/src/internal.ts +49 -0
- package/src/media.ts +59 -0
- package/src/modes/shared.ts +354 -0
- package/src/modes/text.ts +174 -0
- package/src/request.ts +238 -0
- package/src/send.ts +120 -0
- package/src/signature.ts +54 -0
- package/src/transport/webhook.ts +267 -0
- package/src/types.ts +244 -0
- package/README.md +0 -3
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inbound decoding: one change of a webhook delivery becomes zero or more intake `Session`s.
|
|
3
|
+
*
|
|
4
|
+
* The Cloud API pushes everything at once — several entries, each with several changes, each with several messages —
|
|
5
|
+
* and there is no API that reads a message back, so this payload is the only copy of the content. Every message is
|
|
6
|
+
* decoded; a chat is always the sender's own `wa_id`, because this adapter supports no group.
|
|
7
|
+
*/
|
|
8
|
+
import type { ChatRef, Session, UserRef } from '@deepseek-ai/cordis-plugin-im-core';
|
|
9
|
+
import type { WhatsappBot } from './bot.ts';
|
|
10
|
+
import type { Change, MessageBody, MessageBodyBase } from './types.ts';
|
|
11
|
+
/** Adapter-private context carried through `Target.referrer`. */
|
|
12
|
+
export interface WhatsappReferrer {
|
|
13
|
+
/** The peer's `wa_id`: the `chat.id` and the `to` of every send. */
|
|
14
|
+
waId: string;
|
|
15
|
+
/** The `wamid` of the inbound message. */
|
|
16
|
+
messageId: string;
|
|
17
|
+
/** The business number's Graph id; the `{phoneNumberId}` of every send. */
|
|
18
|
+
phoneNumberId: string;
|
|
19
|
+
timestamp: number;
|
|
20
|
+
}
|
|
21
|
+
/** What an interaction or reaction session needs from the payload it came from. */
|
|
22
|
+
export interface WhatsappContext {
|
|
23
|
+
chat: ChatRef;
|
|
24
|
+
user: UserRef;
|
|
25
|
+
referrer: WhatsappReferrer;
|
|
26
|
+
timestamp: number;
|
|
27
|
+
}
|
|
28
|
+
/** The direct chat of one sender; a Cloud API conversation has exactly one peer. */
|
|
29
|
+
export declare function chatRefOf(message: MessageBodyBase): ChatRef;
|
|
30
|
+
/**
|
|
31
|
+
* The sender of one message.
|
|
32
|
+
*
|
|
33
|
+
* The payload names the sender's display name in `contacts`, keyed by `wa_id` — a batch can carry several senders, so
|
|
34
|
+
* the entry is matched instead of taken from the first position.
|
|
35
|
+
* @param message - the message being decoded.
|
|
36
|
+
* @param contacts - the `contacts` array of the same change.
|
|
37
|
+
* @returns the sender reference, named when the payload names it.
|
|
38
|
+
*/
|
|
39
|
+
export declare function senderOf(message: MessageBodyBase, contacts: Change['value']['contacts']): UserRef;
|
|
40
|
+
/** The platform timestamp of a message: Meta sends seconds as a string. */
|
|
41
|
+
export declare function timeOf(message: MessageBodyBase): number;
|
|
42
|
+
/** The adapter context an interaction or reaction session is built from. */
|
|
43
|
+
export declare function contextOf(bot: WhatsappBot, message: MessageBodyBase, contacts: Change['value']['contacts']): WhatsappContext;
|
|
44
|
+
/**
|
|
45
|
+
* Decode every message of one change.
|
|
46
|
+
*
|
|
47
|
+
* A non-`messages` change family is dropped with a debug line: the IM host has no consumer for account, template or
|
|
48
|
+
* quality updates. Delivery receipts never reach the message store.
|
|
49
|
+
*
|
|
50
|
+
* @param bot - the receiving adapter.
|
|
51
|
+
* @param change - one change of one entry.
|
|
52
|
+
* @returns the sessions to publish, in payload order.
|
|
53
|
+
*/
|
|
54
|
+
export declare function decodeChange(bot: WhatsappBot, change: Change): Session[];
|
|
55
|
+
/**
|
|
56
|
+
* Decode one message of the payload.
|
|
57
|
+
*
|
|
58
|
+
* @param bot - the receiving adapter.
|
|
59
|
+
* @param message - one `messages` entry.
|
|
60
|
+
* @param contacts - the `contacts` array of the same change.
|
|
61
|
+
* @returns the session to publish, or undefined for a message this adapter drops.
|
|
62
|
+
*/
|
|
63
|
+
export declare function decodeMessage(bot: WhatsappBot, message: MessageBody, contacts: Change['value']['contacts']): Session | undefined;
|
|
64
|
+
//# sourceMappingURL=decode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../src/decode.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EACV,OAAO,EAEP,OAAO,EACP,OAAO,EACR,MAAM,oCAAoC,CAAA;AAG3C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AAM3C,OAAO,KAAK,EACV,MAAM,EACN,WAAW,EACX,eAAe,EAIhB,MAAM,YAAY,CAAA;AAEnB,iEAAiE;AACjE,MAAM,WAAW,gBAAgB;IAC/B,oEAAoE;IACpE,IAAI,EAAE,MAAM,CAAA;IACZ,0CAA0C;IAC1C,SAAS,EAAE,MAAM,CAAA;IACjB,2EAA2E;IAC3E,aAAa,EAAE,MAAM,CAAA;IACrB,SAAS,EAAE,MAAM,CAAA;CAClB;AAED,mFAAmF;AACnF,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,OAAO,CAAA;IACb,IAAI,EAAE,OAAO,CAAA;IACb,QAAQ,EAAE,gBAAgB,CAAA;IAC1B,SAAS,EAAE,MAAM,CAAA;CAClB;AAOD,oFAAoF;AACpF,wBAAgB,SAAS,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,CAE3D;AAED;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CACtB,OAAO,EAAE,eAAe,EACxB,QAAQ,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC,UAAU,CAAC,GACpC,OAAO,CAQT;AAED,2EAA2E;AAC3E,wBAAgB,MAAM,CAAC,OAAO,EAAE,eAAe,GAAG,MAAM,CAGvD;AAED,4EAA4E;AAC5E,wBAAgB,SAAS,CACvB,GAAG,EAAE,WAAW,EAChB,OAAO,EAAE,eAAe,EACxB,QAAQ,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC,UAAU,CAAC,GACpC,eAAe,CAYjB;AAuKD;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,EAAE,CAkBxE;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAC3B,GAAG,EAAE,WAAW,EAChB,OAAO,EAAE,WAAW,EACpB,QAAQ,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC,UAAU,CAAC,GACpC,OAAO,GAAG,SAAS,CAoBrB"}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inbound decoding: one change of a webhook delivery becomes zero or more intake `Session`s.
|
|
3
|
+
*
|
|
4
|
+
* The Cloud API pushes everything at once — several entries, each with several changes, each with several messages —
|
|
5
|
+
* and there is no API that reads a message back, so this payload is the only copy of the content. Every message is
|
|
6
|
+
* decoded; a chat is always the sender's own `wa_id`, because this adapter supports no group.
|
|
7
|
+
*/
|
|
8
|
+
import { h, normalize } from '@deepseek-ai/satori-element';
|
|
9
|
+
import { decodeInteractive, decodeReaction, decodeTemplateButton, } from "./interactive.js";
|
|
10
|
+
/** Message of any caught value, for a log line. */
|
|
11
|
+
function messageOf(error) {
|
|
12
|
+
return error instanceof Error ? error.message : String(error);
|
|
13
|
+
}
|
|
14
|
+
/** The direct chat of one sender; a Cloud API conversation has exactly one peer. */
|
|
15
|
+
export function chatRefOf(message) {
|
|
16
|
+
return { id: message.from, type: 'direct' };
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The sender of one message.
|
|
20
|
+
*
|
|
21
|
+
* The payload names the sender's display name in `contacts`, keyed by `wa_id` — a batch can carry several senders, so
|
|
22
|
+
* the entry is matched instead of taken from the first position.
|
|
23
|
+
* @param message - the message being decoded.
|
|
24
|
+
* @param contacts - the `contacts` array of the same change.
|
|
25
|
+
* @returns the sender reference, named when the payload names it.
|
|
26
|
+
*/
|
|
27
|
+
export function senderOf(message, contacts) {
|
|
28
|
+
const name = contacts?.find((contact) => contact.wa_id === message.from)
|
|
29
|
+
?.profile.name;
|
|
30
|
+
return {
|
|
31
|
+
id: message.from,
|
|
32
|
+
...(name === undefined ? {} : { name }),
|
|
33
|
+
isBot: false,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/** The platform timestamp of a message: Meta sends seconds as a string. */
|
|
37
|
+
export function timeOf(message) {
|
|
38
|
+
const seconds = Number.parseInt(message.timestamp, 10);
|
|
39
|
+
return Number.isFinite(seconds) ? seconds * 1_000 : Date.now();
|
|
40
|
+
}
|
|
41
|
+
/** The adapter context an interaction or reaction session is built from. */
|
|
42
|
+
export function contextOf(bot, message, contacts) {
|
|
43
|
+
return {
|
|
44
|
+
chat: chatRefOf(message),
|
|
45
|
+
user: senderOf(message, contacts),
|
|
46
|
+
referrer: {
|
|
47
|
+
waId: message.from,
|
|
48
|
+
messageId: message.id,
|
|
49
|
+
phoneNumberId: bot.config.phoneNumberId,
|
|
50
|
+
timestamp: timeOf(message),
|
|
51
|
+
},
|
|
52
|
+
timestamp: timeOf(message),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/** The `satori:` address of one received media object. */
|
|
56
|
+
function mediaSource(bot, media) {
|
|
57
|
+
return bot.getInternalUrl(`/assets/${encodeURIComponent(media.id)}`);
|
|
58
|
+
}
|
|
59
|
+
/** The caption that precedes a media element, when the sender typed one. */
|
|
60
|
+
function captionOf(media) {
|
|
61
|
+
return media.caption === undefined || media.caption === ''
|
|
62
|
+
? []
|
|
63
|
+
: [media.caption];
|
|
64
|
+
}
|
|
65
|
+
/** The elements of one media message: its caption, then the media itself. */
|
|
66
|
+
function mediaNodes(bot, message) {
|
|
67
|
+
const media = message[message.type];
|
|
68
|
+
if (media === undefined)
|
|
69
|
+
return [];
|
|
70
|
+
const src = mediaSource(bot, media);
|
|
71
|
+
const name = media.filename === undefined ? {} : { name: media.filename };
|
|
72
|
+
switch (message.type) {
|
|
73
|
+
case 'image':
|
|
74
|
+
return [...captionOf(media), h('img', { src })];
|
|
75
|
+
case 'audio':
|
|
76
|
+
// A voice note is an audio message recorded in the app; the vocabulary has no separate element for it.
|
|
77
|
+
return [...captionOf(media), h('audio', { src, ...name })];
|
|
78
|
+
case 'video':
|
|
79
|
+
return [...captionOf(media), h('video', { src, ...name })];
|
|
80
|
+
case 'document':
|
|
81
|
+
return [...captionOf(media), h('file', { src, ...name })];
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* The elements of one message of a type that carries content.
|
|
86
|
+
*
|
|
87
|
+
* @param bot - the receiving adapter.
|
|
88
|
+
* @param message - the decoded message.
|
|
89
|
+
* @returns its elements, possibly empty for a shape this snapshot does not carry.
|
|
90
|
+
*/
|
|
91
|
+
function contentNodes(bot, message) {
|
|
92
|
+
switch (message.type) {
|
|
93
|
+
case 'text':
|
|
94
|
+
return message.text.body === '' ? [] : [message.text.body];
|
|
95
|
+
case 'image':
|
|
96
|
+
case 'audio':
|
|
97
|
+
case 'video':
|
|
98
|
+
case 'document':
|
|
99
|
+
return mediaNodes(bot, message);
|
|
100
|
+
case 'sticker':
|
|
101
|
+
return message.sticker === undefined
|
|
102
|
+
? []
|
|
103
|
+
: [h('img', { src: mediaSource(bot, message.sticker), alt: '[贴纸]' })];
|
|
104
|
+
case 'location':
|
|
105
|
+
// The vocabulary has no location element; the coordinates are what a reader needs from one.
|
|
106
|
+
return [`📍 ${message.location.latitude}, ${message.location.longitude}`];
|
|
107
|
+
case 'contacts': {
|
|
108
|
+
const names = message.contacts.map((contact) => contact.name.formatted_name);
|
|
109
|
+
const summary = names.length === 0 ? '联系人' : `联系人:${names.join('、')}`;
|
|
110
|
+
return [
|
|
111
|
+
summary,
|
|
112
|
+
h('raw', {
|
|
113
|
+
platform: 'whatsapp',
|
|
114
|
+
type: 'contacts',
|
|
115
|
+
data: message.contacts,
|
|
116
|
+
}),
|
|
117
|
+
];
|
|
118
|
+
}
|
|
119
|
+
case 'system':
|
|
120
|
+
return message.system.body === '' ? [] : [message.system.body];
|
|
121
|
+
default:
|
|
122
|
+
return [];
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The content tree of one message, never empty.
|
|
127
|
+
* @param bot - the receiving adapter.
|
|
128
|
+
* @param message - the decoded message.
|
|
129
|
+
* @returns the normalized `<message>` root.
|
|
130
|
+
*/
|
|
131
|
+
function contentOf(bot, message) {
|
|
132
|
+
const nodes = contentNodes(bot, message);
|
|
133
|
+
if (nodes.length > 0)
|
|
134
|
+
return normalize(h('message', nodes));
|
|
135
|
+
// A type this snapshot does not model still reaches the model as its payload rather than disappearing.
|
|
136
|
+
return normalize(h('message', h('raw', { platform: 'whatsapp', type: message.type, data: message })));
|
|
137
|
+
}
|
|
138
|
+
/** The quote an inbound message answers, when the payload names one. */
|
|
139
|
+
function quoteOf(message) {
|
|
140
|
+
if (message.context === undefined)
|
|
141
|
+
return undefined;
|
|
142
|
+
return { id: message.context.id, sender: { id: message.context.from } };
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Decode one ordinary message into a `message` session.
|
|
146
|
+
*
|
|
147
|
+
* @param bot - the receiving adapter.
|
|
148
|
+
* @param message - the decoded message.
|
|
149
|
+
* @param contacts - the `contacts` array of the same change.
|
|
150
|
+
* @returns the session to publish.
|
|
151
|
+
*/
|
|
152
|
+
function messageSession(bot, message, contacts) {
|
|
153
|
+
const context = contextOf(bot, message, contacts);
|
|
154
|
+
const quote = quoteOf(message);
|
|
155
|
+
const inbound = {
|
|
156
|
+
id: message.id,
|
|
157
|
+
chat: context.chat,
|
|
158
|
+
sender: context.user,
|
|
159
|
+
content: contentOf(bot, message),
|
|
160
|
+
...(quote === undefined ? {} : { quote }),
|
|
161
|
+
// A 1:1 WhatsApp conversation has no mention syntax and the platform names no mentioned user.
|
|
162
|
+
mentions: { self: false, all: false, users: [] },
|
|
163
|
+
time: context.timestamp,
|
|
164
|
+
referrer: context.referrer,
|
|
165
|
+
raw: message,
|
|
166
|
+
};
|
|
167
|
+
return bot.session({
|
|
168
|
+
type: 'message',
|
|
169
|
+
id: message.id,
|
|
170
|
+
timestamp: context.timestamp,
|
|
171
|
+
message: inbound,
|
|
172
|
+
raw: message,
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
/** Log one delivery receipt; the platform's own view of an outbound message has no DSH event. */
|
|
176
|
+
function logStatus(bot, status) {
|
|
177
|
+
if (status.status !== 'failed') {
|
|
178
|
+
bot.ctx.logger.debug('whatsapp: message %s is %s', status.id, status.status);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
// A failed delivery is where a closed 24-hour window, a rejected template and a refused recipient first appear.
|
|
182
|
+
const errors = status.errors ?? [];
|
|
183
|
+
bot.ctx.logger.warn('whatsapp: delivery of %s to %s failed: %s', status.id, status.recipient_id, errors.length === 0
|
|
184
|
+
? 'no error detail'
|
|
185
|
+
: errors
|
|
186
|
+
.map((error) => [String(error.code), error.title, error.error_data?.details]
|
|
187
|
+
.filter((part) => part !== undefined && part !== '')
|
|
188
|
+
.join(' '))
|
|
189
|
+
.join('; '));
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Decode every message of one change.
|
|
193
|
+
*
|
|
194
|
+
* A non-`messages` change family is dropped with a debug line: the IM host has no consumer for account, template or
|
|
195
|
+
* quality updates. Delivery receipts never reach the message store.
|
|
196
|
+
*
|
|
197
|
+
* @param bot - the receiving adapter.
|
|
198
|
+
* @param change - one change of one entry.
|
|
199
|
+
* @returns the sessions to publish, in payload order.
|
|
200
|
+
*/
|
|
201
|
+
export function decodeChange(bot, change) {
|
|
202
|
+
if (change.field !== 'messages') {
|
|
203
|
+
bot.ctx.logger.debug('whatsapp: ignoring a %s change', change.field);
|
|
204
|
+
return [];
|
|
205
|
+
}
|
|
206
|
+
for (const status of change.value.statuses ?? [])
|
|
207
|
+
logStatus(bot, status);
|
|
208
|
+
for (const error of change.value.errors ?? []) {
|
|
209
|
+
bot.ctx.logger.warn('whatsapp: the webhook reported an error: %s', messageOf(error));
|
|
210
|
+
}
|
|
211
|
+
const sessions = [];
|
|
212
|
+
for (const message of change.value.messages ?? []) {
|
|
213
|
+
const session = decodeMessage(bot, message, change.value.contacts);
|
|
214
|
+
if (session !== undefined)
|
|
215
|
+
sessions.push(session);
|
|
216
|
+
}
|
|
217
|
+
return sessions;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Decode one message of the payload.
|
|
221
|
+
*
|
|
222
|
+
* @param bot - the receiving adapter.
|
|
223
|
+
* @param message - one `messages` entry.
|
|
224
|
+
* @param contacts - the `contacts` array of the same change.
|
|
225
|
+
* @returns the session to publish, or undefined for a message this adapter drops.
|
|
226
|
+
*/
|
|
227
|
+
export function decodeMessage(bot, message, contacts) {
|
|
228
|
+
if (message.group_id !== undefined) {
|
|
229
|
+
// A group message names the member who wrote it, not the conversation; treating it as a direct chat would make
|
|
230
|
+
// the bot answer that member privately. v1 supports no group, so the message is dropped without a trace.
|
|
231
|
+
return undefined;
|
|
232
|
+
}
|
|
233
|
+
switch (message.type) {
|
|
234
|
+
case 'reaction':
|
|
235
|
+
return decodeReaction(bot, message, contextOf(bot, message, contacts));
|
|
236
|
+
case 'interactive':
|
|
237
|
+
return decodeInteractive(bot, message, contextOf(bot, message, contacts));
|
|
238
|
+
case 'button':
|
|
239
|
+
return decodeTemplateButton(bot, message, contextOf(bot, message, contacts));
|
|
240
|
+
default:
|
|
241
|
+
return messageSession(bot, message, contacts);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
//# sourceMappingURL=decode.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decode.js","sourceRoot":"","sources":["../../src/decode.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAQH,OAAO,EAAE,CAAC,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAA;AAG1D,OAAO,EACL,iBAAiB,EACjB,cAAc,EACd,oBAAoB,GACrB,MAAM,kBAAkB,CAAA;AA6BzB,mDAAmD;AACnD,SAAS,SAAS,CAAC,KAAc;IAC/B,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AAC/D,CAAC;AAED,oFAAoF;AACpF,MAAM,UAAU,SAAS,CAAC,OAAwB;IAChD,OAAO,EAAE,EAAE,EAAE,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAA;AAC7C,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,QAAQ,CACtB,OAAwB,EACxB,QAAqC;IAErC,MAAM,IAAI,GAAG,QAAQ,EAAE,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,KAAK,OAAO,CAAC,IAAI,CAAC;QACtE,EAAE,OAAO,CAAC,IAAI,CAAA;IAChB,OAAO;QACL,EAAE,EAAE,OAAO,CAAC,IAAI;QAChB,GAAG,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC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AO,CAAC,IAAI,EAAE,CAAC;QACrB,KAAK,UAAU;YACb,OAAO,cAAc,CAAC,GAAG,EAAE,OAAO,EAAE,SAAS,CAAC,GAAG,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAA;QACxE,KAAK,aAAa;YAChB,OAAO,iBAAiB,CAAC,GAAG,EAAE,OAAO,EAAE,SAAS,CAAC,GAAG,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAA;QAC3E,KAAK,QAAQ;YACX,OAAO,oBAAoB,CACzB,GAAG,EACH,OAAO,EACP,SAAS,CAAC,GAAG,EAAE,OAAO,EAAE,QAAQ,CAAC,CAClC,CAAA;QACH;YACE,OAAO,cAAc,CAAC,GAAG,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAA;IACjD,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The failure shape of one Graph call and its mapping onto the core platform taxonomy.
|
|
3
|
+
*
|
|
4
|
+
* Meta rejects a message in one atomic request — authentication, the recipient, the 24-hour window, template
|
|
5
|
+
* parameters and media ids are all checked before anything is delivered — so an answer carrying an error code means
|
|
6
|
+
* nothing was published. Only a failure whose answer itself is inconclusive leaves the outcome unknown.
|
|
7
|
+
*/
|
|
8
|
+
import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core';
|
|
9
|
+
/** Fields a failed Graph call carries for classification and logs. */
|
|
10
|
+
export interface WhatsappApiErrorInit {
|
|
11
|
+
/** HTTP status, absent when the request never got an answer. */
|
|
12
|
+
status?: number;
|
|
13
|
+
/** Graph error `code` (`190`, `131047`, `130429`, …). */
|
|
14
|
+
code?: number;
|
|
15
|
+
/** Graph error `error_subcode`. */
|
|
16
|
+
subcode?: number;
|
|
17
|
+
/** Graph error `message`. */
|
|
18
|
+
platformMessage?: string;
|
|
19
|
+
/** Graph error `type` (`OAuthException`, `GraphMethodException`, …). */
|
|
20
|
+
type?: string;
|
|
21
|
+
/** Graph error `error_data.details`, which names the offending field or the window state. */
|
|
22
|
+
details?: string;
|
|
23
|
+
/** Start of the answer body, for a refusal that carried no JSON. */
|
|
24
|
+
bodyText?: string;
|
|
25
|
+
/** The answer body was not JSON, so the status alone does not explain the outcome. */
|
|
26
|
+
nonJson?: boolean;
|
|
27
|
+
cause?: unknown;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* One failed Graph call.
|
|
31
|
+
*
|
|
32
|
+
* The Graph API answers a refusal with `{ error: { message, type, code, error_subcode?, fbtrace_id,
|
|
33
|
+
* error_data? } }`, or with a page no proxy should produce.
|
|
34
|
+
*/
|
|
35
|
+
export declare class WhatsappApiError extends Error {
|
|
36
|
+
readonly status: number | undefined;
|
|
37
|
+
readonly code: number | undefined;
|
|
38
|
+
readonly subcode: number | undefined;
|
|
39
|
+
readonly platformMessage: string | undefined;
|
|
40
|
+
readonly type: string | undefined;
|
|
41
|
+
readonly details: string | undefined;
|
|
42
|
+
readonly bodyText: string | undefined;
|
|
43
|
+
readonly nonJson: boolean;
|
|
44
|
+
constructor(message: string, init?: WhatsappApiErrorInit);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Map any failure onto the core platform taxonomy.
|
|
48
|
+
*
|
|
49
|
+
* @param error - the caught failure.
|
|
50
|
+
* @returns the classified platform error.
|
|
51
|
+
*/
|
|
52
|
+
export declare function classifyWhatsappError(error: unknown): PlatformError;
|
|
53
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAA;AAElE,sEAAsE;AACtE,MAAM,WAAW,oBAAoB;IACnC,gEAAgE;IAChE,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,yDAAyD;IACzD,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,mCAAmC;IACnC,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,6BAA6B;IAC7B,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB,wEAAwE;IACxE,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,6FAA6F;IAC7F,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,oEAAoE;IACpE,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,sFAAsF;IACtF,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB;AAED;;;;;GAKG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;IACzC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAA;IACnC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAA;IACjC,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAA;IACpC,QAAQ,CAAC,eAAe,EAAE,MAAM,GAAG,SAAS,CAAA;IAC5C,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAA;IACjC,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAA;IACpC,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAA;IACrC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;gBAEb,OAAO,EAAE,MAAM,EAAE,IAAI,GAAE,oBAAyB;CAY7D;AAmED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,CAwDnE"}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The failure shape of one Graph call and its mapping onto the core platform taxonomy.
|
|
3
|
+
*
|
|
4
|
+
* Meta rejects a message in one atomic request — authentication, the recipient, the 24-hour window, template
|
|
5
|
+
* parameters and media ids are all checked before anything is delivered — so an answer carrying an error code means
|
|
6
|
+
* nothing was published. Only a failure whose answer itself is inconclusive leaves the outcome unknown.
|
|
7
|
+
*/
|
|
8
|
+
import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core';
|
|
9
|
+
/**
|
|
10
|
+
* One failed Graph call.
|
|
11
|
+
*
|
|
12
|
+
* The Graph API answers a refusal with `{ error: { message, type, code, error_subcode?, fbtrace_id,
|
|
13
|
+
* error_data? } }`, or with a page no proxy should produce.
|
|
14
|
+
*/
|
|
15
|
+
export class WhatsappApiError extends Error {
|
|
16
|
+
status;
|
|
17
|
+
code;
|
|
18
|
+
subcode;
|
|
19
|
+
platformMessage;
|
|
20
|
+
type;
|
|
21
|
+
details;
|
|
22
|
+
bodyText;
|
|
23
|
+
nonJson;
|
|
24
|
+
constructor(message, init = {}) {
|
|
25
|
+
super(message, init.cause === undefined ? undefined : { cause: init.cause });
|
|
26
|
+
this.name = 'WhatsappApiError';
|
|
27
|
+
this.status = init.status;
|
|
28
|
+
this.code = init.code;
|
|
29
|
+
this.subcode = init.subcode;
|
|
30
|
+
this.platformMessage = init.platformMessage;
|
|
31
|
+
this.type = init.type;
|
|
32
|
+
this.details = init.details;
|
|
33
|
+
this.bodyText = init.bodyText;
|
|
34
|
+
this.nonJson = init.nonJson ?? false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The Graph codes that override the status-based default (§7.2).
|
|
39
|
+
*
|
|
40
|
+
* The `132xxx` entries are template errors: this adapter sends no template, and the table stays for a caller of
|
|
41
|
+
* `bot.internal.sendMessage` and for a future template mode.
|
|
42
|
+
*/
|
|
43
|
+
const CODE_RULES = new Map([
|
|
44
|
+
// The bearer token is invalid, expired or revoked.
|
|
45
|
+
[190, { kind: 'permission', published: 'no' }],
|
|
46
|
+
// Invalid or missing request parameters.
|
|
47
|
+
[100, { kind: 'invalid', published: 'no' }],
|
|
48
|
+
[131008, { kind: 'invalid', published: 'no' }],
|
|
49
|
+
[131009, { kind: 'invalid', published: 'no' }],
|
|
50
|
+
[131021, { kind: 'invalid', published: 'no' }],
|
|
51
|
+
// The recipient cannot receive a message.
|
|
52
|
+
[131026, { kind: 'not-found', published: 'no' }],
|
|
53
|
+
// The 24-hour customer service window is closed; the message is refused for re-engagement.
|
|
54
|
+
[131047, { kind: 'invalid', published: 'no' }],
|
|
55
|
+
// Throughput and pair rate limits.
|
|
56
|
+
[4, { kind: 'rate-limit', published: 'no' }],
|
|
57
|
+
[80007, { kind: 'rate-limit', published: 'no' }],
|
|
58
|
+
[130429, { kind: 'rate-limit', published: 'no' }],
|
|
59
|
+
[131048, { kind: 'rate-limit', published: 'no' }],
|
|
60
|
+
[131056, { kind: 'rate-limit', published: 'no' }],
|
|
61
|
+
// The business account is restricted, locked or unqualified.
|
|
62
|
+
[368, { kind: 'permission', published: 'no' }],
|
|
63
|
+
[131031, { kind: 'permission', published: 'no' }],
|
|
64
|
+
[131042, { kind: 'permission', published: 'no' }],
|
|
65
|
+
[133004, { kind: 'permission', published: 'no' }],
|
|
66
|
+
[133005, { kind: 'permission', published: 'no' }],
|
|
67
|
+
[133006, { kind: 'permission', published: 'no' }],
|
|
68
|
+
[133008, { kind: 'permission', published: 'no' }],
|
|
69
|
+
[133010, { kind: 'permission', published: 'no' }],
|
|
70
|
+
// The platform itself reports an unknown failure: it may have published the message.
|
|
71
|
+
[131000, { kind: 'retryable', published: 'unknown' }],
|
|
72
|
+
[131016, { kind: 'retryable', published: 'unknown' }],
|
|
73
|
+
// Template parameters, template state.
|
|
74
|
+
[132000, { kind: 'invalid', published: 'no' }],
|
|
75
|
+
[132001, { kind: 'invalid', published: 'no' }],
|
|
76
|
+
[132005, { kind: 'invalid', published: 'no' }],
|
|
77
|
+
[132007, { kind: 'invalid', published: 'no' }],
|
|
78
|
+
[132012, { kind: 'invalid', published: 'no' }],
|
|
79
|
+
[132015, { kind: 'invalid', published: 'no' }],
|
|
80
|
+
[132016, { kind: 'invalid', published: 'no' }],
|
|
81
|
+
]);
|
|
82
|
+
/** The message of a re-engagement refusal names the window and the only way out of it. */
|
|
83
|
+
const CLOSED_WINDOW = 'whatsapp: the 24-hour customer service window is closed (131047): a free-form message ' +
|
|
84
|
+
'cannot be sent until the recipient writes again, and only a template message could reach them before that';
|
|
85
|
+
/** Whether a caught value is an abort error, including the `DOMException` a signal produces. */
|
|
86
|
+
function isAbortError(error) {
|
|
87
|
+
return ((error instanceof Error || error instanceof DOMException) &&
|
|
88
|
+
error.name === 'AbortError');
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Map any failure onto the core platform taxonomy.
|
|
92
|
+
*
|
|
93
|
+
* @param error - the caught failure.
|
|
94
|
+
* @returns the classified platform error.
|
|
95
|
+
*/
|
|
96
|
+
export function classifyWhatsappError(error) {
|
|
97
|
+
if (error instanceof PlatformError)
|
|
98
|
+
return error;
|
|
99
|
+
if (error instanceof WhatsappApiError) {
|
|
100
|
+
// The Graph `code` is the platform's own identity of the failure; the HTTP status is only the transport's view.
|
|
101
|
+
const code = error.code ?? error.status;
|
|
102
|
+
const init = {
|
|
103
|
+
...(code === undefined ? {} : { code }),
|
|
104
|
+
cause: error,
|
|
105
|
+
};
|
|
106
|
+
// No answer at all, or an answer that is not the Graph JSON envelope (a proxy page, a cut-off reply): the status
|
|
107
|
+
// alone cannot say whether the request was rejected or half-processed.
|
|
108
|
+
if (error.status === undefined || error.nonJson) {
|
|
109
|
+
return new PlatformError('retryable', error.message, {
|
|
110
|
+
...init,
|
|
111
|
+
published: 'unknown',
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
if (error.code === 131047) {
|
|
115
|
+
return new PlatformError('invalid', CLOSED_WINDOW, {
|
|
116
|
+
code: error.code,
|
|
117
|
+
published: 'no',
|
|
118
|
+
cause: error,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
const rule = error.code === undefined ? undefined : CODE_RULES.get(error.code);
|
|
122
|
+
if (rule !== undefined) {
|
|
123
|
+
return new PlatformError(rule.kind, error.message, {
|
|
124
|
+
...init,
|
|
125
|
+
published: rule.published,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
if (error.status >= 500) {
|
|
129
|
+
return new PlatformError('retryable', error.message, {
|
|
130
|
+
...init,
|
|
131
|
+
published: 'unknown',
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
// Every other answered 4xx: the request was refused before delivery, so nothing was published.
|
|
135
|
+
return new PlatformError('invalid', error.message, {
|
|
136
|
+
...init,
|
|
137
|
+
published: 'no',
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
if (isAbortError(error)) {
|
|
141
|
+
return new PlatformError('fatal', error.message, {
|
|
142
|
+
published: 'unknown',
|
|
143
|
+
cause: error,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
147
|
+
return new PlatformError('retryable', `whatsapp: request failed: ${message}`, { published: 'unknown', cause: error });
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAA;AAuBlE;;;;;GAKG;AACH,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAChC,MAAM,CAAoB;IAC1B,IAAI,CAAoB;IACxB,OAAO,CAAoB;IAC3B,eAAe,CAAoB;IACnC,IAAI,CAAoB;IACxB,OAAO,CAAoB;IAC3B,QAAQ,CAAoB;IAC5B,OAAO,CAAS;IAEzB,YAAY,OAAe,EAAE,OAA6B,EAAE;QAC1D,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC,CAAA;QAC5E,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAA;QAC9B,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,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AAE,CAAC;QACxB,OAAO,IAAI,aAAa,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE;YAC/C,SAAS,EAAE,SAAS;YACpB,KAAK,EAAE,KAAK;SACb,CAAC,CAAA;IACJ,CAAC;IACD,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IACtE,OAAO,IAAI,aAAa,CACtB,WAAW,EACX,6BAA6B,OAAO,EAAE,EACtC,EAAE,SAAS,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,EAAE,CACvC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The phone number a bot speaks for.
|
|
3
|
+
*
|
|
4
|
+
* The Cloud API names a number by its Graph id in every path (`POST /{phoneNumberId}/messages`), while the webhook
|
|
5
|
+
* addresses it by `metadata.phone_number_id`. The adapter configures the id (one row per number) and proves on connect
|
|
6
|
+
* that the configured token reaches that number: a token/row mismatch would otherwise surface only on the first send.
|
|
7
|
+
*/
|
|
8
|
+
import type { User } from '@deepseek-ai/cordis-plugin-im-core';
|
|
9
|
+
import type { WhatsAppRequest } from './request.ts';
|
|
10
|
+
/** The `GET /{phoneNumberId}` answer fields this adapter reads. */
|
|
11
|
+
export interface PhoneNumberInfo {
|
|
12
|
+
id: string;
|
|
13
|
+
display_phone_number: string;
|
|
14
|
+
verified_name?: string;
|
|
15
|
+
}
|
|
16
|
+
/** Failure to confirm the configured phone number. */
|
|
17
|
+
export declare class WhatsappIdentityError extends Error {
|
|
18
|
+
constructor(message: string);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Read the configured phone number.
|
|
22
|
+
*
|
|
23
|
+
* @param request - the Graph client of the configured number.
|
|
24
|
+
* @param phoneNumberId - the configured Graph id.
|
|
25
|
+
* @returns the number's display name and itself.
|
|
26
|
+
* @throws WhatsappApiError when the token cannot read that number.
|
|
27
|
+
*/
|
|
28
|
+
export declare function fetchPhoneNumber(request: WhatsAppRequest, phoneNumberId: string): Promise<PhoneNumberInfo>;
|
|
29
|
+
/**
|
|
30
|
+
* Verify a `GET /{phoneNumberId}` answer against the configured id.
|
|
31
|
+
*
|
|
32
|
+
* @param phoneNumberId - the configured Graph id.
|
|
33
|
+
* @param info - the read-back.
|
|
34
|
+
* @returns the Graph id, which is `phoneNumberId`.
|
|
35
|
+
* @throws WhatsappIdentityError when the answer names another number.
|
|
36
|
+
*/
|
|
37
|
+
export declare function verifyIdentity(phoneNumberId: string, info: PhoneNumberInfo): string;
|
|
38
|
+
/**
|
|
39
|
+
* The bot's own platform identity.
|
|
40
|
+
*
|
|
41
|
+
* The display phone number is the only human-readable name the Cloud API gives a business number; `wa_id` is not in
|
|
42
|
+
* any payload, and deriving it from the display form would be a guess.
|
|
43
|
+
*
|
|
44
|
+
* @param info - the verified read-back.
|
|
45
|
+
* @returns the user of `bot.user`.
|
|
46
|
+
*/
|
|
47
|
+
export declare function userOf(info: PhoneNumberInfo): User;
|
|
48
|
+
//# sourceMappingURL=identity.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../../src/identity.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,oCAAoC,CAAA;AAC9D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAEnD,mEAAmE;AACnE,MAAM,WAAW,eAAe;IAC9B,EAAE,EAAE,MAAM,CAAA;IACV,oBAAoB,EAAE,MAAM,CAAA;IAC5B,aAAa,CAAC,EAAE,MAAM,CAAA;CACvB;AAED,sDAAsD;AACtD,qBAAa,qBAAsB,SAAQ,KAAK;gBAClC,OAAO,EAAE,MAAM;CAI5B;AAED;;;;;;;GAOG;AACH,wBAAsB,gBAAgB,CACpC,OAAO,EAAE,eAAe,EACxB,aAAa,EAAE,MAAM,GACpB,OAAO,CAAC,eAAe,CAAC,CAK1B;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAC5B,aAAa,EAAE,MAAM,EACrB,IAAI,EAAE,eAAe,GACpB,MAAM,CAOR;AAED;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,eAAe,GAAG,IAAI,CAElD"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The phone number a bot speaks for.
|
|
3
|
+
*
|
|
4
|
+
* The Cloud API names a number by its Graph id in every path (`POST /{phoneNumberId}/messages`), while the webhook
|
|
5
|
+
* addresses it by `metadata.phone_number_id`. The adapter configures the id (one row per number) and proves on connect
|
|
6
|
+
* that the configured token reaches that number: a token/row mismatch would otherwise surface only on the first send.
|
|
7
|
+
*/
|
|
8
|
+
/** Failure to confirm the configured phone number. */
|
|
9
|
+
export class WhatsappIdentityError extends Error {
|
|
10
|
+
constructor(message) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = 'WhatsappIdentityError';
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Read the configured phone number.
|
|
17
|
+
*
|
|
18
|
+
* @param request - the Graph client of the configured number.
|
|
19
|
+
* @param phoneNumberId - the configured Graph id.
|
|
20
|
+
* @returns the number's display name and itself.
|
|
21
|
+
* @throws WhatsappApiError when the token cannot read that number.
|
|
22
|
+
*/
|
|
23
|
+
export async function fetchPhoneNumber(request, phoneNumberId) {
|
|
24
|
+
return await request.get(`/${encodeURIComponent(phoneNumberId)}`, { fields: 'display_phone_number,verified_name' });
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Verify a `GET /{phoneNumberId}` answer against the configured id.
|
|
28
|
+
*
|
|
29
|
+
* @param phoneNumberId - the configured Graph id.
|
|
30
|
+
* @param info - the read-back.
|
|
31
|
+
* @returns the Graph id, which is `phoneNumberId`.
|
|
32
|
+
* @throws WhatsappIdentityError when the answer names another number.
|
|
33
|
+
*/
|
|
34
|
+
export function verifyIdentity(phoneNumberId, info) {
|
|
35
|
+
if (info.id !== phoneNumberId) {
|
|
36
|
+
throw new WhatsappIdentityError(`whatsapp: phoneNumberId is ${phoneNumberId} but GET /${phoneNumberId} returned ${info.id}`);
|
|
37
|
+
}
|
|
38
|
+
return info.id;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The bot's own platform identity.
|
|
42
|
+
*
|
|
43
|
+
* The display phone number is the only human-readable name the Cloud API gives a business number; `wa_id` is not in
|
|
44
|
+
* any payload, and deriving it from the display form would be a guess.
|
|
45
|
+
*
|
|
46
|
+
* @param info - the verified read-back.
|
|
47
|
+
* @returns the user of `bot.user`.
|
|
48
|
+
*/
|
|
49
|
+
export function userOf(info) {
|
|
50
|
+
return { id: info.id, name: info.display_phone_number, isBot: true };
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=identity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identity.js","sourceRoot":"","sources":["../../src/identity.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAYH,sDAAsD;AACtD,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IAC9C,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAA;IACrC,CAAC;CACF;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,OAAwB,EACxB,aAAqB;IAErB,OAAO,MAAM,OAAO,CAAC,GAAG,CACtB,IAAI,kBAAkB,CAAC,aAAa,CAAC,EAAE,EACvC,EAAE,MAAM,EAAE,oCAAoC,EAAE,CACjD,CAAA;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAC5B,aAAqB,EACrB,IAAqB;IAErB,IAAI,IAAI,CAAC,EAAE,KAAK,aAAa,EAAE,CAAC;QAC9B,MAAM,IAAI,qBAAqB,CAC7B,8BAA8B,aAAa,aAAa,aAAa,aAAa,IAAI,CAAC,EAAE,EAAE,CAC5F,CAAA;IACH,CAAC;IACD,OAAO,IAAI,CAAC,EAAE,CAAA;AAChB,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,MAAM,CAAC,IAAqB;IAC1C,OAAO,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,oBAAoB,EAAE,KAAK,EAAE,IAAI,EAAE,CAAA;AACtE,CAAC"}
|