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
package/package.json
CHANGED
|
@@ -7,54 +7,24 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @readonly
|
|
9
9
|
* @enum {string}
|
|
10
|
+
* @type {{ [key: string]: string }}
|
|
10
11
|
*
|
|
11
12
|
* @property {"text"} TEXT
|
|
12
|
-
* Represents plain text content.
|
|
13
|
-
*
|
|
14
13
|
* @property {"poll"} POLL
|
|
15
|
-
* Represents a Telegram poll (multiple-choice question).
|
|
16
|
-
*
|
|
17
14
|
* @property {"video"} VIDEO
|
|
18
|
-
* Represents a standard video file.
|
|
19
|
-
*
|
|
20
15
|
* @property {"photo"} PHOTO
|
|
21
|
-
* Represents a Telegram “photo” array (before mapping to IMAGE).
|
|
22
|
-
*
|
|
23
16
|
* @property {"image"} IMAGE
|
|
24
|
-
* Represents an image file (after normalization).
|
|
25
|
-
*
|
|
26
17
|
* @property {"voice"} VOICE
|
|
27
|
-
* Represents Telegram's "voice" messages (OGG encoded voice notes).
|
|
28
|
-
*
|
|
29
18
|
* @property {"audio"} AUDIO
|
|
30
|
-
* Represents general audio files (WhatsApp voice notes, audio uploads).
|
|
31
|
-
*
|
|
32
19
|
* @property {"sticker"} STICKER
|
|
33
|
-
* Represents sticker messages (Telegram or WhatsApp).
|
|
34
|
-
*
|
|
35
20
|
* @property {"contact"} CONTACT
|
|
36
|
-
* Represents a shared contact card.
|
|
37
|
-
*
|
|
38
21
|
* @property {"reaction"} REACTION
|
|
39
|
-
* Represents WhatsApp/Telegram reactions (emojis on messages).
|
|
40
|
-
*
|
|
41
22
|
* @property {"document"} DOCUMENT
|
|
42
|
-
* Represents generic uploaded files, including PDFs.
|
|
43
|
-
*
|
|
44
23
|
* @property {"location"} LOCATION
|
|
45
|
-
* Represents geographic coordinates.
|
|
46
|
-
*
|
|
47
24
|
* @property {"contacts"} CONTACTS
|
|
48
|
-
* Represents WhatsApp contacts array (before mapping to CONTACT).
|
|
49
|
-
*
|
|
50
25
|
* @property {"video_note"} VIDEO_NOTE
|
|
51
|
-
* Represents Telegram's circular "video note".
|
|
52
|
-
*
|
|
53
26
|
* @property {"button_click"} BUTTON_CLICK
|
|
54
|
-
* Represents a button press (interactive replies).
|
|
55
|
-
*
|
|
56
27
|
* @property {"button_click_multiple"} BUTTON_CLICK_MULTIPLE
|
|
57
|
-
* Represents list/menu selection (e.g., WhatsApp list_reply).
|
|
58
28
|
*/
|
|
59
29
|
export const MESSAGE_MEDIA_TYPE = {
|
|
60
30
|
TEXT: 'text',
|
|
@@ -75,7 +45,6 @@ export const MESSAGE_MEDIA_TYPE = {
|
|
|
75
45
|
BUTTON_CLICK: 'button_click',
|
|
76
46
|
BUTTON_CLICK_MULTIPLE: 'button_click_multiple',
|
|
77
47
|
}
|
|
78
|
-
|
|
79
48
|
/**
|
|
80
49
|
* Additional high-level message categories.
|
|
81
50
|
*
|
|
@@ -83,6 +52,7 @@ export const MESSAGE_MEDIA_TYPE = {
|
|
|
83
52
|
*
|
|
84
53
|
* @readonly
|
|
85
54
|
* @enum {string}
|
|
55
|
+
* @type {{ [key: string]: string }}
|
|
86
56
|
*
|
|
87
57
|
* @property {"message"} MESSAGE
|
|
88
58
|
* Regular message container (base type in some providers).
|
|
@@ -125,3 +95,27 @@ export const MESSAGE_MEDIA_TYPE_MAPPER = {
|
|
|
125
95
|
[MESSAGE_MEDIA_TYPE.PHOTO]: MESSAGE_MEDIA_TYPE.IMAGE,
|
|
126
96
|
[MESSAGE_MEDIA_TYPE.CONTACTS]: MESSAGE_MEDIA_TYPE.CONTACT,
|
|
127
97
|
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Unified message media types based on existing MESSAGE_MEDIA_TYPE and MESSAGE_TYPE.
|
|
101
|
+
*
|
|
102
|
+
* This enum flattens and merges all raw message media types
|
|
103
|
+
* into a single canonical type list.
|
|
104
|
+
*
|
|
105
|
+
* VOICE → AUDIO
|
|
106
|
+
* PHOTO → IMAGE
|
|
107
|
+
* CONTACTS → CONTACT
|
|
108
|
+
*
|
|
109
|
+
* @readonly
|
|
110
|
+
* @enum {string}
|
|
111
|
+
* @type {{ [key: string]: string }}
|
|
112
|
+
*/
|
|
113
|
+
export const UNIFIED_MESSAGE_MEDIA_TYPE = {
|
|
114
|
+
...MESSAGE_MEDIA_TYPE,
|
|
115
|
+
...MESSAGE_TYPE,
|
|
116
|
+
|
|
117
|
+
// Normalized equivalents
|
|
118
|
+
AUDIO: MESSAGE_MEDIA_TYPE.AUDIO,
|
|
119
|
+
IMAGE: MESSAGE_MEDIA_TYPE.IMAGE,
|
|
120
|
+
CONTACT: MESSAGE_MEDIA_TYPE.CONTACT,
|
|
121
|
+
}
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { post } from '../../http/http.js'
|
|
2
|
+
|
|
3
|
+
const TELEGRAM_API_BASE_URL = 'https://api.telegram.org'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Builds a full set of Telegram Bot API endpoint URLs
|
|
7
|
+
* based on the provided bot token and an optional base URL.
|
|
8
|
+
*
|
|
9
|
+
* This helper centralizes all Telegram endpoints used by the system,
|
|
10
|
+
* making it easier to mock, override, or customize for testing environments.
|
|
11
|
+
*
|
|
12
|
+
* @typedef {Object} TelegramApiUrls
|
|
13
|
+
* @property {string} SEND_MESSAGE
|
|
14
|
+
* URL to send a text message to a chat using the Telegram Bot API.
|
|
15
|
+
*
|
|
16
|
+
* @property {string} FORWARD_MESSAGE
|
|
17
|
+
* URL to forward an existing message from one chat to another.
|
|
18
|
+
*
|
|
19
|
+
* @property {string} SEND_PHOTO
|
|
20
|
+
* URL to send a photo to a chat.
|
|
21
|
+
*
|
|
22
|
+
* @property {string} SEND_AUDIO
|
|
23
|
+
* URL to send an audio file.
|
|
24
|
+
*
|
|
25
|
+
* @property {string} SEND_DOCUMENT
|
|
26
|
+
* URL to send files such as PDF, DOC, ZIP, and others supported by Telegram.
|
|
27
|
+
*
|
|
28
|
+
* @property {string} SEND_STICKER
|
|
29
|
+
* URL to send a sticker.
|
|
30
|
+
*
|
|
31
|
+
* @property {string} SEND_VIDEO
|
|
32
|
+
* URL to send a video file.
|
|
33
|
+
*
|
|
34
|
+
* @property {string} SEND_VOICE
|
|
35
|
+
* URL to send a voice note.
|
|
36
|
+
*
|
|
37
|
+
* @property {string} SEND_LOCATION
|
|
38
|
+
* URL to send a geolocation point.
|
|
39
|
+
*
|
|
40
|
+
* @property {string} SEND_CHAT_ACTION
|
|
41
|
+
* URL to send a chat action (typing, uploading photo, etc).
|
|
42
|
+
*
|
|
43
|
+
* @property {string} GET_USER_PROFILE_PHOTOS
|
|
44
|
+
* URL to retrieve the profile photos of a specific user.
|
|
45
|
+
*
|
|
46
|
+
* @property {string} GET_UPDATES
|
|
47
|
+
* URL to poll for new updates (not used when using webhooks).
|
|
48
|
+
*
|
|
49
|
+
* @property {string} GET_FILE
|
|
50
|
+
* URL to fetch a file path for downloading a file uploaded to Telegram servers.
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Generates Telegram Bot API endpoint URLs for the given bot token.
|
|
55
|
+
*
|
|
56
|
+
* @param {Object} params
|
|
57
|
+
* @param {string} params.token
|
|
58
|
+
* The bot token obtained from BotFather.
|
|
59
|
+
*
|
|
60
|
+
* @param {string} [params.telegramBaseUrl=TELEGRAM_API_BASE_URL]
|
|
61
|
+
* Optional override for the Telegram API base URL.
|
|
62
|
+
* Useful for testing or for proxying requests.
|
|
63
|
+
*
|
|
64
|
+
* @returns {TelegramApiUrls}
|
|
65
|
+
* A dictionary of fully resolved Telegram API endpoint URLs.
|
|
66
|
+
*/
|
|
67
|
+
export const getTelegramApiUrls = ({
|
|
68
|
+
token,
|
|
69
|
+
telegramBaseUrl = TELEGRAM_API_BASE_URL,
|
|
70
|
+
}) => ({
|
|
71
|
+
SEND_MESSAGE: `${telegramBaseUrl}/bot${token}/sendMessage`,
|
|
72
|
+
FORWARD_MESSAGE: `${telegramBaseUrl}/bot${token}/forwardMessage`,
|
|
73
|
+
SEND_PHOTO: `${telegramBaseUrl}/bot${token}/sendPhoto`,
|
|
74
|
+
SEND_AUDIO: `${telegramBaseUrl}/bot${token}/sendAudio`,
|
|
75
|
+
SEND_DOCUMENT: `${telegramBaseUrl}/bot${token}/sendDocument`,
|
|
76
|
+
SEND_STICKER: `${telegramBaseUrl}/bot${token}/sendSticker`,
|
|
77
|
+
SEND_VIDEO: `${telegramBaseUrl}/bot${token}/sendVideo`,
|
|
78
|
+
SEND_VOICE: `${telegramBaseUrl}/bot${token}/sendVoice`,
|
|
79
|
+
SEND_LOCATION: `${telegramBaseUrl}/bot${token}/sendLocation`,
|
|
80
|
+
SEND_CHAT_ACTION: `${telegramBaseUrl}/bot${token}/sendChatAction`,
|
|
81
|
+
GET_USER_PROFILE_PHOTOS: `${telegramBaseUrl}/bot${token}/getUserProfilePhotos`,
|
|
82
|
+
GET_UPDATES: `${telegramBaseUrl}/bot${token}/getUpdates`,
|
|
83
|
+
GET_FILE: `${telegramBaseUrl}/bot${token}/getFile`,
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Factory that creates a set of high level Telegram Bot API helper methods.
|
|
88
|
+
*
|
|
89
|
+
* Each method sends a specific type of message (text, photo, video, document)
|
|
90
|
+
* through the Telegram Bot API using the provided bot token.
|
|
91
|
+
*
|
|
92
|
+
* This abstraction wraps the raw URL generation logic and HTTP calls,
|
|
93
|
+
* allowing higher level services to use clean method calls instead of
|
|
94
|
+
* managing endpoint URLs manually.
|
|
95
|
+
*
|
|
96
|
+
* @typedef {Object} TelegramApis
|
|
97
|
+
*
|
|
98
|
+
* @property {Function} sendMessage
|
|
99
|
+
* Sends a text message to a specific chat.
|
|
100
|
+
*
|
|
101
|
+
* @property {Function} sendButtonsGroup
|
|
102
|
+
* Sends a text message with an inline keyboard button group.
|
|
103
|
+
*
|
|
104
|
+
* @property {Function} sendPhoto
|
|
105
|
+
* Sends a photo with an optional caption.
|
|
106
|
+
*
|
|
107
|
+
* @property {Function} sendVideo
|
|
108
|
+
* Sends a video with an optional caption.
|
|
109
|
+
*
|
|
110
|
+
* @property {Function} sendAudio
|
|
111
|
+
* Sends an audio file with an optional caption.
|
|
112
|
+
*
|
|
113
|
+
* @property {Function} sendDocument
|
|
114
|
+
* Sends a document file with an optional caption.
|
|
115
|
+
*/
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Creates Telegram API methods bound to a specific bot token.
|
|
119
|
+
*
|
|
120
|
+
* @param {Object} params
|
|
121
|
+
* @param {string} params.token
|
|
122
|
+
* Telegram bot token obtained from BotFather.
|
|
123
|
+
*
|
|
124
|
+
* @returns {TelegramApis}
|
|
125
|
+
* An object containing all supported Telegram message sending functions.
|
|
126
|
+
*/
|
|
127
|
+
export const telegramApis = ({ token }) => {
|
|
128
|
+
const APIS = getTelegramApiUrls({ token })
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
/**
|
|
132
|
+
* Sends a text message to a Telegram chat.
|
|
133
|
+
*
|
|
134
|
+
* @param {Object} params
|
|
135
|
+
* @param {string} params.text
|
|
136
|
+
* The message content.
|
|
137
|
+
*
|
|
138
|
+
* @param {number|string} params.chatId
|
|
139
|
+
* Chat identifier where the message should be sent.
|
|
140
|
+
*
|
|
141
|
+
* @param {Array<Object>} [params.entities]
|
|
142
|
+
* Optional entities for formatting (bold, URL, etc).
|
|
143
|
+
*
|
|
144
|
+
* @returns {Promise<import('../../http/http.js').HttpResponse>}
|
|
145
|
+
* Telegram API response.
|
|
146
|
+
*/
|
|
147
|
+
async sendMessage({ text, chatId, entities }) {
|
|
148
|
+
const res = await post({
|
|
149
|
+
url: APIS.SEND_MESSAGE,
|
|
150
|
+
body: {
|
|
151
|
+
chat_id: chatId,
|
|
152
|
+
text,
|
|
153
|
+
entities,
|
|
154
|
+
},
|
|
155
|
+
})
|
|
156
|
+
return res
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Sends a text message with inline keyboard buttons.
|
|
161
|
+
*
|
|
162
|
+
* @param {Object} params
|
|
163
|
+
* @param {string} params.text
|
|
164
|
+
* The message content.
|
|
165
|
+
*
|
|
166
|
+
* @param {number|string} params.chatId
|
|
167
|
+
* Chat identifier.
|
|
168
|
+
*
|
|
169
|
+
* @param {Array<Array<Object>>} params.options
|
|
170
|
+
* Two dimensional array of inline keyboard button objects.
|
|
171
|
+
*
|
|
172
|
+
* @returns {Promise<import('../../http/http.js').HttpResponse>}
|
|
173
|
+
* Telegram API response.
|
|
174
|
+
*/
|
|
175
|
+
async sendButtonsGroup({ text, chatId, options }) {
|
|
176
|
+
const res = await post({
|
|
177
|
+
url: APIS.SEND_MESSAGE,
|
|
178
|
+
body: {
|
|
179
|
+
chat_id: chatId,
|
|
180
|
+
text,
|
|
181
|
+
reply_markup: {
|
|
182
|
+
inline_keyboard: options,
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
})
|
|
186
|
+
return res
|
|
187
|
+
},
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Sends a photo message using an HTTP URL.
|
|
191
|
+
*
|
|
192
|
+
* @param {Object} params
|
|
193
|
+
* @param {number|string} params.chatId
|
|
194
|
+
* Chat identifier.
|
|
195
|
+
*
|
|
196
|
+
* @param {string} params.photo
|
|
197
|
+
* Publicly accessible HTTP URL of the photo.
|
|
198
|
+
*
|
|
199
|
+
* @param {string} [params.caption]
|
|
200
|
+
* Optional caption for the photo.
|
|
201
|
+
*
|
|
202
|
+
* @returns {Promise<import('../../http/http.js').HttpResponse>}
|
|
203
|
+
* Telegram API response.
|
|
204
|
+
*/
|
|
205
|
+
async sendPhoto({ caption, photo, chatId }) {
|
|
206
|
+
const res = await post({
|
|
207
|
+
url: APIS.SEND_PHOTO,
|
|
208
|
+
body: {
|
|
209
|
+
chat_id: chatId,
|
|
210
|
+
caption,
|
|
211
|
+
photo,
|
|
212
|
+
},
|
|
213
|
+
})
|
|
214
|
+
return res
|
|
215
|
+
},
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Sends a video message using an HTTP URL.
|
|
219
|
+
*
|
|
220
|
+
* @param {Object} params
|
|
221
|
+
* @param {number|string} params.chatId
|
|
222
|
+
* Chat identifier.
|
|
223
|
+
*
|
|
224
|
+
* @param {string} params.video
|
|
225
|
+
* Public video URL.
|
|
226
|
+
*
|
|
227
|
+
* @param {string} [params.caption]
|
|
228
|
+
* Optional caption.
|
|
229
|
+
*
|
|
230
|
+
* @returns {Promise<import('../../http/http.js').HttpResponse>}
|
|
231
|
+
* Telegram API response.
|
|
232
|
+
*/
|
|
233
|
+
async sendVideo({ caption, video, chatId }) {
|
|
234
|
+
const res = await post({
|
|
235
|
+
url: APIS.SEND_VIDEO,
|
|
236
|
+
body: {
|
|
237
|
+
chat_id: chatId,
|
|
238
|
+
caption,
|
|
239
|
+
video,
|
|
240
|
+
},
|
|
241
|
+
})
|
|
242
|
+
return res
|
|
243
|
+
},
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Sends an audio message using an HTTP URL.
|
|
247
|
+
*
|
|
248
|
+
* @param {Object} params
|
|
249
|
+
* @param {number|string} params.chatId
|
|
250
|
+
* Chat identifier.
|
|
251
|
+
*
|
|
252
|
+
* @param {string} params.audio
|
|
253
|
+
* Public audio URL.
|
|
254
|
+
*
|
|
255
|
+
* @param {string} [params.caption]
|
|
256
|
+
* Optional caption.
|
|
257
|
+
*
|
|
258
|
+
* @returns {Promise<import('../../http/http.js').HttpResponse>}
|
|
259
|
+
* Telegram API response.
|
|
260
|
+
*/
|
|
261
|
+
async sendAudio({ caption, audio, chatId }) {
|
|
262
|
+
const res = await post({
|
|
263
|
+
url: APIS.SEND_AUDIO,
|
|
264
|
+
body: {
|
|
265
|
+
chat_id: chatId,
|
|
266
|
+
caption,
|
|
267
|
+
audio,
|
|
268
|
+
},
|
|
269
|
+
})
|
|
270
|
+
return res
|
|
271
|
+
},
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Sends a document file using an HTTP URL.
|
|
275
|
+
*
|
|
276
|
+
* @param {Object} params
|
|
277
|
+
* @param {number|string} params.chatId
|
|
278
|
+
* Chat identifier.
|
|
279
|
+
*
|
|
280
|
+
* @param {string} params.document
|
|
281
|
+
* URL to the document file.
|
|
282
|
+
*
|
|
283
|
+
* @param {string} [params.caption]
|
|
284
|
+
* Optional caption.
|
|
285
|
+
*
|
|
286
|
+
* @returns {Promise<import('../../http/http.js').HttpResponse>}
|
|
287
|
+
* Telegram API response.
|
|
288
|
+
*/
|
|
289
|
+
async sendDocument({ caption, document, chatId }) {
|
|
290
|
+
const res = await post({
|
|
291
|
+
url: APIS.SEND_DOCUMENT,
|
|
292
|
+
body: {
|
|
293
|
+
chat_id: chatId,
|
|
294
|
+
caption,
|
|
295
|
+
document,
|
|
296
|
+
},
|
|
297
|
+
})
|
|
298
|
+
return res
|
|
299
|
+
},
|
|
300
|
+
}
|
|
301
|
+
}
|