-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
323 lines (269 loc) · 10.5 KB
/
app.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
const express = require('express');
const axios = require('axios');
const qs = require('qs'); // qs is a querystring parsing library
const app = express();
const port = 3010;
app.use(express.json());
// Middleware to log all POST requests
// app.use((req, res, next) => {
// console.log(req.method);
// console.log(req.query);
// if (req.method === 'GET') {
// console.log(`Received POST request to ${req.query}`);
// console.log('Body:', req.query);
// }
// next(); // Proceed to the next middleware or route handler
// });
// app.use((req, res, next) => {
// console.log(req.method);
// // Correctly handle logging objects by using JSON.stringify
// if (req.method === 'GET') {
// console.log(`Received GET request to ${req.path}`);
// console.log('Query:', JSON.stringify(req.query)); // Use JSON.stringify to safely log the object
// } else if (req.method === 'POST') {
// console.log(`Received POST request to ${req.path}`);
// // Assuming you want to log the body for POST requests
// console.log('Body:', JSON.stringify(req.body)); // Similarly, use JSON.stringify for the body
// }
// next(); // Proceed to the next middleware or route handler
// });
// app.post('/sendTemplateMessage', async (req, res) => {
// const { data } = req.body;
// const templatename = data.elementName;
// const receivers = data.Contact_phone_number; // Assuming single receiver for simplicity
// // Parse the Message_Parameters string into an object
// const parsedParams = qs.parse(data.Message_Parameters);
// // Construct the customParams array from customParamsData
// const customParams = Object.keys(parsedParams).map(key => ({
// name: key,
// value: parsedParams[key]
// }));
// console.log(customParams);
// const requestData = {
// template_name: templatename,
// broadcast_name: "string", // This can be dynamically set based on your needs
// receivers: receivers.map(whatsappNumber => ({
// receivers, // Corrected use of whatsappNumber
// customParams
// }))
// };
// try {
// const response = await axios.post('https://live-mt-server.wati.io/Your Tanent ID/api/v2/sendTemplateMessages', requestData, {
// headers: {
// 'accept': '*/*',
// 'Authorization': 'Bearer YOUR_ACTUAL_TOKEN',
// 'Content-Type': 'application/json-patch+json'
// }
// });
// res.json({ success: true, response: response.data });
// } catch (error) {
// res.status(500).json({ success: false, error: error.message });
// }
// });
app.post('/sendTemplateMessage', async (req, res) => {
// const { data } = req.body;
// const templatename = data.elementName;
// const receivers = [data.Contact_phone_number]; // Now it's an array
// var customParams = data.Message_Parameters.split('&').map(param => {
// // return param; // This already is "name=Haris", "tap=wertyu"
// const [key, value] = param.split('='); // Split each param into key and value
// return { [key]: value };
// });
// //console.log(customParams);
// customParams=JSON.stringify(customParams);
// //console.log(customParams);
// var requestData = {
// template_name: templatename,
// broadcast_name: "string",
// // Correctly construct the receivers array for the request
// receivers: receivers.map(whatsappNumber => ({
// whatsappNumber, // Use this variable correctly
// customParams // This already is an array of objects {name, value}
// }))
// };
// //requestData==JSON.stringify(requestData);
// console.log(requestData);
const { data } = req.body;
const templatename = data.elementName;
const receivers = [data.Contact_phone_number];
const wai=data.Contact_phone_number;
// var customParams = data.Message_Parameters.split('&').map(param => {
// const [key, value] = param.split('=');
// return { [key]: value };
// });
// var customParams = data.Message_Parameters.split('&').reduce((acc, param) => {
// const [key, value] = param.split('=');
// acc[key] = value; // Assign each key-value pair to the accumulator object
// return acc;
// }, {});
var customParams = data.Message_Parameters.split('||>').map(param => {
const [key, value] = param.split('=');
return { name: key, value: value }; // This aligns with the desired output
});
var requestData = {
template_name: templatename,
broadcast_name: "string",
receivers: receivers.map(whatsappNumber => ({
whatsappNumber,
customParams
}))
};
// console.log(requestData);
requestData=JSON.stringify(requestData, null, 2);
try {
const response = await axios.post('https://live-mt-server.wati.io/Your Tanent ID/api/v2/sendTemplateMessages', requestData, {
headers: {
'accept': '*/*',
'Authorization': 'Your Bearer Token', // Make sure to replace with your actual token
'Content-Type': 'application/json-patch+json'
}
});
console.log(requestData);
// console.log(response.data)
console.log("This COde Runs Perfectly");
res.json({ success: true, response: response.data });
} catch (error) {
console.log(error.message);
// console.log(requestData);
res.status(500).json({ success: false, error: error.message });
}
});
app.get('/sendTempMessage', async (req, res) => {
const { tenant,templatename,receivers, ...queryParams } = req.query;
const whatsappNumber = receivers; // Assuming `receivers` is a single WhatsApp number for simplicity
// Dynamically generate customParams from queryParams
const customParams = Object.entries(queryParams).map(([name, value]) => ({
name,
value
}));
const data = {
template_name: templatename,
broadcast_name: "string",
receivers: [
{
whatsappNumber,
customParams
}
]
};
try {
const response = await axios.post('https://live-mt-server.wati.io/Your Tanent ID/api/v2/sendTemplateMessages', data, {
headers: {
'accept': '*/*',
'Authorization': 'Your Bearer Token', // Replace YOUR_TOKEN with your actual token
'Content-Type': 'application/json-patch+json'
}
});
res.json({ success: true, response: response.data });
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
});
// New endpoint for adding a contact
app.get('/addContact', async (req, res) => {
// Extract 'name' and 'number' from query parameters
const { tenant,name, number, ...customParamsQuery } = req.query;
// Dynamically generate customParams from the remaining query parameters
const customParams = Object.entries(customParamsQuery).map(([name, value]) => ({
name,
value
}));
const data = {
name: name, // Use the 'name' from the query
customParams: customParams
};
// Replace 'YOUR_TOKEN' with your actual authorization token
const config = {
headers: {
'accept': '*/*',
'Authorization': 'Your Bearer Token',
'Content-Type': 'application/json-patch+json'
}
};
try {
// The 'number' from the query is appended to the URL
const response = await axios.post(`https://live-mt-server.wati.io/Your Tanent ID/api/v1/addContact/${number}`, data, config);
res.json({ success: true, response: response.data });
} catch (error) {
console.error(error);
res.status(500).json({ success: false, error: error.message });
}
});
// New endpoint for assigning an operator
app.get('/assignOperator', async (req, res) => {
const { tenant,email, whatsappNumber } = req.query;
// Ensure both email and whatsappNumber are provided
if (!email || !whatsappNumber) {
return res.status(400).json({ success: false, message: "Both 'email' and 'whatsappNumber' are required query parameters." });
}
const config = {
headers: {
'accept': '*/*',
'Authorization': 'Your Bearer Token', // Replace YOUR_TOKEN with your actual token
'Content-Type': 'application/json-patch+json'
},
params: {
email,
whatsappNumber
}
};
try {
// Note: Using the POST method but the parameters are passed as query strings
const response = await axios.post(`https://live-mt-server.wati.io/Your Tanent ID/api/v1/assignOperator`, {}, config);
res.json({ success: true, response: response.data });
} catch (error) {
console.error(error);
res.status(500).json({ success: false, error: error.message });
}
});
// Endpoint to get message templates
// Endpoint to get message templates and filter the response
// Endpoint to get message templates and filter the response
// Endpoint to get message templates and filter the response for specific templates
app.get('/getMessageTemplates', async (req, res) => {
const { pageSize = 200, pageNumber = 1,tenant } = req.query; // Default values if not provided
console.log(tenant);
const config = {
headers: {
'accept': '*/*',
'Authorization': 'Your Bearer Token'
},
params: {
pageSize,
pageNumber,
}
};
try {
const response = await axios.get('https://live-mt-server.wati.io/Your Tanent ID/api/v1/getMessageTemplates', config);
console.log(response.data); // Log the data to see its structure
// if (response.data && Array.isArray(response.data.messageTemplates)) {
// const elementNames = response.data.messageTemplates.map(template => template.elementName);
// res.json({ elementNames });
// } else {
// console.log("The expected data was not found in the response.");
// res.status(500).json({ success: false, message: 'Unexpected response structure from API' });
// }
if (response.data && Array.isArray(response.data.messageTemplates)) {
// Initialize an empty array for the options
let options = [];
// Iterate through each template and add it to the options array as an object with label and value
response.data.messageTemplates.forEach((template) => {
options.push({
label: template.elementName+ ` -> (${template.status})`,
value: template.elementName
});
});
// Wrap the options array in an object under the key 'options' and send as JSON response
res.json({ options });
} else {
console.log("The expected data was not found in the response.");
res.status(500).json({ success: false, message: 'Unexpected response structure from API' });
}
} catch (error) {
console.error(error);
res.status(500).json({ success: false, error: error.message });
}
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});