core-services-sdk 1.3.46 → 1.3.48
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/message-type.js +26 -26
- package/src/instant-messages/message-unified-mapper.js +35 -31
- package/tests/instant-messages/message-type.unit.test.js +50 -58
- package/tests/instant-messages/message-unified-mapper-telegram.unit.test.js +3 -3
- package/tests/instant-messages/message-unified-mapper-united.unit.test.js +1 -1
- package/tests/instant-messages/message-unified-mapper-whatsapp.unit.test.js +10 -5
- package/types/ids/generators.d.ts +1 -0
- package/types/ids/prefixes.d.ts +2 -0
- package/types/instant-messages/message-type.d.ts +17 -21
- package/types/instant-messages/message-unified-mapper.d.ts +25 -49
package/package.json
CHANGED
|
@@ -5,12 +5,12 @@ import { MESSAGE_MEDIA_TYPE, MESSAGE_TYPE } from './message-types.js'
|
|
|
5
5
|
* inside the platform-original message object.
|
|
6
6
|
*
|
|
7
7
|
* @param {string} mediaType - One of MESSAGE_MEDIA_TYPE.*
|
|
8
|
-
* @returns {(params: {
|
|
8
|
+
* @returns {(params: { imMessage: any }) => boolean}
|
|
9
9
|
*/
|
|
10
10
|
export const isItMediaType =
|
|
11
11
|
(mediaType) =>
|
|
12
|
-
({
|
|
13
|
-
const message =
|
|
12
|
+
({ imMessage }) => {
|
|
13
|
+
const message = imMessage?.message
|
|
14
14
|
if (!message || typeof message !== 'object') {
|
|
15
15
|
return false
|
|
16
16
|
}
|
|
@@ -23,14 +23,14 @@ export const isItMediaType =
|
|
|
23
23
|
*
|
|
24
24
|
* @function isMessageTypeof
|
|
25
25
|
* @param {string} typeOfMessage - One of the MESSAGE_TYPE.* values.
|
|
26
|
-
* @returns {(params: {
|
|
27
|
-
* A function that accepts an object containing `
|
|
26
|
+
* @returns {(params: { imMessage: any }) => boolean}
|
|
27
|
+
* A function that accepts an object containing `imMessage`
|
|
28
28
|
* and returns true if its `type` matches the expected type.
|
|
29
29
|
*/
|
|
30
30
|
export const isMessageTypeof =
|
|
31
31
|
(typeOfMessage) =>
|
|
32
|
-
({
|
|
33
|
-
const type =
|
|
32
|
+
({ imMessage }) => {
|
|
33
|
+
const type = imMessage?.type
|
|
34
34
|
return type === typeOfMessage
|
|
35
35
|
}
|
|
36
36
|
|
|
@@ -41,11 +41,11 @@ export const isMessageTypeof =
|
|
|
41
41
|
*
|
|
42
42
|
* @function isCallbackQuery
|
|
43
43
|
* @param {Object} params
|
|
44
|
-
* @param {Object} params.
|
|
44
|
+
* @param {Object} params.imMessage - Raw Telegram update
|
|
45
45
|
* @returns {boolean}
|
|
46
46
|
*/
|
|
47
|
-
export const isCallbackQuery = ({
|
|
48
|
-
return 'callback_query' in
|
|
47
|
+
export const isCallbackQuery = ({ imMessage }) => {
|
|
48
|
+
return 'callback_query' in imMessage
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
// Media-type detectors for each supported message detail section.
|
|
@@ -77,7 +77,7 @@ export const isItButtonClick = isMessageTypeof(MESSAGE_TYPE.BUTTON_CLICK)
|
|
|
77
77
|
*
|
|
78
78
|
* @function getTelegramMessageType
|
|
79
79
|
* @param {Object} params
|
|
80
|
-
* @param {Object} params.
|
|
80
|
+
* @param {Object} params.imMessage - Raw update object from Telegram or WhatsApp.
|
|
81
81
|
* For Telegram:
|
|
82
82
|
* - May contain: message, callback_query, poll, etc.
|
|
83
83
|
* For WhatsApp:
|
|
@@ -89,60 +89,60 @@ export const isItButtonClick = isMessageTypeof(MESSAGE_TYPE.BUTTON_CLICK)
|
|
|
89
89
|
* - MESSAGE_TYPE.UNKNOWN_MESSAGE_TYPE
|
|
90
90
|
*
|
|
91
91
|
* @example
|
|
92
|
-
* getTelegramMessageType({
|
|
92
|
+
* getTelegramMessageType({ imMessage: telegramUpdate })
|
|
93
93
|
* // → "text"
|
|
94
94
|
*
|
|
95
95
|
* @example
|
|
96
|
-
* getTelegramMessageType({
|
|
96
|
+
* getTelegramMessageType({ imMessage: whatsappPayload })
|
|
97
97
|
* // → "image"
|
|
98
98
|
*/
|
|
99
|
-
export const getTelegramMessageType = ({
|
|
99
|
+
export const getTelegramMessageType = ({ imMessage }) => {
|
|
100
100
|
switch (true) {
|
|
101
|
-
case isCallbackQuery({
|
|
101
|
+
case isCallbackQuery({ imMessage }): {
|
|
102
102
|
return MESSAGE_TYPE.BUTTON_CLICK
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
case isItFreeText({
|
|
105
|
+
case isItFreeText({ imMessage }): {
|
|
106
106
|
return MESSAGE_MEDIA_TYPE.TEXT
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
-
case isItVideo({
|
|
109
|
+
case isItVideo({ imMessage }): {
|
|
110
110
|
return MESSAGE_MEDIA_TYPE.VIDEO
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
-
case isItPhoto({
|
|
113
|
+
case isItPhoto({ imMessage }): {
|
|
114
114
|
return MESSAGE_MEDIA_TYPE.PHOTO
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
-
case isItDocument({
|
|
117
|
+
case isItDocument({ imMessage }): {
|
|
118
118
|
return MESSAGE_MEDIA_TYPE.DOCUMENT
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
case isItLocation({
|
|
121
|
+
case isItLocation({ imMessage }): {
|
|
122
122
|
return MESSAGE_MEDIA_TYPE.LOCATION
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
-
case isItVoice({
|
|
125
|
+
case isItVoice({ imMessage }): {
|
|
126
126
|
return MESSAGE_MEDIA_TYPE.VOICE
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
-
case isItVideoNote({
|
|
129
|
+
case isItVideoNote({ imMessage }): {
|
|
130
130
|
return MESSAGE_MEDIA_TYPE.VIDEO_NOTE
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
-
case isItPoll({
|
|
133
|
+
case isItPoll({ imMessage }): {
|
|
134
134
|
return MESSAGE_MEDIA_TYPE.POLL
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
-
case isItSticker({
|
|
137
|
+
case isItSticker({ imMessage }): {
|
|
138
138
|
return MESSAGE_MEDIA_TYPE.STICKER
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
-
case isItMessage({
|
|
141
|
+
case isItMessage({ imMessage }): {
|
|
142
142
|
return MESSAGE_MEDIA_TYPE.MESSAGE
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
-
case isItContact({
|
|
145
|
+
case isItContact({ imMessage }): {
|
|
146
146
|
return MESSAGE_MEDIA_TYPE.CONTACT
|
|
147
147
|
}
|
|
148
148
|
|
|
@@ -17,25 +17,25 @@ const INTERACTIVE_MAPPER = {
|
|
|
17
17
|
* and delegates to the correct internal resolver.
|
|
18
18
|
*
|
|
19
19
|
* @param {Object} params
|
|
20
|
-
* @param {Object} params.
|
|
20
|
+
* @param {Object} params.imMessage - Raw message payload
|
|
21
21
|
* @returns {string} Unified message type
|
|
22
22
|
*/
|
|
23
|
-
export const getMessageType = ({
|
|
24
|
-
if (!
|
|
23
|
+
export const getMessageType = ({ imMessage }) => {
|
|
24
|
+
if (!imMessage || typeof imMessage !== 'object') {
|
|
25
25
|
return MESSAGE_TYPE.UNKNOWN_MESSAGE_TYPE
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
// Telegram format
|
|
29
29
|
if (
|
|
30
|
-
'update_id' in
|
|
31
|
-
'message' in
|
|
32
|
-
'callback_query' in
|
|
30
|
+
'update_id' in imMessage ||
|
|
31
|
+
'message' in imMessage ||
|
|
32
|
+
'callback_query' in imMessage
|
|
33
33
|
) {
|
|
34
|
-
return getTelegramMessageType({
|
|
34
|
+
return getTelegramMessageType({ imMessage })
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
// WhatsApp format
|
|
38
|
-
const entry =
|
|
38
|
+
const entry = imMessage?.entry?.[0]
|
|
39
39
|
const change = entry?.changes?.[0]
|
|
40
40
|
const message = change?.value?.messages?.[0]
|
|
41
41
|
|
|
@@ -46,11 +46,11 @@ export const getMessageType = ({ originalMessage }) => {
|
|
|
46
46
|
return MESSAGE_TYPE.UNKNOWN_MESSAGE_TYPE
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
export const mapMessageTelegramBase = ({
|
|
50
|
-
const { callback_query, message, update_id } =
|
|
49
|
+
export const mapMessageTelegramBase = ({ imMessage }) => {
|
|
50
|
+
const { callback_query, message, update_id } = imMessage
|
|
51
51
|
const messageData = callback_query?.message || message
|
|
52
52
|
const { chat, date, from, message_id } = messageData
|
|
53
|
-
const type = getTelegramMessageType({
|
|
53
|
+
const type = getTelegramMessageType({ imMessage })
|
|
54
54
|
const typeMapped = MESSAGE_MEDIA_TYPE_MAPPER[type] || type
|
|
55
55
|
const { forward_date, forward_from } = messageData
|
|
56
56
|
const itIsForward = !!(forward_date && forward_from)
|
|
@@ -115,17 +115,21 @@ export const mapMessageWhatsAppContent = ({ message, type }) => {
|
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
-
export const mapMessageTelegram = ({
|
|
118
|
+
export const mapMessageTelegram = ({ imMessage }) => {
|
|
119
119
|
const { messageBase, type, message } = mapMessageTelegramBase({
|
|
120
|
-
|
|
120
|
+
imMessage,
|
|
121
121
|
})
|
|
122
122
|
const messageContent = mapMessageTelegramContent({
|
|
123
123
|
type,
|
|
124
124
|
message,
|
|
125
|
-
|
|
125
|
+
imMessage,
|
|
126
126
|
})
|
|
127
|
-
|
|
128
|
-
return
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
...messageBase,
|
|
130
|
+
...messageContent,
|
|
131
|
+
type: MESSAGE_MEDIA_TYPE_MAPPER[type] || type,
|
|
132
|
+
}
|
|
129
133
|
}
|
|
130
134
|
|
|
131
135
|
export const getWhatsAppMessageType = ({ message }) => {
|
|
@@ -140,8 +144,8 @@ export const getWhatsAppMessageType = ({ message }) => {
|
|
|
140
144
|
}
|
|
141
145
|
}
|
|
142
146
|
|
|
143
|
-
export const extractReply = ({
|
|
144
|
-
const { callback_query } =
|
|
147
|
+
export const extractReply = ({ imMessage }) => {
|
|
148
|
+
const { callback_query } = imMessage
|
|
145
149
|
const { data: id, message } = callback_query
|
|
146
150
|
const {
|
|
147
151
|
reply_markup: { inline_keyboard },
|
|
@@ -155,17 +159,17 @@ export const extractReply = ({ originalMessage }) => {
|
|
|
155
159
|
|
|
156
160
|
return { id, title }
|
|
157
161
|
}
|
|
158
|
-
export const whatsappBaseExtraction = ({
|
|
162
|
+
export const whatsappBaseExtraction = ({ imMessage }) => {
|
|
159
163
|
const {
|
|
160
164
|
entry: [{ changes, id }],
|
|
161
|
-
} =
|
|
165
|
+
} = imMessage
|
|
162
166
|
const [change] = changes
|
|
163
167
|
const { field, value } = change
|
|
164
168
|
return { field, value, wbaid: id }
|
|
165
169
|
}
|
|
166
170
|
|
|
167
|
-
export const mapMessageWhatsAppBase = ({
|
|
168
|
-
const { field, value, wbaid } = whatsappBaseExtraction({
|
|
171
|
+
export const mapMessageWhatsAppBase = ({ imMessage }) => {
|
|
172
|
+
const { field, value, wbaid } = whatsappBaseExtraction({ imMessage })
|
|
169
173
|
const { [field]: messages, contacts } = value
|
|
170
174
|
const [message] = messages
|
|
171
175
|
const [contact] = contacts
|
|
@@ -190,11 +194,7 @@ export const mapMessageWhatsAppBase = ({ originalMessage }) => {
|
|
|
190
194
|
return { messageBase, message, contact, context }
|
|
191
195
|
}
|
|
192
196
|
|
|
193
|
-
export const mapMessageTelegramContent = ({
|
|
194
|
-
type,
|
|
195
|
-
message,
|
|
196
|
-
originalMessage,
|
|
197
|
-
}) => {
|
|
197
|
+
export const mapMessageTelegramContent = ({ type, message, imMessage }) => {
|
|
198
198
|
switch (type) {
|
|
199
199
|
case MESSAGE_MEDIA_TYPE.TEXT:
|
|
200
200
|
return {
|
|
@@ -222,7 +222,7 @@ export const mapMessageTelegramContent = ({
|
|
|
222
222
|
...(animation ? { attachment: 'animation' } : null),
|
|
223
223
|
}
|
|
224
224
|
case MESSAGE_MEDIA_TYPE.BUTTON_CLICK:
|
|
225
|
-
const reply = extractReply({
|
|
225
|
+
const reply = extractReply({ imMessage })
|
|
226
226
|
return {
|
|
227
227
|
reply,
|
|
228
228
|
}
|
|
@@ -231,9 +231,9 @@ export const mapMessageTelegramContent = ({
|
|
|
231
231
|
}
|
|
232
232
|
}
|
|
233
233
|
|
|
234
|
-
export const mapMessageWhatsApp = ({
|
|
234
|
+
export const mapMessageWhatsApp = ({ imMessage }) => {
|
|
235
235
|
const { messageBase, message, context } = mapMessageWhatsAppBase({
|
|
236
|
-
|
|
236
|
+
imMessage,
|
|
237
237
|
})
|
|
238
238
|
const { type } = messageBase
|
|
239
239
|
const messageContent = mapMessageWhatsAppContent({
|
|
@@ -242,7 +242,11 @@ export const mapMessageWhatsApp = ({ originalMessage }) => {
|
|
|
242
242
|
context,
|
|
243
243
|
})
|
|
244
244
|
|
|
245
|
-
return {
|
|
245
|
+
return {
|
|
246
|
+
...messageBase,
|
|
247
|
+
...messageContent,
|
|
248
|
+
type: MESSAGE_MEDIA_TYPE_MAPPER[type] || type,
|
|
249
|
+
}
|
|
246
250
|
}
|
|
247
251
|
|
|
248
252
|
export const messageUnifiedMapper = {
|
|
@@ -23,18 +23,18 @@ describe('message-type helpers', () => {
|
|
|
23
23
|
const isPhoto = isItMediaType(MESSAGE_MEDIA_TYPE.PHOTO)
|
|
24
24
|
|
|
25
25
|
it('returns true when media type exists inside message', () => {
|
|
26
|
-
const
|
|
27
|
-
expect(isPhoto({
|
|
26
|
+
const imMessage = { message: { photo: [{}] } }
|
|
27
|
+
expect(isPhoto({ imMessage })).toBe(true)
|
|
28
28
|
})
|
|
29
29
|
|
|
30
30
|
it('returns false when media type does not exist', () => {
|
|
31
|
-
const
|
|
32
|
-
expect(isPhoto({
|
|
31
|
+
const imMessage = { message: { text: 'hi' } }
|
|
32
|
+
expect(isPhoto({ imMessage })).toBe(false)
|
|
33
33
|
})
|
|
34
34
|
|
|
35
35
|
it('returns false when message is missing', () => {
|
|
36
|
-
expect(isPhoto({
|
|
37
|
-
expect(isPhoto({
|
|
36
|
+
expect(isPhoto({ imMessage: {} })).toBe(false)
|
|
37
|
+
expect(isPhoto({ imMessage: null })).toBe(false)
|
|
38
38
|
})
|
|
39
39
|
})
|
|
40
40
|
|
|
@@ -42,150 +42,142 @@ describe('message-type helpers', () => {
|
|
|
42
42
|
const isButtonClick = isMessageTypeof(MESSAGE_TYPE.BUTTON_CLICK)
|
|
43
43
|
|
|
44
44
|
it('returns true when type matches', () => {
|
|
45
|
-
const
|
|
46
|
-
expect(isButtonClick({
|
|
45
|
+
const imMessage = { type: MESSAGE_TYPE.BUTTON_CLICK }
|
|
46
|
+
expect(isButtonClick({ imMessage })).toBe(true)
|
|
47
47
|
})
|
|
48
48
|
|
|
49
49
|
it('returns false when type differs', () => {
|
|
50
|
-
const
|
|
51
|
-
expect(isButtonClick({
|
|
50
|
+
const imMessage = { type: MESSAGE_MEDIA_TYPE.TEXT }
|
|
51
|
+
expect(isButtonClick({ imMessage })).toBe(false)
|
|
52
52
|
})
|
|
53
53
|
|
|
54
54
|
it('returns false on missing type', () => {
|
|
55
|
-
const
|
|
56
|
-
expect(isButtonClick({
|
|
55
|
+
const imMessage = {}
|
|
56
|
+
expect(isButtonClick({ imMessage })).toBe(false)
|
|
57
57
|
})
|
|
58
58
|
})
|
|
59
59
|
|
|
60
60
|
describe('isCallbackQuery', () => {
|
|
61
61
|
it('returns true for Telegram callback_query', () => {
|
|
62
|
-
const
|
|
63
|
-
expect(isCallbackQuery({
|
|
62
|
+
const imMessage = { callback_query: { data: '1' } }
|
|
63
|
+
expect(isCallbackQuery({ imMessage })).toBe(true)
|
|
64
64
|
})
|
|
65
65
|
|
|
66
66
|
it('returns false otherwise', () => {
|
|
67
|
-
expect(isCallbackQuery({
|
|
67
|
+
expect(isCallbackQuery({ imMessage: {} })).toBe(false)
|
|
68
68
|
})
|
|
69
69
|
})
|
|
70
70
|
|
|
71
71
|
describe('media helpers', () => {
|
|
72
72
|
it('isItFreeText works', () => {
|
|
73
|
-
expect(
|
|
74
|
-
|
|
75
|
-
)
|
|
73
|
+
expect(isItFreeText({ imMessage: { message: { text: 'hi' } } })).toBe(
|
|
74
|
+
true,
|
|
75
|
+
)
|
|
76
76
|
})
|
|
77
77
|
|
|
78
78
|
it('isItPhoto works', () => {
|
|
79
|
-
expect(isItPhoto({
|
|
80
|
-
true,
|
|
81
|
-
)
|
|
79
|
+
expect(isItPhoto({ imMessage: { message: { photo: [{}] } } })).toBe(true)
|
|
82
80
|
})
|
|
83
81
|
|
|
84
82
|
it('isItVideo works', () => {
|
|
85
|
-
expect(isItVideo({
|
|
86
|
-
true,
|
|
87
|
-
)
|
|
83
|
+
expect(isItVideo({ imMessage: { message: { video: {} } } })).toBe(true)
|
|
88
84
|
})
|
|
89
85
|
|
|
90
86
|
it('isItVoice works', () => {
|
|
91
|
-
expect(isItVoice({
|
|
92
|
-
true,
|
|
93
|
-
)
|
|
87
|
+
expect(isItVoice({ imMessage: { message: { voice: {} } } })).toBe(true)
|
|
94
88
|
})
|
|
95
89
|
|
|
96
90
|
it('isItDocument works', () => {
|
|
97
|
-
expect(
|
|
98
|
-
|
|
99
|
-
)
|
|
91
|
+
expect(isItDocument({ imMessage: { message: { document: {} } } })).toBe(
|
|
92
|
+
true,
|
|
93
|
+
)
|
|
100
94
|
})
|
|
101
95
|
|
|
102
96
|
it('isItContact works', () => {
|
|
103
|
-
expect(
|
|
104
|
-
|
|
105
|
-
)
|
|
97
|
+
expect(isItContact({ imMessage: { message: { contact: {} } } })).toBe(
|
|
98
|
+
true,
|
|
99
|
+
)
|
|
106
100
|
})
|
|
107
101
|
|
|
108
102
|
it('isItPoll works', () => {
|
|
109
|
-
expect(isItPoll({
|
|
110
|
-
true,
|
|
111
|
-
)
|
|
103
|
+
expect(isItPoll({ imMessage: { message: { poll: {} } } })).toBe(true)
|
|
112
104
|
})
|
|
113
105
|
})
|
|
114
106
|
|
|
115
107
|
describe('getTelegramMessageType', () => {
|
|
116
108
|
it('detects callback_query → BUTTON_CLICK', () => {
|
|
117
|
-
const
|
|
118
|
-
expect(getTelegramMessageType({
|
|
109
|
+
const imMessage = { callback_query: {} }
|
|
110
|
+
expect(getTelegramMessageType({ imMessage })).toBe(
|
|
119
111
|
MESSAGE_TYPE.BUTTON_CLICK,
|
|
120
112
|
)
|
|
121
113
|
})
|
|
122
114
|
|
|
123
115
|
it('detects text', () => {
|
|
124
|
-
const
|
|
125
|
-
expect(getTelegramMessageType({
|
|
116
|
+
const imMessage = { message: { text: 'hello' } }
|
|
117
|
+
expect(getTelegramMessageType({ imMessage })).toBe(
|
|
126
118
|
MESSAGE_MEDIA_TYPE.TEXT,
|
|
127
119
|
)
|
|
128
120
|
})
|
|
129
121
|
|
|
130
122
|
it('detects photo', () => {
|
|
131
|
-
const
|
|
132
|
-
expect(getTelegramMessageType({
|
|
123
|
+
const imMessage = { message: { photo: [{}] } }
|
|
124
|
+
expect(getTelegramMessageType({ imMessage })).toBe(
|
|
133
125
|
MESSAGE_MEDIA_TYPE.PHOTO,
|
|
134
126
|
)
|
|
135
127
|
})
|
|
136
128
|
|
|
137
129
|
it('detects video', () => {
|
|
138
|
-
const
|
|
139
|
-
expect(getTelegramMessageType({
|
|
130
|
+
const imMessage = { message: { video: {} } }
|
|
131
|
+
expect(getTelegramMessageType({ imMessage })).toBe(
|
|
140
132
|
MESSAGE_MEDIA_TYPE.VIDEO,
|
|
141
133
|
)
|
|
142
134
|
})
|
|
143
135
|
|
|
144
136
|
it('detects document', () => {
|
|
145
|
-
const
|
|
146
|
-
expect(getTelegramMessageType({
|
|
137
|
+
const imMessage = { message: { document: {} } }
|
|
138
|
+
expect(getTelegramMessageType({ imMessage })).toBe(
|
|
147
139
|
MESSAGE_MEDIA_TYPE.DOCUMENT,
|
|
148
140
|
)
|
|
149
141
|
})
|
|
150
142
|
|
|
151
143
|
it('detects location', () => {
|
|
152
|
-
const
|
|
153
|
-
expect(getTelegramMessageType({
|
|
144
|
+
const imMessage = { message: { location: {} } }
|
|
145
|
+
expect(getTelegramMessageType({ imMessage })).toBe(
|
|
154
146
|
MESSAGE_MEDIA_TYPE.LOCATION,
|
|
155
147
|
)
|
|
156
148
|
})
|
|
157
149
|
|
|
158
150
|
it('detects voice', () => {
|
|
159
|
-
const
|
|
160
|
-
expect(getTelegramMessageType({
|
|
151
|
+
const imMessage = { message: { voice: {} } }
|
|
152
|
+
expect(getTelegramMessageType({ imMessage })).toBe(
|
|
161
153
|
MESSAGE_MEDIA_TYPE.VOICE,
|
|
162
154
|
)
|
|
163
155
|
})
|
|
164
156
|
|
|
165
157
|
it('detects poll', () => {
|
|
166
|
-
const
|
|
167
|
-
expect(getTelegramMessageType({
|
|
158
|
+
const imMessage = { message: { poll: {} } }
|
|
159
|
+
expect(getTelegramMessageType({ imMessage })).toBe(
|
|
168
160
|
MESSAGE_MEDIA_TYPE.POLL,
|
|
169
161
|
)
|
|
170
162
|
})
|
|
171
163
|
|
|
172
164
|
it('detects sticker', () => {
|
|
173
|
-
const
|
|
174
|
-
expect(getTelegramMessageType({
|
|
165
|
+
const imMessage = { message: { sticker: {} } }
|
|
166
|
+
expect(getTelegramMessageType({ imMessage })).toBe(
|
|
175
167
|
MESSAGE_MEDIA_TYPE.STICKER,
|
|
176
168
|
)
|
|
177
169
|
})
|
|
178
170
|
|
|
179
171
|
it('detects contact', () => {
|
|
180
|
-
const
|
|
181
|
-
expect(getTelegramMessageType({
|
|
172
|
+
const imMessage = { message: { contact: {} } }
|
|
173
|
+
expect(getTelegramMessageType({ imMessage })).toBe(
|
|
182
174
|
MESSAGE_MEDIA_TYPE.CONTACT,
|
|
183
175
|
)
|
|
184
176
|
})
|
|
185
177
|
|
|
186
178
|
it('falls back to UNKNOWN_MESSAGE_TYPE', () => {
|
|
187
|
-
const
|
|
188
|
-
expect(getTelegramMessageType({
|
|
179
|
+
const imMessage = { message: { something_else: 'x' } }
|
|
180
|
+
expect(getTelegramMessageType({ imMessage })).toBe(
|
|
189
181
|
MESSAGE_TYPE.UNKNOWN_MESSAGE_TYPE,
|
|
190
182
|
)
|
|
191
183
|
})
|
|
@@ -31,10 +31,10 @@ describe('Telegram unified message mapper – all mock samples', () => {
|
|
|
31
31
|
|
|
32
32
|
describe(`Message mock: ${file}`, () => {
|
|
33
33
|
it('should map type correctly', () => {
|
|
34
|
-
const unifiedType = getTelegramMessageType({
|
|
34
|
+
const unifiedType = getTelegramMessageType({ imMessage: raw })
|
|
35
35
|
|
|
36
36
|
const unifiedMessage = mapMessageTelegram({
|
|
37
|
-
|
|
37
|
+
imMessage: raw,
|
|
38
38
|
})
|
|
39
39
|
|
|
40
40
|
expect(unifiedMessage).toBeTypeOf('object')
|
|
@@ -48,7 +48,7 @@ describe('Telegram unified message mapper – all mock samples', () => {
|
|
|
48
48
|
})
|
|
49
49
|
|
|
50
50
|
it('should include mandatory unified fields', () => {
|
|
51
|
-
const unified = mapMessageTelegram({
|
|
51
|
+
const unified = mapMessageTelegram({ imMessage: raw })
|
|
52
52
|
|
|
53
53
|
expect(unified).toHaveProperty('id')
|
|
54
54
|
expect(unified).toHaveProperty('chatId')
|
|
@@ -66,7 +66,7 @@ describe('Unified message mapper – all platforms, all mock samples', () => {
|
|
|
66
66
|
const mapper = messageUnifiedMapper[application]
|
|
67
67
|
expect(mapper).toBeTypeOf('function')
|
|
68
68
|
|
|
69
|
-
const unified = mapper({
|
|
69
|
+
const unified = mapper({ imMessage: raw })
|
|
70
70
|
|
|
71
71
|
expect(unified).toBeTypeOf('object')
|
|
72
72
|
expect(unified).toHaveProperty('id')
|
|
@@ -8,7 +8,10 @@ import {
|
|
|
8
8
|
mapMessageWhatsApp,
|
|
9
9
|
} from '../../src/instant-messages/message-unified-mapper.js'
|
|
10
10
|
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
MESSAGE_MEDIA_TYPE,
|
|
13
|
+
MESSAGE_MEDIA_TYPE_MAPPER,
|
|
14
|
+
} from '../../src/instant-messages/message-types.js'
|
|
12
15
|
const __filename = fileURLToPath(import.meta.url)
|
|
13
16
|
const __dirname = dirname(__filename)
|
|
14
17
|
|
|
@@ -30,24 +33,26 @@ describe('WhatsApp unified message mapper – all mock samples', () => {
|
|
|
30
33
|
describe(`Message mock: ${file}`, () => {
|
|
31
34
|
it('should map type correctly', () => {
|
|
32
35
|
console.log(file)
|
|
33
|
-
const unifiedType = getMessageType({
|
|
36
|
+
const unifiedType = getMessageType({ imMessage: raw })
|
|
34
37
|
|
|
35
38
|
const unifiedMessage = mapMessageWhatsApp({
|
|
36
|
-
|
|
39
|
+
imMessage: raw,
|
|
37
40
|
})
|
|
38
41
|
|
|
39
42
|
expect(unifiedMessage).toBeTypeOf('object')
|
|
40
43
|
expect(unifiedType).toBeTypeOf('string')
|
|
41
44
|
|
|
42
45
|
// Type must match mapper result
|
|
43
|
-
expect(unifiedMessage.type).toBe(
|
|
46
|
+
expect(unifiedMessage.type).toBe(
|
|
47
|
+
MESSAGE_MEDIA_TYPE_MAPPER[unifiedType] || unifiedType,
|
|
48
|
+
)
|
|
44
49
|
|
|
45
50
|
// All unified types must be supported values
|
|
46
51
|
expect(Object.values(MESSAGE_MEDIA_TYPE)).toContain(unifiedType)
|
|
47
52
|
})
|
|
48
53
|
|
|
49
54
|
it('should include mandatory unified fields', () => {
|
|
50
|
-
const unified = mapMessageWhatsApp({
|
|
55
|
+
const unified = mapMessageWhatsApp({ imMessage: raw })
|
|
51
56
|
|
|
52
57
|
expect(unified).toHaveProperty('id')
|
|
53
58
|
expect(unified).toHaveProperty('chatId')
|
package/types/ids/prefixes.d.ts
CHANGED
|
@@ -1,28 +1,24 @@
|
|
|
1
1
|
export function isItMediaType(
|
|
2
2
|
mediaType: string,
|
|
3
|
-
): (params: {
|
|
3
|
+
): (params: { imMessage: any }) => boolean
|
|
4
4
|
export function isMessageTypeof(
|
|
5
5
|
typeOfMessage: string,
|
|
6
|
-
): (params: {
|
|
7
|
-
export function isCallbackQuery({
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
})
|
|
12
|
-
export const
|
|
13
|
-
export const
|
|
14
|
-
export const
|
|
15
|
-
export const
|
|
16
|
-
export const
|
|
17
|
-
export const
|
|
18
|
-
export const
|
|
19
|
-
export const
|
|
20
|
-
export const isItLocation: (params: { originalMessage: any }) => boolean
|
|
21
|
-
export const isItDocument: (params: { originalMessage: any }) => boolean
|
|
22
|
-
export const isItVideoNote: (params: { originalMessage: any }) => boolean
|
|
23
|
-
export const isItButtonClick: (params: { originalMessage: any }) => boolean
|
|
6
|
+
): (params: { imMessage: any }) => boolean
|
|
7
|
+
export function isCallbackQuery({ imMessage }: { imMessage: any }): boolean
|
|
8
|
+
export const isItPoll: (params: { imMessage: any }) => boolean
|
|
9
|
+
export const isItMessage: (params: { imMessage: any }) => boolean
|
|
10
|
+
export const isItVoice: (params: { imMessage: any }) => boolean
|
|
11
|
+
export const isItVideo: (params: { imMessage: any }) => boolean
|
|
12
|
+
export const isItPhoto: (params: { imMessage: any }) => boolean
|
|
13
|
+
export const isItFreeText: (params: { imMessage: any }) => boolean
|
|
14
|
+
export const isItSticker: (params: { imMessage: any }) => boolean
|
|
15
|
+
export const isItContact: (params: { imMessage: any }) => boolean
|
|
16
|
+
export const isItLocation: (params: { imMessage: any }) => boolean
|
|
17
|
+
export const isItDocument: (params: { imMessage: any }) => boolean
|
|
18
|
+
export const isItVideoNote: (params: { imMessage: any }) => boolean
|
|
19
|
+
export const isItButtonClick: (params: { imMessage: any }) => boolean
|
|
24
20
|
export function getTelegramMessageType({
|
|
25
|
-
|
|
21
|
+
imMessage,
|
|
26
22
|
}: {
|
|
27
|
-
|
|
23
|
+
imMessage: any
|
|
28
24
|
}): string
|
|
@@ -1,13 +1,5 @@
|
|
|
1
|
-
export function getMessageType({
|
|
2
|
-
|
|
3
|
-
}: {
|
|
4
|
-
originalMessage: any
|
|
5
|
-
}): string
|
|
6
|
-
export function mapMessageTelegramBase({
|
|
7
|
-
originalMessage,
|
|
8
|
-
}: {
|
|
9
|
-
originalMessage: any
|
|
10
|
-
}): {
|
|
1
|
+
export function getMessageType({ imMessage }: { imMessage: any }): string
|
|
2
|
+
export function mapMessageTelegramBase({ imMessage }: { imMessage: any }): {
|
|
11
3
|
messageBase: {
|
|
12
4
|
timestamp: string
|
|
13
5
|
forwardInfo: {
|
|
@@ -46,12 +38,9 @@ export function mapMessageWhatsAppContent({
|
|
|
46
38
|
reply: any
|
|
47
39
|
text?: undefined
|
|
48
40
|
}
|
|
49
|
-
export function mapMessageTelegram({
|
|
50
|
-
originalMessage,
|
|
51
|
-
}: {
|
|
52
|
-
originalMessage: any
|
|
53
|
-
}):
|
|
41
|
+
export function mapMessageTelegram({ imMessage }: { imMessage: any }):
|
|
54
42
|
| {
|
|
43
|
+
type: string
|
|
55
44
|
text: any
|
|
56
45
|
reply?: undefined
|
|
57
46
|
timestamp: string
|
|
@@ -64,11 +53,11 @@ export function mapMessageTelegram({
|
|
|
64
53
|
tmId: any
|
|
65
54
|
}
|
|
66
55
|
chatId: any
|
|
67
|
-
type: string
|
|
68
56
|
chatter: any
|
|
69
57
|
itIsForward: boolean
|
|
70
58
|
}
|
|
71
59
|
| {
|
|
60
|
+
type: string
|
|
72
61
|
text?: undefined
|
|
73
62
|
reply?: undefined
|
|
74
63
|
timestamp: string
|
|
@@ -81,11 +70,11 @@ export function mapMessageTelegram({
|
|
|
81
70
|
tmId: any
|
|
82
71
|
}
|
|
83
72
|
chatId: any
|
|
84
|
-
type: string
|
|
85
73
|
chatter: any
|
|
86
74
|
itIsForward: boolean
|
|
87
75
|
}
|
|
88
76
|
| {
|
|
77
|
+
type: string
|
|
89
78
|
attachment: string
|
|
90
79
|
animation: any
|
|
91
80
|
text?: undefined
|
|
@@ -100,11 +89,11 @@ export function mapMessageTelegram({
|
|
|
100
89
|
tmId: any
|
|
101
90
|
}
|
|
102
91
|
chatId: any
|
|
103
|
-
type: string
|
|
104
92
|
chatter: any
|
|
105
93
|
itIsForward: boolean
|
|
106
94
|
}
|
|
107
95
|
| {
|
|
96
|
+
type: string
|
|
108
97
|
reply: {
|
|
109
98
|
id: any
|
|
110
99
|
title: any
|
|
@@ -120,29 +109,20 @@ export function mapMessageTelegram({
|
|
|
120
109
|
tmId: any
|
|
121
110
|
}
|
|
122
111
|
chatId: any
|
|
123
|
-
type: string
|
|
124
112
|
chatter: any
|
|
125
113
|
itIsForward: boolean
|
|
126
114
|
}
|
|
127
115
|
export function getWhatsAppMessageType({ message }: { message: any }): any
|
|
128
|
-
export function extractReply({
|
|
116
|
+
export function extractReply({ imMessage }: { imMessage: any }): {
|
|
129
117
|
id: any
|
|
130
118
|
title: any
|
|
131
119
|
}
|
|
132
|
-
export function whatsappBaseExtraction({
|
|
133
|
-
originalMessage,
|
|
134
|
-
}: {
|
|
135
|
-
originalMessage: any
|
|
136
|
-
}): {
|
|
120
|
+
export function whatsappBaseExtraction({ imMessage }: { imMessage: any }): {
|
|
137
121
|
field: any
|
|
138
122
|
value: any
|
|
139
123
|
wbaid: any
|
|
140
124
|
}
|
|
141
|
-
export function mapMessageWhatsAppBase({
|
|
142
|
-
originalMessage,
|
|
143
|
-
}: {
|
|
144
|
-
originalMessage: any
|
|
145
|
-
}): {
|
|
125
|
+
export function mapMessageWhatsAppBase({ imMessage }: { imMessage: any }): {
|
|
146
126
|
messageBase: {
|
|
147
127
|
id: any
|
|
148
128
|
chatId: any
|
|
@@ -165,11 +145,11 @@ export function mapMessageWhatsAppBase({
|
|
|
165
145
|
export function mapMessageTelegramContent({
|
|
166
146
|
type,
|
|
167
147
|
message,
|
|
168
|
-
|
|
148
|
+
imMessage,
|
|
169
149
|
}: {
|
|
170
150
|
type: any
|
|
171
151
|
message: any
|
|
172
|
-
|
|
152
|
+
imMessage: any
|
|
173
153
|
}):
|
|
174
154
|
| {
|
|
175
155
|
text: any
|
|
@@ -193,12 +173,9 @@ export function mapMessageTelegramContent({
|
|
|
193
173
|
}
|
|
194
174
|
text?: undefined
|
|
195
175
|
}
|
|
196
|
-
export function mapMessageWhatsApp({
|
|
197
|
-
originalMessage,
|
|
198
|
-
}: {
|
|
199
|
-
originalMessage: any
|
|
200
|
-
}):
|
|
176
|
+
export function mapMessageWhatsApp({ imMessage }: { imMessage: any }):
|
|
201
177
|
| {
|
|
178
|
+
type: any
|
|
202
179
|
text: any
|
|
203
180
|
reply?: undefined
|
|
204
181
|
id: any
|
|
@@ -206,7 +183,6 @@ export function mapMessageWhatsApp({
|
|
|
206
183
|
imExtraInfo: {
|
|
207
184
|
wbaid: any
|
|
208
185
|
}
|
|
209
|
-
type: any
|
|
210
186
|
chatter: {
|
|
211
187
|
id: any
|
|
212
188
|
name: any
|
|
@@ -216,6 +192,7 @@ export function mapMessageWhatsApp({
|
|
|
216
192
|
timestamp: any
|
|
217
193
|
}
|
|
218
194
|
| {
|
|
195
|
+
type: any
|
|
219
196
|
text?: undefined
|
|
220
197
|
reply?: undefined
|
|
221
198
|
id: any
|
|
@@ -223,7 +200,6 @@ export function mapMessageWhatsApp({
|
|
|
223
200
|
imExtraInfo: {
|
|
224
201
|
wbaid: any
|
|
225
202
|
}
|
|
226
|
-
type: any
|
|
227
203
|
chatter: {
|
|
228
204
|
id: any
|
|
229
205
|
name: any
|
|
@@ -233,6 +209,7 @@ export function mapMessageWhatsApp({
|
|
|
233
209
|
timestamp: any
|
|
234
210
|
}
|
|
235
211
|
| {
|
|
212
|
+
type: any
|
|
236
213
|
reply: any
|
|
237
214
|
text?: undefined
|
|
238
215
|
id: any
|
|
@@ -240,7 +217,6 @@ export function mapMessageWhatsApp({
|
|
|
240
217
|
imExtraInfo: {
|
|
241
218
|
wbaid: any
|
|
242
219
|
}
|
|
243
|
-
type: any
|
|
244
220
|
chatter: {
|
|
245
221
|
id: any
|
|
246
222
|
name: any
|
|
@@ -250,8 +226,9 @@ export function mapMessageWhatsApp({
|
|
|
250
226
|
timestamp: any
|
|
251
227
|
}
|
|
252
228
|
export const messageUnifiedMapper: {
|
|
253
|
-
[IM_PLATFORM.TELEGRAM]: ({
|
|
229
|
+
[IM_PLATFORM.TELEGRAM]: ({ imMessage }: { imMessage: any }) =>
|
|
254
230
|
| {
|
|
231
|
+
type: string
|
|
255
232
|
text: any
|
|
256
233
|
reply?: undefined
|
|
257
234
|
timestamp: string
|
|
@@ -264,11 +241,11 @@ export const messageUnifiedMapper: {
|
|
|
264
241
|
tmId: any
|
|
265
242
|
}
|
|
266
243
|
chatId: any
|
|
267
|
-
type: string
|
|
268
244
|
chatter: any
|
|
269
245
|
itIsForward: boolean
|
|
270
246
|
}
|
|
271
247
|
| {
|
|
248
|
+
type: string
|
|
272
249
|
text?: undefined
|
|
273
250
|
reply?: undefined
|
|
274
251
|
timestamp: string
|
|
@@ -281,11 +258,11 @@ export const messageUnifiedMapper: {
|
|
|
281
258
|
tmId: any
|
|
282
259
|
}
|
|
283
260
|
chatId: any
|
|
284
|
-
type: string
|
|
285
261
|
chatter: any
|
|
286
262
|
itIsForward: boolean
|
|
287
263
|
}
|
|
288
264
|
| {
|
|
265
|
+
type: string
|
|
289
266
|
attachment: string
|
|
290
267
|
animation: any
|
|
291
268
|
text?: undefined
|
|
@@ -300,11 +277,11 @@ export const messageUnifiedMapper: {
|
|
|
300
277
|
tmId: any
|
|
301
278
|
}
|
|
302
279
|
chatId: any
|
|
303
|
-
type: string
|
|
304
280
|
chatter: any
|
|
305
281
|
itIsForward: boolean
|
|
306
282
|
}
|
|
307
283
|
| {
|
|
284
|
+
type: string
|
|
308
285
|
reply: {
|
|
309
286
|
id: any
|
|
310
287
|
title: any
|
|
@@ -320,12 +297,12 @@ export const messageUnifiedMapper: {
|
|
|
320
297
|
tmId: any
|
|
321
298
|
}
|
|
322
299
|
chatId: any
|
|
323
|
-
type: string
|
|
324
300
|
chatter: any
|
|
325
301
|
itIsForward: boolean
|
|
326
302
|
}
|
|
327
|
-
[IM_PLATFORM.WHATSAPP]: ({
|
|
303
|
+
[IM_PLATFORM.WHATSAPP]: ({ imMessage }: { imMessage: any }) =>
|
|
328
304
|
| {
|
|
305
|
+
type: any
|
|
329
306
|
text: any
|
|
330
307
|
reply?: undefined
|
|
331
308
|
id: any
|
|
@@ -333,7 +310,6 @@ export const messageUnifiedMapper: {
|
|
|
333
310
|
imExtraInfo: {
|
|
334
311
|
wbaid: any
|
|
335
312
|
}
|
|
336
|
-
type: any
|
|
337
313
|
chatter: {
|
|
338
314
|
id: any
|
|
339
315
|
name: any
|
|
@@ -343,6 +319,7 @@ export const messageUnifiedMapper: {
|
|
|
343
319
|
timestamp: any
|
|
344
320
|
}
|
|
345
321
|
| {
|
|
322
|
+
type: any
|
|
346
323
|
text?: undefined
|
|
347
324
|
reply?: undefined
|
|
348
325
|
id: any
|
|
@@ -350,7 +327,6 @@ export const messageUnifiedMapper: {
|
|
|
350
327
|
imExtraInfo: {
|
|
351
328
|
wbaid: any
|
|
352
329
|
}
|
|
353
|
-
type: any
|
|
354
330
|
chatter: {
|
|
355
331
|
id: any
|
|
356
332
|
name: any
|
|
@@ -360,6 +336,7 @@ export const messageUnifiedMapper: {
|
|
|
360
336
|
timestamp: any
|
|
361
337
|
}
|
|
362
338
|
| {
|
|
339
|
+
type: any
|
|
363
340
|
reply: any
|
|
364
341
|
text?: undefined
|
|
365
342
|
id: any
|
|
@@ -367,7 +344,6 @@ export const messageUnifiedMapper: {
|
|
|
367
344
|
imExtraInfo: {
|
|
368
345
|
wbaid: any
|
|
369
346
|
}
|
|
370
|
-
type: any
|
|
371
347
|
chatter: {
|
|
372
348
|
id: any
|
|
373
349
|
name: any
|