Repository navigation
Expand file tree
/
Copy pathsendSlackNotification.js
More file actions
108 lines (101 loc) · 3.43 KB
/
Copy pathsendSlackNotification.js
File metadata and controls
108 lines (101 loc) · 3.43 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
/*
* Copyright (C) 2025 Kraiany,
* Dmytro Kovalov <dmytro.kovalov@gmail.com>
*
* This file is part of GmailTabelogAutomation.
*
* GmailTabelogAutomation is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GmailTabelogAutomation is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const SLACK_WEBHOOK_URL = config().slackWebhookUrl;
/**
* Sends a notification message to Slack.
* Function uses scriptProperty SLACK_SEND (Bool) - if set to false, Slack message is not sent. For use during debuging.
*
* @param {string} title The main title of the notification (e.g., "[New Reservation]").
* @param {object} entry The calendar entry object containing reservation data.
* @param {string} color The color bar for the Slack message (e.g., 'good', 'warning', 'danger').
*/
function sendSlackNotification(title, entry, color) {
if (config().slackSend === 'false') { return }; // Disable during debugging
// if (SLACK_WEBHOOK_URL === config().slackWebhookUrl) {
// Logger.log("Slack Webhook URL is not configured. Skipping notification.");
// return;
// }
// Format the main details into Slack 'fields' for a compact view
const fields = [
{
"title": "Diner Name",
"value": entry.dinerName,
"short": true
},
{
"title": "Guests",
"value": `${entry.guestCount}名`,
"short": true
},
{
"title": "Date",
"value": entry.finalReservationDate,
"short": true
},
{
"title": "Time",
"value": entry.time,
"short": true
},
{
"title": "Booking ID",
"value": entry.bookingId,
"short": true
},
{
"title": "Plan/Course",
"value": entry.coursePlan || 'お席のみ',
"short": true
}
];
// If there are changes or cancellations, add a dedicated field
if (entry.changes && entry.changes !== 'N/A') {
fields.push({ "title": "CHANGES", "value": entry.changes, "short": false });
}
if (entry.cancellationReason && entry.cancellationReason !== 'N/A') {
fields.push({ "title": "CANCELLATION REASON", "value": entry.cancellationReason, "short": false });
}
// Optional: Include a link to the calendar event if available
if (entry.eventUrl) {
fields.push({ "title": "CALENDAR LINK", "value": `<${entry.eventUrl}|View Event>`, "short": false });
}
const payload = {
"attachments": [
{
"mrkdwn_in": ["text"],
"color": color, // 'good' (green), 'warning' (yellow), 'danger' (red)
"pretext": `*${title}*`,
"fields": fields,
"ts": Math.floor(Date.now() / 1000) // Timestamp
}
]
};
const options = {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify(payload)
};
try {
UrlFetchApp.fetch(SLACK_WEBHOOK_URL, options);
Logger.log(`Slack notification sent for ${title}.`);
} catch (e) {
Logger.log(`Failed to send Slack notification: ${e.toString()} Entry: ${JSON.stringify(entry)}`);
}
}