-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
msg_parser.js
155 lines (138 loc) · 5.3 KB
/
msg_parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
'use strict';
module.exports = ({ requestBody, currentWABA_ID }) => {
// THIS FUNCTION IS NOT YET OPTIMIZED FOR PERFORMANCE. IT IS ONLY MADE AS A TEMPORARY SOLUTION.
if (!requestBody) {
throw new Error('"requestBody" is required');
}
if (!currentWABA_ID) {
throw new Error(
'currentWABA_ID is required. This is the business ID that you have configured in your WABA account.'
);
}
let WABA_ID = requestBody.entry[0]?.id; // extract the business ID from the webhook payload
if (WABA_ID == 0) {
signale.warn({
message: `WABA_ID is 0. You seem to be testing with Meta test subscription. This is not really a valid WABA_ID. I recommend you to send an actual message from an actual whatsapp customer's number.`,
});
}
if (!WABA_ID || WABA_ID !== currentWABA_ID) {
throw new Error(
'WABA_ID is not valid. Hint: the message is not intended for this Whatsapp Business Account.'
);
}
//first check if the message is a whatsapp message
if (
!requestBody.object ||
requestBody.object !== 'whatsapp_business_account'
) {
throw new Error(
'requestBody is not a valid whatsapp message. Hint: check the "object" property'
);
}
if (!requestBody.entry || !requestBody.entry?.length) {
throw new Error(
'requestBody is not a valid whatsapp message. Hint: check the "entry" property'
);
}
if (
!requestBody.entry[0].changes?.length ||
requestBody.entry[0].changes[0].field !== 'messages'
) {
throw new Error(
'requestBody is not a valid whatsapp message. Hint: check the "changes" property'
);
}
let metadata = requestBody.entry[0].changes[0].value.metadata;
let contacts = requestBody.entry[0].changes[0].value.contacts?.length
? requestBody.entry[0].changes[0].value.contacts[0]
: null;
// Messages vs Notifications
let message = requestBody.entry[0].changes[0].value?.messages?.length
? requestBody.entry[0].changes[0].value.messages[0]
: null;
let notificationMessage = requestBody.entry[0].changes[0].value?.statuses
?.length
? requestBody.entry[0].changes[0].value.statuses[0]
: null;
const output = {
metadata,
contacts,
WABA_ID,
};
if (notificationMessage) {
output['isNotificationMessage'] = true;
output['isMessage'] = false;
output['notificationType'] = notificationMessage.status;
notificationMessage['from'] = {
name: null, //name is not available for notifications, it is only available for messages
phone: notificationMessage.recipient_id,
};
output['notificationMessage'] = notificationMessage;
} else if (message) {
output['isNotificationMessage'] = false;
output['isMessage'] = true;
let msgType;
if (message.type === 'text' && message.referral) {
msgType = 'ad_message';
} else if (message.type === 'text') {
msgType = 'text_message';
} else if (message.type === 'audio') {
msgType = 'audio_message';
} else if (message.type === 'sticker') {
msgType = 'sticker_message';
} else if (message.type === 'image') {
msgType = 'media_message';
} else if (message.location) {
msgType = 'location_message';
} else if (message.contacts) {
msgType = 'contact_message';
} else if (message.type === 'button') {
msgType = 'quick_reply_message';
} else if (message.type === 'interactive') {
if (message.interactive?.type === 'list_reply') {
msgType = 'radio_button_message';
message['list_reply'] = message.interactive.list_reply;
} else if (message.interactive?.type === 'button_reply') {
msgType = 'simple_button_message';
message['button_reply'] = message.interactive.button_reply;
}
} else if (message.type === 'unsupported') {
msgType = 'unknown_message';
if (message.errors?.length) {
console.log({ Q: message.errors });
output['isNotificationMessage'] = true;
output['isMessage'] = false;
notificationMessage = {
errors: message.errors,
};
}
}
message['type'] = msgType;
message['from'] = {
name: contacts.profile.name,
phone: message?.from,
};
if (output.isMessage) {
let thread = null;
if (message.context) {
thread = {
from: {
name: null,
phone: message.context?.from,
message_id: message.context?.id,
},
};
}
output['message'] = {
...message,
thread,
message_id: message.id || null,
};
delete output.message.id; //keep the data light
delete output.context; //keep the data light
}
} else {
console.warn('An unidentified.');
}
return output;
};