Skip to content

Commit 8435924

Browse files
authored
Merge pull request #9 from Secreto31126/statuses
Added statuses updates via POST support
2 parents c37df61 + f7c120a commit 8435924

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "whatsapp-api-js",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"description": "A Whatsapp Official API helper for Node.js",
55
"main": "index.js",
66
"scripts": {

requests.js

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* GET helper, must be called inside the get function of your code.
33
* Used once at the first webhook setup.
44
*
5-
* @ignore
65
* @param {Object} params The GET request parameters in object format
76
* @param {String} verify_token The verification token
87
* @returns {String} The challenge string, it must be the http response body
@@ -28,41 +27,67 @@ function get(params, verify_token) {
2827
}
2928

3029
/**
31-
* POST helper callback
30+
* POST helper callback for messages
3231
*
33-
* @callback postCallback
32+
* @callback onMessage
3433
* @param {String} phoneID The bot's phoneID
3534
* @param {String} phone The user's phone number
3635
* @param {Object} message The messages object
3736
* @param {String} name The username
3837
* @param {Object} raw The raw data from the API
3938
*/
4039

40+
/**
41+
* POST helper callback for statuses
42+
*
43+
* @callback onStatus
44+
* @param {String} phoneID The bot's phoneID
45+
* @param {String} phone The user's phone number
46+
* @param {String} status The message status
47+
* @param {String} messageID The message ID
48+
* @param {Object} conversation The conversation object
49+
* @param {Object} pricing The pricing object
50+
* @param {Object} raw The raw data from the API
51+
*/
52+
4153
/**
4254
* POST helper, must be called inside the post function of your code.
4355
* When setting up the webhook, only subscribe to messages. Other subscritions support might be added later.
4456
*
45-
* @ignore
4657
* @param {Object} data The post data sent by Whatsapp, already parsed to object
47-
* @param {postCallback} callback The function to be called if the post request is valid
58+
* @param {onMessage} onMessage The function to be called if the post request is a valid message
59+
* @param {onStatus} [onStatus] The function to be called if the post request is a valid status update
4860
* @returns {Number} 200, it's the expected http/s response code
4961
* @throws {Number} 400 if the POST request isn't valid
5062
*/
51-
function post(data, callback) {
63+
function post(data, onMessage, onStatus) {
5264
// Validate the webhook
5365
if (data.object) {
5466
const value = data.entry[0].changes[0].value;
55-
5667
const phoneID = value.metadata.phone_number_id;
5768

58-
const contact = value.contacts[0];
69+
// Check if the message is a message
70+
if (value.message) {
71+
const contact = value.contacts[0];
5972

60-
const phone = contact.wa_id;
61-
const name = contact.profile.name;
73+
const phone = contact.wa_id;
74+
const name = contact.profile.name;
6275

63-
const message = value.messages[0];
76+
const message = value.messages[0];
6477

65-
callback(phoneID, phone, message, name, data);
78+
onMessage(phoneID, phone, message, name, data);
79+
} else if (value.statuses && onStatus) {
80+
const statuses = value.statuses[0];
81+
82+
const phone = statuses.recipient_id;
83+
const status = statuses.status;
84+
const messageID = statuses.id;
85+
86+
const conversation = value.conversation;
87+
const pricing = value.pricing;
88+
89+
onStatus(phoneID, phone, status, messageID, conversation, pricing, data);
90+
}
6691

6792
return 200;
6893
} else {

0 commit comments

Comments
 (0)