forked from claudeisakeeb/ctc-slackbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
124 lines (107 loc) · 3.66 KB
/
app.js
File metadata and controls
124 lines (107 loc) · 3.66 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
/**
* Matchy Slack Bot - Socket Mode + Express
* Deploy to Koyeb. Use UptimeRobot to ping /health every 30 min to prevent scale-to-zero.
*/
const schedule = require("node-schedule");
const express = require("express");
const path = require("path");
require("dotenv").config({ path: path.join(__dirname, ".env") });
const Bot = require("./utils/bot");
const {
generateMatches,
} = require("./utils/matchy-json");
// Register handlers before starting
require("./handlers");
const MATCHY_CHANNEL_ID = process.env.MATCHY_CHANNEL_ID || "C01FL4VCE1Z";
(async () => {
await Bot.start();
console.log("⚡️ Slack Bolt app (Socket Mode) is running!");
// Schedule weekly matchy generation (Wednesdays at 5 PM PST)
const rule = new schedule.RecurrenceRule();
rule.tz = "America/Los_Angeles";
rule.dayOfWeek = 3; // Wednesday
rule.hour = 17; // 5 PM
rule.minute = 0;
schedule.scheduleJob(rule, async () => {
console.log("🕐 Running scheduled matchy generation...");
try {
const mockContext = {
ack: async () => {},
respond: async (message) => {
try {
await Bot.client.chat.postMessage({
channel: MATCHY_CHANNEL_ID,
text: `🤖 *Automated Matchy Generation*\n\n${message}`,
});
} catch (error) {
console.error("Error sending to channel:", error);
}
},
};
await generateMatches(mockContext);
} catch (error) {
console.error("Error in scheduled matchy generation:", error);
}
});
// Express server: keep-alive endpoint + matchy webhook
const app = express();
app.use(express.json());
// Keep-alive endpoint - ping this every 30 min (UptimeRobot, Cron-job.org) to prevent Koyeb scale-to-zero
app.get("/", (req, res) => {
res.status(200).send("Bot is running");
});
app.get("/health", (req, res) => {
res.status(200).json({
status: "ok",
message: "Matchy Bot is running",
timestamp: new Date().toISOString(),
});
});
// Webhook for GitHub Actions / external cron to trigger matchy
app.post("/matchy-scheduled", async (req, res) => {
const authHeader = req.headers.authorization;
const cronSecret = process.env.CRON_SECRET;
const isValidAuth =
cronSecret &&
(authHeader === `Bearer ${cronSecret}` ||
authHeader === `bearer ${cronSecret}`);
if (!isValidAuth) {
return res.status(401).json({ error: "Unauthorized" });
}
try {
console.log("🕐 Webhook triggered: Running scheduled matchy generation...");
const mockContext = {
ack: async () => {},
respond: async (message) => {
try {
await Bot.client.chat.postMessage({
channel: MATCHY_CHANNEL_ID,
text: `🤖 *Automated Matchy Generation*\n\n${message}`,
});
} catch (error) {
console.error("Error sending to channel:", error);
}
},
};
await generateMatches(mockContext);
res.status(200).json({
success: true,
message: "Matchy generation completed",
timestamp: new Date().toISOString(),
});
} catch (error) {
console.error("Error in webhook matchy generation:", error);
res.status(500).json({
error: error.message,
timestamp: new Date().toISOString(),
});
}
});
const port = process.env.PORT || 8000;
app.listen(port, () => {
console.log(`🔗 HTTP server on port ${port}`);
console.log(` Keep-alive: GET / or GET /health (ping every 30 min)`);
console.log(` Matchy cron: POST /matchy-scheduled`);
console.log("🚀 Matchy Bot ready for Koyeb deployment!");
});
})();