core-services-sdk 1.3.48 → 1.3.50
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/package.json +1 -1
- package/src/instant-messages/index.js +2 -0
- package/src/instant-messages/message-types.js +26 -32
- package/src/instant-messages/telegram-apis/telegram-apis.js +301 -0
- package/src/instant-messages/whatsapp-apis/whatsapp-apis.js +401 -0
- package/tests/instant-messages/message-unified-mapper-telegram.unit.test.js +0 -1
- package/tests/instant-messages/telegram-apis.unit.test.js +157 -0
- package/tests/instant-messages/unified-message-type.unit.test.js +78 -0
- package/tests/instant-messages/whatsapp-apis.unit.test.js +277 -0
- package/types/instant-messages/index.d.ts +2 -0
- package/types/instant-messages/message-types.d.ts +74 -26
- package/types/instant-messages/telegram-apis/telegram-apis.d.ts +105 -0
- package/types/instant-messages/whatsapp-apis/whatsapp-apis.d.ts +73 -0
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
import { post } from '../../http/http.js'
|
|
2
|
+
|
|
3
|
+
const WHATSAPP_API_BASE_URL = 'https://graph.facebook.com'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Retrieves metadata for a WhatsApp media object.
|
|
7
|
+
*
|
|
8
|
+
* WhatsApp Cloud API does not provide the media file directly via mediaId.
|
|
9
|
+
* Instead, you must first request the metadata for the media item, which
|
|
10
|
+
* includes a temporary download URL. This URL can then be used to retrieve
|
|
11
|
+
* the actual binary content of the file.
|
|
12
|
+
*
|
|
13
|
+
* The returned metadata object typically includes:
|
|
14
|
+
* - `url` A temporary URL that allows downloading the media file
|
|
15
|
+
* - `mime_type` The detected MIME type of the media
|
|
16
|
+
* - `id` The mediaId itself
|
|
17
|
+
*
|
|
18
|
+
* Example output from WhatsApp:
|
|
19
|
+
* {
|
|
20
|
+
* "url": "https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=...",
|
|
21
|
+
* "mime_type": "image/jpeg",
|
|
22
|
+
* "id": "MEDIA_ID"
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* Note:
|
|
26
|
+
* The temporary download URL is short-lived and must be accessed quickly.
|
|
27
|
+
*
|
|
28
|
+
* @param {Object} params
|
|
29
|
+
* @param {string} params.mediaId
|
|
30
|
+
* The media identifier received in an incoming WhatsApp webhook message.
|
|
31
|
+
*
|
|
32
|
+
* @param {string} params.token
|
|
33
|
+
* WhatsApp Cloud API access token used for authorization.
|
|
34
|
+
*
|
|
35
|
+
* @param {string} [params.version='v21.0']
|
|
36
|
+
* The WhatsApp Cloud API Graph version to use.
|
|
37
|
+
*
|
|
38
|
+
* @returns {Promise<Object>}
|
|
39
|
+
* Resolves with the media metadata object containing `url`, `mime_type`, and `id`.
|
|
40
|
+
*
|
|
41
|
+
* @throws {Error}
|
|
42
|
+
* If the metadata request fails or WhatsApp responds with a non successful status code.
|
|
43
|
+
*/
|
|
44
|
+
export const getWhatsAppMediaInfo = async ({
|
|
45
|
+
token,
|
|
46
|
+
mediaId,
|
|
47
|
+
version = 'v21.0',
|
|
48
|
+
baseUrl = WHATSAPP_API_BASE_URL,
|
|
49
|
+
}) => {
|
|
50
|
+
const url = `${baseUrl}/${version}/${mediaId}`
|
|
51
|
+
|
|
52
|
+
const res = await fetch(url, {
|
|
53
|
+
headers: {
|
|
54
|
+
Authorization: `Bearer ${token}`,
|
|
55
|
+
},
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
if (!res.ok) {
|
|
59
|
+
throw new Error(`Failed to retrieve media info: ${res.status}`)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return res.json()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Downloads WhatsApp media and returns either a Buffer or a Stream,
|
|
67
|
+
* depending on mode.
|
|
68
|
+
*
|
|
69
|
+
* @param {Object} params
|
|
70
|
+
* @param {string} params.mediaId
|
|
71
|
+
* @param {string} params.token
|
|
72
|
+
* @param {'buffer' | 'stream'} params.mode
|
|
73
|
+
* @returns {Promise<Buffer|ReadableStream>}
|
|
74
|
+
*/
|
|
75
|
+
export const downloadWhatsAppMedia = async ({
|
|
76
|
+
token,
|
|
77
|
+
mediaId,
|
|
78
|
+
mode = 'buffer',
|
|
79
|
+
}) => {
|
|
80
|
+
const info = await getWhatsAppMediaInfo({ mediaId, token })
|
|
81
|
+
const { url: downloadUrl } = info
|
|
82
|
+
|
|
83
|
+
const res = await fetch(downloadUrl, {
|
|
84
|
+
headers: {
|
|
85
|
+
Authorization: `Bearer ${token}`,
|
|
86
|
+
},
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
if (!res.ok) {
|
|
90
|
+
throw new Error(`Failed to download media: ${res.status}`)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (mode === 'stream') {
|
|
94
|
+
return res.body
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const buffer = Buffer.from(await res.arrayBuffer())
|
|
98
|
+
return buffer
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Builds the WhatsApp Cloud API messages endpoint URL.
|
|
103
|
+
*
|
|
104
|
+
* This function generates the full URL required to send messages
|
|
105
|
+
* through the WhatsApp Cloud API. It concatenates the base Graph API URL,
|
|
106
|
+
* the selected API version, and the phone number ID to produce:
|
|
107
|
+
*
|
|
108
|
+
* https://graph.facebook.com/{version}/{phoneNumberId}/messages
|
|
109
|
+
*
|
|
110
|
+
* The returned URL can then be used for POST requests to send text,
|
|
111
|
+
* media, interactive messages, and more.
|
|
112
|
+
*
|
|
113
|
+
* @param {Object} params
|
|
114
|
+
* @param {string} params.phoneNumberId
|
|
115
|
+
* The WhatsApp phone number ID associated with the business account.
|
|
116
|
+
*
|
|
117
|
+
* @param {string} [params.version='v21.0']
|
|
118
|
+
* The WhatsApp Cloud API version to use. Defaults to the current stable.
|
|
119
|
+
*
|
|
120
|
+
* @param {string} [params.baseUrl=WHATSAPP_API_BASE_URL]
|
|
121
|
+
* Optional override for the Graph API base URL (useful for testing or proxying).
|
|
122
|
+
*
|
|
123
|
+
* @returns {string}
|
|
124
|
+
* Fully resolved WhatsApp Cloud API endpoint URL for sending messages.
|
|
125
|
+
*/
|
|
126
|
+
export const getWhatsAppApiUrls = ({
|
|
127
|
+
phoneNumberId,
|
|
128
|
+
version = 'v21.0',
|
|
129
|
+
baseUrl = WHATSAPP_API_BASE_URL,
|
|
130
|
+
}) => `${baseUrl}/${version}/${phoneNumberId}/messages`
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Factory that creates WhatsApp Cloud API helper methods.
|
|
134
|
+
*
|
|
135
|
+
* This module wraps the WhatsApp Graph API endpoints and exposes
|
|
136
|
+
* high level functions for sending text messages, interactive buttons,
|
|
137
|
+
* images, videos, documents, and audio files.
|
|
138
|
+
*
|
|
139
|
+
* Each returned method builds the correct request format according
|
|
140
|
+
* to the WhatsApp Cloud API specification.
|
|
141
|
+
*
|
|
142
|
+
* @typedef {Object} WhatsAppApis
|
|
143
|
+
*
|
|
144
|
+
* @property {Function} sendMessage
|
|
145
|
+
* Sends a plain text message to an individual WhatsApp user.
|
|
146
|
+
*
|
|
147
|
+
* @property {Function} sendButtonsGroup
|
|
148
|
+
* Sends an interactive message containing buttons.
|
|
149
|
+
*
|
|
150
|
+
* @property {Function} sendPhoto
|
|
151
|
+
* Sends an image message using a public URL.
|
|
152
|
+
*
|
|
153
|
+
* @property {Function} sendVideo
|
|
154
|
+
* Sends a video file using a public URL.
|
|
155
|
+
*
|
|
156
|
+
* @property {Function} sendDocument
|
|
157
|
+
* Sends a document message using a public URL.
|
|
158
|
+
*
|
|
159
|
+
* @property {Function} sendAudio
|
|
160
|
+
* Sends an audio file using a public URL.
|
|
161
|
+
*/
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Creates a WhatsApp API client bound to a specific token, phone number ID,
|
|
165
|
+
* and Graph API version.
|
|
166
|
+
*
|
|
167
|
+
* @param {Object} params
|
|
168
|
+
* @param {string} params.token
|
|
169
|
+
* WhatsApp Cloud API access token.
|
|
170
|
+
*
|
|
171
|
+
* @param {string} params.phoneNumberId
|
|
172
|
+
* The phone number ID from Meta Business Manager used for sending messages.
|
|
173
|
+
*
|
|
174
|
+
* @param {string} [params.version='v21.0']
|
|
175
|
+
* WhatsApp Graph API version to use.
|
|
176
|
+
*
|
|
177
|
+
* @returns {WhatsAppApis}
|
|
178
|
+
* A set of helper functions for interacting with the WhatsApp Cloud API.
|
|
179
|
+
*/
|
|
180
|
+
export const whatsappApis = ({ token, phoneNumberId, version = 'v21.0' }) => {
|
|
181
|
+
const url = getWhatsAppApiUrls({ phoneNumberId, version })
|
|
182
|
+
|
|
183
|
+
const headers = {
|
|
184
|
+
Authorization: `Bearer ${token}`,
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const bodyBase = {
|
|
188
|
+
recipient_type: 'individual',
|
|
189
|
+
messaging_product: 'whatsapp',
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return {
|
|
193
|
+
/**
|
|
194
|
+
* Sends a text message to an individual WhatsApp user.
|
|
195
|
+
*
|
|
196
|
+
* @param {Object} params
|
|
197
|
+
* @param {string} params.chatId
|
|
198
|
+
* The WhatsApp number (in international format) of the recipient.
|
|
199
|
+
*
|
|
200
|
+
* @param {string} params.text
|
|
201
|
+
* The message content to send.
|
|
202
|
+
*
|
|
203
|
+
* @param {boolean} [params.preview_url=true]
|
|
204
|
+
* Whether URL previews should be generated automatically.
|
|
205
|
+
*
|
|
206
|
+
* @returns {Promise<import('bot-services-libs-shared').HttpResponse>}
|
|
207
|
+
* The API response from the WhatsApp Cloud API.
|
|
208
|
+
*/
|
|
209
|
+
async sendMessage({ chatId, text, preview_url = true }) {
|
|
210
|
+
const textMessage = {
|
|
211
|
+
to: chatId,
|
|
212
|
+
type: 'text',
|
|
213
|
+
text: {
|
|
214
|
+
preview_url,
|
|
215
|
+
body: text,
|
|
216
|
+
},
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const res = await post({
|
|
220
|
+
url,
|
|
221
|
+
headers,
|
|
222
|
+
body: {
|
|
223
|
+
...bodyBase,
|
|
224
|
+
...textMessage,
|
|
225
|
+
},
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
return res
|
|
229
|
+
},
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Sends an interactive buttons message to a WhatsApp user.
|
|
233
|
+
*
|
|
234
|
+
* @param {Object} params
|
|
235
|
+
* @param {string} params.chatId
|
|
236
|
+
* The recipient phone number.
|
|
237
|
+
*
|
|
238
|
+
* @param {Object} params.buttonsBody
|
|
239
|
+
* The full interactive object containing button definitions.
|
|
240
|
+
* The caller is expected to pass a structure matching
|
|
241
|
+
* WhatsApp's interactive message schema.
|
|
242
|
+
*
|
|
243
|
+
* @returns {Promise<import('bot-services-libs-shared').HttpResponse>}
|
|
244
|
+
* The API response.
|
|
245
|
+
*/
|
|
246
|
+
async sendButtonsGroup({ chatId, buttonsBody }) {
|
|
247
|
+
const res = await post({
|
|
248
|
+
url,
|
|
249
|
+
headers,
|
|
250
|
+
body: {
|
|
251
|
+
...bodyBase,
|
|
252
|
+
to: chatId,
|
|
253
|
+
type: 'interactive',
|
|
254
|
+
...buttonsBody,
|
|
255
|
+
},
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
return res
|
|
259
|
+
},
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Sends an image message using a public URL.
|
|
263
|
+
*
|
|
264
|
+
* @param {Object} params
|
|
265
|
+
* @param {string} params.chatId
|
|
266
|
+
* The phone number of the recipient.
|
|
267
|
+
*
|
|
268
|
+
* @param {string} params.photo
|
|
269
|
+
* Public URL of the image.
|
|
270
|
+
*
|
|
271
|
+
* @param {string} [params.caption]
|
|
272
|
+
* Optional caption added to the image.
|
|
273
|
+
*
|
|
274
|
+
* @returns {Promise<import('bot-services-libs-shared').HttpResponse>}
|
|
275
|
+
* The API response.
|
|
276
|
+
*/
|
|
277
|
+
async sendPhoto({ caption, photo, chatId }) {
|
|
278
|
+
const res = await post({
|
|
279
|
+
url,
|
|
280
|
+
headers,
|
|
281
|
+
body: {
|
|
282
|
+
...bodyBase,
|
|
283
|
+
to: chatId,
|
|
284
|
+
type: 'image',
|
|
285
|
+
image: {
|
|
286
|
+
link: photo,
|
|
287
|
+
caption,
|
|
288
|
+
},
|
|
289
|
+
},
|
|
290
|
+
})
|
|
291
|
+
|
|
292
|
+
return res
|
|
293
|
+
},
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Sends a video message using a public URL.
|
|
297
|
+
*
|
|
298
|
+
* @param {Object} params
|
|
299
|
+
* @param {string} params.chatId
|
|
300
|
+
* Recipient phone number.
|
|
301
|
+
*
|
|
302
|
+
* @param {string} params.video
|
|
303
|
+
* Public URL to the video file.
|
|
304
|
+
*
|
|
305
|
+
* @param {string} [params.caption]
|
|
306
|
+
* Optional caption added to the video.
|
|
307
|
+
*
|
|
308
|
+
* @param {string} [params.filename]
|
|
309
|
+
* Optional filename displayed to the user.
|
|
310
|
+
*
|
|
311
|
+
* @returns {Promise<import('bot-services-libs-shared').HttpResponse>}
|
|
312
|
+
* The API response.
|
|
313
|
+
*/
|
|
314
|
+
async sendVideo({ caption, video, filename, chatId }) {
|
|
315
|
+
const res = await post({
|
|
316
|
+
url,
|
|
317
|
+
headers,
|
|
318
|
+
body: {
|
|
319
|
+
...bodyBase,
|
|
320
|
+
to: chatId,
|
|
321
|
+
type: 'video',
|
|
322
|
+
video: {
|
|
323
|
+
link: video,
|
|
324
|
+
caption,
|
|
325
|
+
...(filename ? { filename } : null),
|
|
326
|
+
},
|
|
327
|
+
},
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
return res
|
|
331
|
+
},
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Sends a document file using a public URL.
|
|
335
|
+
*
|
|
336
|
+
* @param {Object} params
|
|
337
|
+
* @param {string} params.chatId
|
|
338
|
+
* Recipient WhatsApp number.
|
|
339
|
+
*
|
|
340
|
+
* @param {string} params.document
|
|
341
|
+
* Public URL to the document.
|
|
342
|
+
*
|
|
343
|
+
* @param {string} [params.caption]
|
|
344
|
+
* Optional text caption.
|
|
345
|
+
*
|
|
346
|
+
* @param {string} [params.filename]
|
|
347
|
+
* Optional filename shown to the user.
|
|
348
|
+
*
|
|
349
|
+
* @returns {Promise<import('bot-services-libs-shared').HttpResponse>}
|
|
350
|
+
* The API response.
|
|
351
|
+
*/
|
|
352
|
+
async sendDocument({ caption, document, filename, chatId }) {
|
|
353
|
+
const res = await post({
|
|
354
|
+
url,
|
|
355
|
+
headers,
|
|
356
|
+
body: {
|
|
357
|
+
...bodyBase,
|
|
358
|
+
to: chatId,
|
|
359
|
+
type: 'document',
|
|
360
|
+
document: {
|
|
361
|
+
link: document,
|
|
362
|
+
caption,
|
|
363
|
+
...(filename ? { filename } : null),
|
|
364
|
+
},
|
|
365
|
+
},
|
|
366
|
+
})
|
|
367
|
+
|
|
368
|
+
return res
|
|
369
|
+
},
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Sends an audio message using a public URL.
|
|
373
|
+
*
|
|
374
|
+
* @param {Object} params
|
|
375
|
+
* @param {string} params.chatId
|
|
376
|
+
* The phone number of the recipient.
|
|
377
|
+
*
|
|
378
|
+
* @param {string} params.audio
|
|
379
|
+
* Public URL to the audio file.
|
|
380
|
+
*
|
|
381
|
+
* @returns {Promise<import('bot-services-libs-shared').HttpResponse>}
|
|
382
|
+
* The API response.
|
|
383
|
+
*/
|
|
384
|
+
async sendAudio({ audio, chatId }) {
|
|
385
|
+
const res = await post({
|
|
386
|
+
url,
|
|
387
|
+
headers,
|
|
388
|
+
body: {
|
|
389
|
+
...bodyBase,
|
|
390
|
+
to: chatId,
|
|
391
|
+
type: 'audio',
|
|
392
|
+
audio: {
|
|
393
|
+
link: audio,
|
|
394
|
+
},
|
|
395
|
+
},
|
|
396
|
+
})
|
|
397
|
+
|
|
398
|
+
return res
|
|
399
|
+
},
|
|
400
|
+
}
|
|
401
|
+
}
|
|
@@ -39,7 +39,6 @@ describe('Telegram unified message mapper – all mock samples', () => {
|
|
|
39
39
|
|
|
40
40
|
expect(unifiedMessage).toBeTypeOf('object')
|
|
41
41
|
expect(unifiedType).toBeTypeOf('string')
|
|
42
|
-
|
|
43
42
|
expect(unifiedMessage.type).toBe(
|
|
44
43
|
MESSAGE_MEDIA_TYPE_MAPPER[unifiedType] || unifiedType,
|
|
45
44
|
)
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
3
|
+
import {
|
|
4
|
+
getTelegramApiUrls,
|
|
5
|
+
telegramApis,
|
|
6
|
+
} from '../../src/instant-messages/telegram-apis/telegram-apis.js'
|
|
7
|
+
import * as httpModule from '../../src/http/http.js'
|
|
8
|
+
|
|
9
|
+
describe('getTelegramApiUrls', () => {
|
|
10
|
+
it('builds correct Telegram API URLs', () => {
|
|
11
|
+
const token = 'TEST_TOKEN'
|
|
12
|
+
const urls = getTelegramApiUrls({ token })
|
|
13
|
+
|
|
14
|
+
expect(urls.SEND_MESSAGE).toBe(
|
|
15
|
+
`https://api.telegram.org/bot${token}/sendMessage`,
|
|
16
|
+
)
|
|
17
|
+
expect(urls.FORWARD_MESSAGE).toBe(
|
|
18
|
+
`https://api.telegram.org/bot${token}/forwardMessage`,
|
|
19
|
+
)
|
|
20
|
+
expect(urls.SEND_PHOTO).toBe(
|
|
21
|
+
`https://api.telegram.org/bot${token}/sendPhoto`,
|
|
22
|
+
)
|
|
23
|
+
expect(urls.GET_FILE).toBe(`https://api.telegram.org/bot${token}/getFile`)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('supports custom base URL', () => {
|
|
27
|
+
const token = 'X'
|
|
28
|
+
const baseUrl = 'http://localhost:9999'
|
|
29
|
+
const urls = getTelegramApiUrls({ token, telegramBaseUrl: baseUrl })
|
|
30
|
+
|
|
31
|
+
expect(urls.SEND_MESSAGE).toBe(`${baseUrl}/bot${token}/sendMessage`)
|
|
32
|
+
})
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
describe('telegramApis', () => {
|
|
36
|
+
const token = 'MOCK_TOKEN'
|
|
37
|
+
let postMock
|
|
38
|
+
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
postMock = vi.spyOn(httpModule, 'post').mockResolvedValue({
|
|
41
|
+
ok: true,
|
|
42
|
+
result: 'mock-response',
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('sends text message with correct body', async () => {
|
|
47
|
+
const api = telegramApis({ token })
|
|
48
|
+
await api.sendMessage({ text: 'hello', chatId: 123 })
|
|
49
|
+
|
|
50
|
+
expect(postMock).toHaveBeenCalledWith({
|
|
51
|
+
url: `https://api.telegram.org/bot${token}/sendMessage`,
|
|
52
|
+
body: {
|
|
53
|
+
chat_id: 123,
|
|
54
|
+
text: 'hello',
|
|
55
|
+
entities: undefined,
|
|
56
|
+
},
|
|
57
|
+
})
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('sends inline keyboard buttons', async () => {
|
|
61
|
+
const api = telegramApis({ token })
|
|
62
|
+
const options = [[{ text: 'A', callback_data: '1' }]]
|
|
63
|
+
|
|
64
|
+
await api.sendButtonsGroup({
|
|
65
|
+
text: 'Choose',
|
|
66
|
+
chatId: 10,
|
|
67
|
+
options,
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
expect(postMock).toHaveBeenCalledWith({
|
|
71
|
+
url: `https://api.telegram.org/bot${token}/sendMessage`,
|
|
72
|
+
body: {
|
|
73
|
+
chat_id: 10,
|
|
74
|
+
text: 'Choose',
|
|
75
|
+
reply_markup: {
|
|
76
|
+
inline_keyboard: options,
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
})
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it('sends a photo with caption', async () => {
|
|
83
|
+
const api = telegramApis({ token })
|
|
84
|
+
|
|
85
|
+
await api.sendPhoto({
|
|
86
|
+
chatId: 77,
|
|
87
|
+
photo: 'http://photo.jpg',
|
|
88
|
+
caption: 'hi',
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
expect(postMock).toHaveBeenCalledWith({
|
|
92
|
+
url: `https://api.telegram.org/bot${token}/sendPhoto`,
|
|
93
|
+
body: {
|
|
94
|
+
chat_id: 77,
|
|
95
|
+
caption: 'hi',
|
|
96
|
+
photo: 'http://photo.jpg',
|
|
97
|
+
},
|
|
98
|
+
})
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('sends a video', async () => {
|
|
102
|
+
const api = telegramApis({ token })
|
|
103
|
+
|
|
104
|
+
await api.sendVideo({
|
|
105
|
+
chatId: 55,
|
|
106
|
+
video: 'http://video.mp4',
|
|
107
|
+
caption: 'watch this',
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
expect(postMock).toHaveBeenCalledWith({
|
|
111
|
+
url: `https://api.telegram.org/bot${token}/sendVideo`,
|
|
112
|
+
body: {
|
|
113
|
+
chat_id: 55,
|
|
114
|
+
caption: 'watch this',
|
|
115
|
+
video: 'http://video.mp4',
|
|
116
|
+
},
|
|
117
|
+
})
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
it('sends an audio file', async () => {
|
|
121
|
+
const api = telegramApis({ token })
|
|
122
|
+
|
|
123
|
+
await api.sendAudio({
|
|
124
|
+
chatId: 200,
|
|
125
|
+
audio: 'http://audio.mp3',
|
|
126
|
+
caption: 'listen',
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
expect(postMock).toHaveBeenCalledWith({
|
|
130
|
+
url: `https://api.telegram.org/bot${token}/sendAudio`,
|
|
131
|
+
body: {
|
|
132
|
+
chat_id: 200,
|
|
133
|
+
caption: 'listen',
|
|
134
|
+
audio: 'http://audio.mp3',
|
|
135
|
+
},
|
|
136
|
+
})
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it('sends a document', async () => {
|
|
140
|
+
const api = telegramApis({ token })
|
|
141
|
+
|
|
142
|
+
await api.sendDocument({
|
|
143
|
+
chatId: 999,
|
|
144
|
+
document: 'http://file.pdf',
|
|
145
|
+
caption: 'file',
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
expect(postMock).toHaveBeenCalledWith({
|
|
149
|
+
url: `https://api.telegram.org/bot${token}/sendDocument`,
|
|
150
|
+
body: {
|
|
151
|
+
chat_id: 999,
|
|
152
|
+
caption: 'file',
|
|
153
|
+
document: 'http://file.pdf',
|
|
154
|
+
},
|
|
155
|
+
})
|
|
156
|
+
})
|
|
157
|
+
})
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import {
|
|
3
|
+
MESSAGE_TYPE,
|
|
4
|
+
MESSAGE_MEDIA_TYPE,
|
|
5
|
+
MESSAGE_MEDIA_TYPE_MAPPER,
|
|
6
|
+
UNIFIED_MESSAGE_MEDIA_TYPE,
|
|
7
|
+
} from '../../src/instant-messages/message-types.js'
|
|
8
|
+
|
|
9
|
+
describe('UNIFIED_MESSAGE_MEDIA_TYPE', () => {
|
|
10
|
+
it('should include all keys from MESSAGE_MEDIA_TYPE via spread', () => {
|
|
11
|
+
for (const key of Object.keys(MESSAGE_MEDIA_TYPE)) {
|
|
12
|
+
expect(UNIFIED_MESSAGE_MEDIA_TYPE).toHaveProperty(key)
|
|
13
|
+
expect(UNIFIED_MESSAGE_MEDIA_TYPE[key]).toBe(MESSAGE_MEDIA_TYPE[key])
|
|
14
|
+
}
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('should include all keys from MESSAGE_TYPE via spread', () => {
|
|
18
|
+
for (const key of Object.keys(MESSAGE_TYPE)) {
|
|
19
|
+
expect(UNIFIED_MESSAGE_MEDIA_TYPE).toHaveProperty(key)
|
|
20
|
+
expect(UNIFIED_MESSAGE_MEDIA_TYPE[key]).toBe(MESSAGE_TYPE[key])
|
|
21
|
+
}
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should include normalized AUDIO mapped from VOICE', () => {
|
|
25
|
+
expect(UNIFIED_MESSAGE_MEDIA_TYPE.AUDIO).toBe(MESSAGE_MEDIA_TYPE.AUDIO)
|
|
26
|
+
expect(MESSAGE_MEDIA_TYPE_MAPPER[MESSAGE_MEDIA_TYPE.VOICE]).toBe(
|
|
27
|
+
MESSAGE_MEDIA_TYPE.AUDIO,
|
|
28
|
+
)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('should include normalized IMAGE mapped from PHOTO', () => {
|
|
32
|
+
expect(UNIFIED_MESSAGE_MEDIA_TYPE.IMAGE).toBe(MESSAGE_MEDIA_TYPE.IMAGE)
|
|
33
|
+
expect(MESSAGE_MEDIA_TYPE_MAPPER[MESSAGE_MEDIA_TYPE.PHOTO]).toBe(
|
|
34
|
+
MESSAGE_MEDIA_TYPE.IMAGE,
|
|
35
|
+
)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('should include normalized CONTACT mapped from CONTACTS', () => {
|
|
39
|
+
expect(UNIFIED_MESSAGE_MEDIA_TYPE.CONTACT).toBe(MESSAGE_MEDIA_TYPE.CONTACT)
|
|
40
|
+
expect(MESSAGE_MEDIA_TYPE_MAPPER[MESSAGE_MEDIA_TYPE.CONTACTS]).toBe(
|
|
41
|
+
MESSAGE_MEDIA_TYPE.CONTACT,
|
|
42
|
+
)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('should define UNKNOWN correctly', () => {
|
|
46
|
+
expect(UNIFIED_MESSAGE_MEDIA_TYPE.UNKNOWN_MESSAGE_TYPE).toBe(
|
|
47
|
+
MESSAGE_TYPE.UNKNOWN_MESSAGE_TYPE,
|
|
48
|
+
)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('should not contain unexpected duplicates', () => {
|
|
52
|
+
const values = Object.values(UNIFIED_MESSAGE_MEDIA_TYPE)
|
|
53
|
+
const uniqueValues = new Set(values)
|
|
54
|
+
expect(values.length).toBe(uniqueValues.size)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('should contain required high-level unified types', () => {
|
|
58
|
+
const required = [
|
|
59
|
+
'TEXT',
|
|
60
|
+
'IMAGE',
|
|
61
|
+
'AUDIO',
|
|
62
|
+
'VIDEO',
|
|
63
|
+
'DOCUMENT',
|
|
64
|
+
'STICKER',
|
|
65
|
+
'CONTACT',
|
|
66
|
+
'LOCATION',
|
|
67
|
+
'POLL',
|
|
68
|
+
'VIDEO_NOTE',
|
|
69
|
+
'BUTTON_CLICK',
|
|
70
|
+
'BUTTON_CLICK_MULTIPLE',
|
|
71
|
+
'REACTION',
|
|
72
|
+
]
|
|
73
|
+
|
|
74
|
+
for (const key of required) {
|
|
75
|
+
expect(UNIFIED_MESSAGE_MEDIA_TYPE).toHaveProperty(key)
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
})
|