-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathmessageHelper.js
More file actions
167 lines (147 loc) · 4.83 KB
/
messageHelper.js
File metadata and controls
167 lines (147 loc) · 4.83 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
/* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/
const axios = require('axios');
const { messageTemplates } = require('./public/javascripts/messageTemplates')
const { products } = require('./public/javascripts/products')
const accessToken = process.env.ACCESS_TOKEN;
const apiVersion = process.env.VERSION;
const recipientNumber = process.env.RECIPIENT_PHONE_NUMBER;
const myNumberId = process.env.PHONE_NUMBER_ID;
const myBizAcctId = process.env.BUSINESS_ACCOUNT_ID;
async function sendWhatsAppMessage(data) {
const config = {
method: 'post',
url: `https://graph.facebook.com/${apiVersion}/${myNumberId}/messages`,
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
},
data: data
};
return await axios(config)
}
async function updateWhatsAppMessage(data) {
const config = {
method: 'put',
url: `https://graph.facebook.com/${apiVersion}/${myNumberId}/messages`,
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
},
data: data
};
return await axios(config)
}
function createProductsList(product) {
return {
"id": `${product.id}`,
"title": product.name,
"description": `$${product.price}`
}
}
function getMessageData(recipient, order) {
const messageTemplate = messageTemplates[order.statusId - 1]
let messageParameters
switch (messageTemplate.name) {
case 'welcome':
messageParameters = [
{ type: "text", text: order.customer.split(' ')[0] },
];
break;
case 'payment_analysis':
messageParameters = [
{ type: "text", text: order.customer.split(' ')[0] },
{ type: "text", text: products[order.items[0].productId - 1].name },
];
break;
case 'payment_approved':
messageParameters = [
{ type: "text", text: order.customer.split(' ')[0] },
{ type: "text", text: order.id },
{ type: "text", text: order.deliveryDate },
];
break;
case 'invoice_available':
messageParameters = [
{ type: "text", text: order.customer.split(' ')[0] },
{ type: "text", text: products[order.items[0].productId - 1].name },
{ type: "text", text: `https://customer.your-awesome-grocery-store-demo.com/my-account/orders/${order.id}` },
];
break;
case 'order_picked_packed':
messageParameters = [
{ type: "text", text: order.customer.split(' ')[0] },
{ type: "text", text: order.id },
{ type: "text", text: `https://customer.your-awesome-grocery-store-demo.com/my-account/orders/${order.id}` },
];
break;
case 'order_in_transit':
messageParameters = [
{ type: "text", text: order.customer.split(' ')[0] },
{ type: "text", text: order.id },
{ type: "text", text: order.deliveryDate },
{ type: "text", text: `https://customer.your-awesome-grocery-store-demo.com/my-account/orders/${order.id}` },
];
break;
case 'order_delivered':
messageParameters = [
{ type: "text", text: order.customer.split(' ')[0] },
{ type: "text", text: order.id },
{ type: "text", text: order.deadlineDays },
];
break;
}
const messageData = {
messaging_product: "whatsapp",
to: recipient,
type: "template",
template: {
name: process.env.TEMPLATE_NAME_PREFIX + '_' + messageTemplate.name,
language: { "code": "en_US" },
components: [{
type: "body",
parameters: messageParameters
}]}}
return JSON.stringify(messageData);
}
async function listTemplates() {
return await axios({
method: 'get',
url: `https://graph.facebook.com/${apiVersion}/${myBizAcctId}/message_templates`
+ '?limit=1000',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
}
})
}
async function createMessageTemplate(template) {
console.log('name:' + process.env.TEMPLATE_NAME_PREFIX + '_' + template.name);
const config = {
method: 'post',
url: `https://graph.facebook.com/${apiVersion}/${myBizAcctId}/message_templates`,
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
},
data: {
name: process.env.TEMPLATE_NAME_PREFIX + '_' + template.name,
category: template.category,
components: template.components,
language: template.language
}
};
return await axios(config)
}
module.exports = {
sendWhatsAppMessage: sendWhatsAppMessage,
updateWhatsAppMessage: updateWhatsAppMessage,
listTemplates: listTemplates,
createMessageTemplate: createMessageTemplate,
getMessageData: getMessageData,
createProductsList: createProductsList
};