forked from agam778/railway-to-tg
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (88 loc) · 3.21 KB
/
index.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
const express = require("express");
const bodyParser = require("body-parser");
const dotenv = require("dotenv");
const fs = require("fs");
const router = express.Router();
const { Telegraf } = require("telegraf");
if (fs.existsSync(".env")) {
dotenv.config();
}
if (!process.env.TELEGRAM_BOT_TOKEN || !process.env.TELEGRAM_CHAT_ID) {
throw new Error(
"Please set the TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID environment variables"
);
}
const app = express();
const bot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN);
const TELEGRAM_CHAT_ID = process.env.TELEGRAM_CHAT_ID;
const PORT = process.env.PORT || 5000;
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
async function sendMessage(message, buttontext, buttonurl) {
await bot.telegram.sendMessage(TELEGRAM_CHAT_ID, message, {
parse_mode: "html",
disable_web_page_preview: true,
reply_markup: {
inline_keyboard: [
[
{
text: buttontext,
url: buttonurl,
},
],
],
},
});
}
router.post("/webhook", (req, res) => {
let data = req.body;
if (data.type === "DEPLOY" && data.status === "SUCCESS") {
sendMessage(
`<b>Deployment: ${data.project.name}</b>\n\✅ Status: <code>${data.status}</code>\n🌳 Environment: <code>${data.environment.name}</code>\n👨💻 Creator: <code>${data.deployment.creator.name}</code>`,
"View Deployment",
`https://railway.app/project/${data.project.id}/`
);
} else if (data.type === "DEPLOY" && data.status === "BUILDING") {
sendMessage(
`<b>Deployment: ${data.project.name}</b>\n\⚒️ Status: <code>${data.status}</code>\n🌳 Environment: <code>${data.environment.name}</code>\n👨💻 Creator: <code>${data.deployment.creator.name}</code>`,
"View Deployment",
`https://railway.app/project/${data.project.id}/`
);
} else if (data.type === "DEPLOY" && data.status === "DEPLOYING") {
sendMessage(
`<b>Deployment: ${data.project.name}</b>\n\🚀 Status: <code>${data.status}</code>\n🌳 Environment: <code>${data.environment.name}</code>\n👨💻 Creator: <code>${data.deployment.creator.name}</code>`,
"View Deployment",
`https://railway.app/project/${data.project.id}/`
);
} else if (data.type === "DEPLOY" && data.status === "CRASHED") {
sendMessage(
`<b>Deployment: ${data.project.name}</b>\n\❌ Status: <code>${data.status}</code>\n🌳 Environment: <code>${data.environment.name}</code>\n👨💻 Creator: <code>${data.deployment.creator.name}</code>`,
"View Deployment",
`https://railway.app/project/${data.project.id}/`
);
} else {
console.log("Unknown event: ", data);
}
res.sendStatus(200);
});
app.get("/", (req, res) => {
res
.status(405)
.send(
"405 Method Not Allowed. Please see the README.md - https://github.com/agam778/github-to-telegram#readme"
);
});
app.get("/webhook", (req, res) => {
res
.status(405)
.send(
"405 Method Not Allowed. Please see the README.md - https://github.com/agam778/github-to-telegram#readme"
);
});
app.use("/", router);
app.listen(PORT, (err) => {
if (err) {
console.log(err);
}
console.log(`Server listening on port ${PORT}`);
});