2
2
* GET helper, must be called inside the get function of your code.
3
3
* Used once at the first webhook setup.
4
4
*
5
- * @ignore
6
5
* @param {Object } params The GET request parameters in object format
7
6
* @param {String } verify_token The verification token
8
7
* @returns {String } The challenge string, it must be the http response body
@@ -28,41 +27,67 @@ function get(params, verify_token) {
28
27
}
29
28
30
29
/**
31
- * POST helper callback
30
+ * POST helper callback for messages
32
31
*
33
- * @callback postCallback
32
+ * @callback onMessage
34
33
* @param {String } phoneID The bot's phoneID
35
34
* @param {String } phone The user's phone number
36
35
* @param {Object } message The messages object
37
36
* @param {String } name The username
38
37
* @param {Object } raw The raw data from the API
39
38
*/
40
39
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
+
41
53
/**
42
54
* POST helper, must be called inside the post function of your code.
43
55
* When setting up the webhook, only subscribe to messages. Other subscritions support might be added later.
44
56
*
45
- * @ignore
46
57
* @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
48
60
* @returns {Number } 200, it's the expected http/s response code
49
61
* @throws {Number } 400 if the POST request isn't valid
50
62
*/
51
- function post ( data , callback ) {
63
+ function post ( data , onMessage , onStatus ) {
52
64
// Validate the webhook
53
65
if ( data . object ) {
54
66
const value = data . entry [ 0 ] . changes [ 0 ] . value ;
55
-
56
67
const phoneID = value . metadata . phone_number_id ;
57
68
58
- const contact = value . contacts [ 0 ] ;
69
+ // Check if the message is a message
70
+ if ( value . message ) {
71
+ const contact = value . contacts [ 0 ] ;
59
72
60
- const phone = contact . wa_id ;
61
- const name = contact . profile . name ;
73
+ const phone = contact . wa_id ;
74
+ const name = contact . profile . name ;
62
75
63
- const message = value . messages [ 0 ] ;
76
+ const message = value . messages [ 0 ] ;
64
77
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
+ }
66
91
67
92
return 200 ;
68
93
} else {
0 commit comments