-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
504 lines (442 loc) Β· 20.1 KB
/
Copy pathapp.js
File metadata and controls
504 lines (442 loc) Β· 20.1 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/**
* Gospel WhatsApp Sender β Green API edition
*
* Flow:
* 5:00 PM IST (day before) β preview email to mom
* 12:30 AM IST (send day) β send to WhatsApp group via Green API
* 1:00 AM IST (send day) β watchdog: retry if not sent
*
* Setup:
* 1. Sign up at green-api.com
* 2. Create an instance, scan QR on WhatsApp
* 3. Copy ID_INSTANCE and API_TOKEN from their dashboard
* 4. Get GROUP_ID from Green API dashboard or journal below
* 5. Set env vars, npm install, pm2 start app.js
*
* Manual triggers (bot must be running via pm2):
* curl "http://localhost:3000/trigger"
* curl "http://localhost:3000/trigger?date=2026-04-17"
* curl "http://localhost:3000/preview"
* curl "http://localhost:3000/status"
*
* CLI (run directly):
* node app.js --trigger-now
* node app.js --trigger-now --date=2026-04-17
* node app.js --preview-now
*/
const cron = require('node-cron');
const nodemailer = require('nodemailer');
const fs = require('fs');
const path = require('path');
const http = require('http');
const { getTodaysMessage } = require('./gospel_matcher');
// βββββββββββββββββββββββββββββββββββββββββββββ
// CONFIG
// βββββββββββββββββββββββββββββββββββββββββββββ
const CONFIG = {
// Green API credentials β from green-api.com dashboard
GREEN_API_ID: process.env.GREEN_API_ID || 'YOUR_ID_INSTANCE',
GREEN_API_TOKEN: process.env.GREEN_API_TOKEN || 'YOUR_API_TOKEN',
// WhatsApp group ID β format: 1234567890-1234567890@g.us
// Get from Green API dashboard β Contacts β find the group
GROUP_ID: process.env.GROUP_ID || 'YOUR_GROUP_ID@g.us',
// Gmail SMTP β use App Password
EMAIL_USER: process.env.EMAIL_USER || 'youremail@gmail.com',
EMAIL_PASS: process.env.EMAIL_PASS || 'your-app-password',
ALERT_EMAIL: process.env.ALERT_EMAIL || 'youremail@gmail.com',
PREVIEW_EMAIL: process.env.PREVIEW_EMAIL || 'moms-email@gmail.com',
// Retry settings
MAX_RETRIES: 3,
RETRY_DELAY_MS: 5 * 60 * 1000, // 5 mins
// HTTP trigger port
TRIGGER_PORT: parseInt(process.env.TRIGGER_PORT || '3000'),
// Paths
SENT_LOG: path.join(__dirname, 'sent.log.json'),
QUEUE_FILE: path.join(__dirname, 'pending.json'),
};
const TIMEZONE = 'Asia/Kolkata';
const DEFAULT_CLOSING = 'Jesus is Lord, spread the living word of God.';
// βββββββββββββββββββββββββββββββββββββββββββββ
// LOGGER
// βββββββββββββββββββββββββββββββββββββββββββββ
function log(level, ...args) {
console.log(`[${new Date().toISOString()}] [${level.toUpperCase()}]`, ...args);
}
// Handle unhandled promise rejections gracefully
process.on('unhandledRejection', (reason) => {
log('error', 'Unhandled rejection:', reason?.message || reason);
});
// βββββββββββββββββββββββββββββββββββββββββββββ
// GREEN API β send message
// βββββββββββββββββββββββββββββββββββββββββββββ
async function sendToGroup(text) {
const url = `https://api.green-api.com/waInstance${CONFIG.GREEN_API_ID}/sendMessage/${CONFIG.GREEN_API_TOKEN}`;
const res = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ chatId: CONFIG.GROUP_ID, message: text }),
});
if (!res.ok) {
const body = await res.text();
throw new Error(`Green API ${res.status}: ${body}`);
}
const data = await res.json();
log('info', 'Green API response:', JSON.stringify(data));
return data;
}
// βββββββββββββββββββββββββββββββββββββββββββββ
// GREEN API β check instance is connected
// βββββββββββββββββββββββββββββββββββββββββββββ
async function checkGreenApiStatus() {
try {
const url = `https://api.green-api.com/waInstance${CONFIG.GREEN_API_ID}/getStateInstance/${CONFIG.GREEN_API_TOKEN}`;
const res = await fetch(url);
const data = await res.json();
// stateInstance: "authorized" = connected, "notAuthorized" = needs QR scan
return data.stateInstance;
} catch (err) {
return 'error: ' + err.message;
}
}
// βββββββββββββββββββββββββββββββββββββββββββββ
// BUILD MESSAGE
// βββββββββββββββββββββββββββββββββββββββββββββ
function buildMessage(pickedMsg, todayReading) {
const dayLabel = todayReading?.celebration?.name
|| todayReading?.season
|| "Today's";
const title = pickedMsg.title?.trim() ? `(${pickedMsg.title}) ` : '';
const verse = pickedMsg.verse?.trim() || '';
const citation = pickedMsg.citation || '';
const closing = pickedMsg.closing?.trim() || DEFAULT_CLOSING;
return `${dayLabel} Gospel Msg ${title}${verse} ${citation} ${closing}`.trim();
}
// βββββββββββββββββββββββββββββββββββββββββββββ
// BIBLE API FALLBACK
// βββββββββββββββββββββββββββββββββββββββββββββ
async function fetchBibleApiFallback(gospelRef) {
try {
const query = (gospelRef || 'john 3:16').toLowerCase().replace(/\s+/g, '+');
const res = await fetch(`https://bible-api.com/${query}?translation=kjv`);
if (!res.ok) throw new Error(`Bible API ${res.status}`);
const data = await res.json();
const text = data.text?.replace(/\n/g, ' ').trim();
return `Today's Gospel Msg ${text} ${data.reference} ${DEFAULT_CLOSING}`;
} catch (err) {
log('warn', 'Bible API failed:', err.message);
return `Today's Gospel Msg Jesus said, "I am the way and the truth and the life." JH(14:6) ${DEFAULT_CLOSING}`;
}
}
// βββββββββββββββββββββββββββββββββββββββββββββ
// GET MESSAGE FOR A DATE
// βββββββββββββββββββββββββββββββββββββββββββββ
async function getMessageForDate(dateStr) {
// Always resolve to IST date β server is UTC so new Date() alone is wrong
const istDateStr = dateStr || new Date().toLocaleDateString('en-CA', { timeZone: TIMEZONE });
const [y, m, d] = istDateStr.split('-').map(Number);
const date = new Date(y, m - 1, d);
const result = await getTodaysMessage(date);
if (result.source === 'archive') {
return { text: buildMessage(result.message, result.todayReading), source: 'archive' };
}
log('info', 'No archive match β falling back to Bible API');
const text = await fetchBibleApiFallback(result.todayReading?.gospelRaw);
return { text, source: 'fallback' };
}
// βββββββββββββββββββββββββββββββββββββββββββββ
// SENT LOG
// βββββββββββββββββββββββββββββββββββββββββββββ
function getSentLog() {
try { return JSON.parse(fs.readFileSync(CONFIG.SENT_LOG, 'utf8')); }
catch { return []; }
}
function markSent(entry) {
const entries = getSentLog();
const istDate = new Date().toLocaleDateString('en-CA', { timeZone: TIMEZONE });
entries.push({ ...entry, sentAt: new Date().toISOString(), istDate });
const cutoff = new Date();
cutoff.setDate(cutoff.getDate() - 90);
fs.writeFileSync(CONFIG.SENT_LOG, JSON.stringify(
entries.filter(e => new Date(e.sentAt) > cutoff), null, 2
));
}
function alreadySentToday() {
const today = new Date().toLocaleDateString('en-CA', { timeZone: TIMEZONE });
return getSentLog().some(e => e.istDate === today || e.sentAt?.startsWith(today));
}
// βββββββββββββββββββββββββββββββββββββββββββββ
// QUEUE
// βββββββββββββββββββββββββββββββββββββββββββββ
function queueMessage(text) {
const q = getPendingQueue();
const today = new Date().toLocaleDateString('en-CA', { timeZone: TIMEZONE });
if (q.some(e => e.date === today)) return;
q.push({ date: today, text, queuedAt: new Date().toISOString() });
fs.writeFileSync(CONFIG.QUEUE_FILE, JSON.stringify(q, null, 2));
log('info', 'Message queued for retry');
}
function getPendingQueue() {
try { return JSON.parse(fs.readFileSync(CONFIG.QUEUE_FILE, 'utf8')); }
catch { return []; }
}
function clearQueue() {
fs.writeFileSync(CONFIG.QUEUE_FILE, JSON.stringify([]));
}
async function flushQueue() {
const queue = getPendingQueue();
if (!queue.length) return;
log('info', `Flushing ${queue.length} queued message(s)`);
for (const item of queue) {
try {
await sendToGroup(item.text);
markSent({ text: item.text.slice(0, 80), source: 'queue', originalDate: item.date });
log('info', `β
Queued message sent (originally for ${item.date})`);
} catch (err) {
log('error', 'Failed to flush queue:', err.message);
return;
}
}
clearQueue();
}
// βββββββββββββββββββββββββββββββββββββββββββββ
// EMAIL
// βββββββββββββββββββββββββββββββββββββββββββββ
const mailer = nodemailer.createTransport({
service: 'gmail',
auth: { user: CONFIG.EMAIL_USER, pass: CONFIG.EMAIL_PASS },
});
async function sendEmail({ to, subject, body }) {
try {
await mailer.sendMail({
from: CONFIG.EMAIL_USER,
to,
subject: `[Gospel Bot] ${subject}`,
text: body,
});
log('info', `Email sent to ${to}: ${subject}`);
} catch (err) {
log('error', 'Email failed:', err.message);
}
}
// βββββββββββββββββββββββββββββββββββββββββββββ
// PREVIEW EMAIL TO MOM
// βββββββββββββββββββββββββββββββββββββββββββββ
async function sendPreviewToMom() {
log('info', 'π§ Preparing preview email for mom...');
const todayStr = new Date().toLocaleDateString('en-CA', { timeZone: TIMEZONE });
const [y, m, d] = todayStr.split('-').map(Number);
const tomorrowStr = new Date(Date.UTC(y, m - 1, d + 1)).toISOString().slice(0, 10);
let messageText;
try {
const result = await getMessageForDate(tomorrowStr);
messageText = result.text;
} catch (err) {
log('error', "Failed to get tomorrow's message:", err.message);
await sendEmail({
to: CONFIG.ALERT_EMAIL,
subject: 'β οΈ Could not prepare preview email for mom',
body: `Error: ${err.message}`,
});
return;
}
const [ty, tm, td] = tomorrowStr.split('-').map(Number);
const tomorrowFormatted = new Date(Date.UTC(ty, tm - 1, td))
.toLocaleDateString('en-IN', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', timeZone: 'UTC' });
await sendEmail({
to: CONFIG.PREVIEW_EMAIL,
subject: `π Tomorrow's Gospel Message β ${tomorrowFormatted}`,
body:
`Hi,
Here is tomorrow's gospel message that will be sent to the group at 12:30 AM:
βββββββββββββββββββββββββββββββββββββ
${messageText}
βββββββββββββββββββββββββββββββββββββ
The bot will send this automatically. If you don't see it in the group by 1:00 AM, please send it manually.
God bless,
Gospel Bot`,
});
log('info', `β
Preview email sent to mom for ${tomorrowStr}`);
}
// βββββββββββββββββββββββββββββββββββββββββββββ
// SEND WITH RETRY
// βββββββββββββββββββββββββββββββββββββββββββββ
async function sendWithRetry(text, attempt = 1) {
try {
await sendToGroup(text);
markSent({ text: text.slice(0, 80), attempt });
log('info', `β
Message sent (attempt ${attempt})`);
await sendEmail({
to: CONFIG.ALERT_EMAIL,
subject: 'β
Gospel message sent',
body: `Sent at ${new Date().toLocaleTimeString('en-IN', { timeZone: TIMEZONE })}\n\n${text}`,
});
// Flush any queued messages now that we know the connection works
await flushQueue();
} catch (err) {
log('error', `Send failed (attempt ${attempt}):`, err.message);
if (attempt < CONFIG.MAX_RETRIES) {
log('info', `Retrying in ${CONFIG.RETRY_DELAY_MS / 60000} mins...`);
await sendEmail({
to: CONFIG.ALERT_EMAIL,
subject: `π Retry ${attempt}/${CONFIG.MAX_RETRIES}`,
body: `Send failed: ${err.message}\nRetrying in ${CONFIG.RETRY_DELAY_MS / 60000} minutes.`,
});
setTimeout(() => sendWithRetry(text, attempt + 1), CONFIG.RETRY_DELAY_MS);
} else {
queueMessage(text);
await sendEmail({
to: CONFIG.ALERT_EMAIL,
subject: 'π All retries failed β message queued',
body: `Failed after ${CONFIG.MAX_RETRIES} attempts.\nError: ${err.message}\n\nQueued for next retry.\n\nMessage:\n${text}`,
});
}
}
}
// βββββββββββββββββββββββββββββββββββββββββββββ
// CRON JOBS (all IST)
// βββββββββββββββββββββββββββββββββββββββββββββ
// 5:00 PM β preview email to mom
cron.schedule('0 17 * * *', async () => {
log('info', 'β° Preview cron fired (5:00 PM IST)');
await sendPreviewToMom();
}, { timezone: TIMEZONE });
// 12:30 AM β send to WhatsApp group
cron.schedule('30 0 * * *', async () => {
log('info', 'β° Send cron fired (12:30 AM IST)');
if (alreadySentToday()) {
log('info', 'Already sent today β skipping');
return;
}
try {
const { text } = await getMessageForDate(null);
await sendWithRetry(text);
} catch (err) {
log('error', 'Send cron failed:', err.message);
await sendEmail({
to: CONFIG.ALERT_EMAIL,
subject: 'β Send cron error',
body: `Failed to get or send today's message:\n${err.message}`,
});
}
}, { timezone: TIMEZONE });
// 1:00 AM β watchdog
cron.schedule('0 1 * * *', async () => {
if (alreadySentToday()) return;
log('warn', 'β οΈ Watchdog: message not sent β retrying');
await sendEmail({
to: CONFIG.ALERT_EMAIL,
subject: 'β οΈ Watchdog triggered',
body: 'Main send missed. Watchdog attempting now.',
});
try {
const { text } = await getMessageForDate(null);
await sendWithRetry(text);
} catch (err) {
log('error', 'Watchdog failed:', err.message);
await sendEmail({
to: CONFIG.ALERT_EMAIL,
subject: 'π Watchdog also failed',
body: `Both main send and watchdog failed.\n\nPlease ask mom to send manually β she received a preview at 5 PM.\n\nError: ${err.message}`,
});
}
}, { timezone: TIMEZONE });
// βββββββββββββββββββββββββββββββββββββββββββββ
// HTTP TRIGGER SERVER
// βββββββββββββββββββββββββββββββββββββββββββββ
const triggerServer = http.createServer(async (req, res) => {
const url = req.url;
// /trigger?date=YYYY-MM-DD (date optional)
if (url.startsWith('/trigger')) {
const dateParam = new URL(url, 'http://localhost').searchParams.get('date') || null;
if (dateParam && !/^\d{4}-\d{2}-\d{2}$/.test(dateParam)) {
res.writeHead(400);
res.end('Bad date. Use YYYY-MM-DD e.g. /trigger?date=2026-04-17\n');
return;
}
log('info', `π§ Manual trigger${dateParam ? ' for ' + dateParam : ' (today)'}`);
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(`Triggered${dateParam ? ' for ' + dateParam : ' for today'}. Check logs: pm2 logs gospel-bot\n`);
try {
const { text } = await getMessageForDate(dateParam);
await sendWithRetry(text);
} catch (err) {
log('error', 'Manual trigger failed:', err.message);
}
// /preview
} else if (url === '/preview') {
log('info', 'π§ Manual preview triggered');
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Preview email triggered. Check inbox.\n');
await sendPreviewToMom();
// /status
} else if (url === '/status') {
const greenStatus = await checkGreenApiStatus();
const status = {
greenApi: greenStatus,
sentToday: alreadySentToday(),
queueLength: getPendingQueue().length,
time: new Date().toLocaleString('en-IN', { timeZone: TIMEZONE }),
};
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(status, null, 2) + '\n');
} else {
res.writeHead(404);
res.end('Try /trigger /trigger?date=YYYY-MM-DD /preview /status\n');
}
});
triggerServer.listen(CONFIG.TRIGGER_PORT, '127.0.0.1', () => {
log('info', `π§ Trigger server on localhost:${CONFIG.TRIGGER_PORT}`);
});
// βββββββββββββββββββββββββββββββββββββββββββββ
// CLI FLAGS
// βββββββββββββββββββββββββββββββββββββββββββββ
// node app.js --trigger-now [--date=YYYY-MM-DD]
if (process.argv.includes('--trigger-now')) {
const dateArg = process.argv.find(a => a.startsWith('--date='));
const dateParam = dateArg ? dateArg.split('=')[1] : null;
if (dateParam && !/^\d{4}-\d{2}-\d{2}$/.test(dateParam)) {
console.error('β Bad date. Use --date=YYYY-MM-DD');
process.exit(1);
}
(async () => {
log('info', `--trigger-now${dateParam ? ' for ' + dateParam : ' (today)'}`);
try {
const { text } = await getMessageForDate(dateParam);
log('info', 'Message:', text);
await sendWithRetry(text);
} catch (err) {
log('error', '--trigger-now failed:', err.message);
}
})();
}
// node app.js --preview-now
if (process.argv.includes('--preview-now')) {
(async () => {
log('info', '--preview-now: sending preview email');
await sendPreviewToMom();
log('info', 'Done');
process.exit(0);
})();
}
// βββββββββββββββββββββββββββββββββββββββββββββ
// STARTUP
// βββββββββββββββββββββββββββββββββββββββββββββ
(async () => {
log('info', 'π Gospel bot starting (Green API edition)');
log('info', `Timezone : ${TIMEZONE}`);
log('info', `Preview : 5:00 PM IST β mom's email`);
log('info', `Send : 12:30 AM IST β WhatsApp group`);
log('info', `Watchdog : 1:00 AM IST`);
log('info', `Group ID : ${CONFIG.GROUP_ID}`);
// Check Green API connection on startup
const status = await checkGreenApiStatus();
log('info', `Green API status: ${status}`);
if (status !== 'authorized') {
log('warn', 'β οΈ Green API not authorized β go to green-api.com dashboard and scan QR');
await sendEmail({
to: CONFIG.ALERT_EMAIL,
subject: 'β οΈ Green API not authorized',
body: `Green API instance is not connected.\n\nGo to green-api.com β your instance β scan QR code to reconnect.`,
});
}
})();