-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcode.gs
134 lines (116 loc) · 4.07 KB
/
code.gs
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
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('WhatsApp Sender')
.addItem('Send Messages', 'showSidebar')
.addItem('Set Settings', 'promptForSettings') // Menu item for settings
.addToUi();
}
function showSidebar() {
const html = HtmlService.createHtmlOutputFromFile('Form')
.setTitle('Send WhatsApp Message')
.setWidth(300);
SpreadsheetApp.getUi().showSidebar(html);
}
function promptForSettings() {
const html = HtmlService.createHtmlOutputFromFile('SettingsForm')
.setWidth(400)
.setHeight(250);
SpreadsheetApp.getUi().showModalDialog(html, 'Set API Token and Endpoint');
}
function setSettings(token, endpoint) {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
sheet.getRange('D1').setValue(token); // Save token to cell D1
sheet.getRange('E1').setValue(endpoint); // Save endpoint to cell E1
}
function getSettings() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
return {
token: sheet.getRange('D1').getValue(),
endpoint: sheet.getRange('E1').getValue()
};
}
function stopSendingMessages() {
const scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperty('stopSending', 'true'); // Set the stop flag
}
function sendWhatsAppMessages(templateName, languageCode, imageUrl, startRow, endRow, delaySeconds) {
const settings = getSettings();
const token = settings.token;
const endpoint = settings.endpoint;
if (!token || !endpoint) {
SpreadsheetApp.getUi().alert('API Token or Endpoint is not set. Please set them first.');
return "Settings not complete";
}
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('Sheet1');
const scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperty('stopSending', 'false'); // Reset the stop flag
startRow = parseInt(startRow);
endRow = parseInt(endRow);
const delay = parseInt(delaySeconds) * 1000;
const statusCell = sheet.getRange("C1");
statusCell.setValue("Starting to send messages...");
for (let i = startRow; i <= endRow; i++) {
if (scriptProperties.getProperty('stopSending') === 'true') {
statusCell.setValue("Sending stopped by user at message " + (i - startRow));
return "Sending stopped by user.";
}
const phoneNumber = sheet.getRange(i, 1).getValue();
if (!phoneNumber || !templateName || !languageCode || !imageUrl) {
sheet.getRange(i, 2).setValue("Error: Missing required information");
continue;
}
const formattedPhoneNumber = `+${phoneNumber}`;
const messageData = {
"messaging_product": "whatsapp",
"to": formattedPhoneNumber,
"type": "template",
"template": {
"name": templateName,
"language": {
"code": languageCode
},
"components": [
{
"type": "header",
"parameters": [
{
"type": "image",
"image": {
"link": imageUrl
}
}
]
}
]
}
};
const options = {
"method": "post",
"headers": {
"Authorization": "Bearer " + token,
"Content-Type": "application/json"
},
"payload": JSON.stringify(messageData),
"muteHttpExceptions": true
};
try {
const response = UrlFetchApp.fetch(endpoint, options);
const responseJson = JSON.parse(response.getContentText());
if (response.getResponseCode() === 200) {
const messageId = responseJson.messages[0].id;
sheet.getRange(i, 2).setValue(messageId);
statusCell.setValue(`Sending message ${i - startRow + 1} of ${endRow - startRow + 1}...`);
} else {
sheet.getRange(i, 2).setValue("Failed to send: " + response.getContentText());
}
} catch (error) {
sheet.getRange(i, 2).setValue("Error: " + error.toString());
}
Utilities.sleep(delay);
}
statusCell.setValue("All messages have been sent.");
return "Attempt to send messages completed.";
}