Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
TELEGRAM_BOT_TOKEN=
TELEGRAM_CHAT_ID=
WEBHOOK_SECRET=
DISABLE_WORKFLOW_NOTIFICATIONS=false
DISABLE_WORKFLOW_NOTIFICATIONS_NAMES=workflow1,workflow2,workflow3
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ Integrate your GitHub Repo events with Telegram Channel/Chat using GitHub Webhoo
- Make your bot admin in the channel/chat you are going to connect
- Add [@MissRose_Bot](https://t.me/MissRose_Bot), make it admin and run `/id` command (to get the id of chat/channel)
- Make a .env file/fill your environment variables like given in [`.env.sample`](./.env.sample)
- Go to repos.js, and add the repository next to the chatID you want its logs to be sent (you can add as many as repositories you want, it's an array!)
- Install all dependencies - `yarn; yarn install`
- Run the bot - `yarn start`
- Now go to your GitHub Repo's settings and add a webhook to your repo, fill the Payload URL with `https://<your-domain>/webhook` and Content Type as `application/json`.
- Now go to all Github Repositories Settings that you have added in repos.js, and add bot's webhook to all your mentioned repositories, fill the Payload URL with `https://<your-domain>/webhook` and Content Type as `application/json`.
- Enter Secret which you have entered in `.env` file.
- Then save your webhook and tada! You are ready to go.

Expand Down
201 changes: 109 additions & 92 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const fs = require("fs");
const router = express.Router();
const { Telegraf } = require("telegraf");
const crypto = require("crypto");
const { SUBSCRIPTED_REPOS } = require("./repos");

if (fs.existsSync(".env")) {
dotenv.config();
Expand All @@ -13,17 +14,15 @@ if (fs.existsSync(".env")) {
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;

const secret = process.env.WEBHOOK_SECRET;

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

async function sendMessage(message, buttontext, buttonurl) {
await bot.telegram.sendMessage(TELEGRAM_CHAT_ID, message, {
async function sendMessage(chatID, message, buttontext, buttonurl) {
await bot.telegram.sendMessage(chatID, message, {
parse_mode: "html",
disable_web_page_preview: true,
reply_markup: {
Expand Down Expand Up @@ -52,106 +51,124 @@ router.post("/webhook", (req, res) => {
return res.sendStatus(403);
}
let data = req.body;
if (data.starred_at) {
sendMessage(
`<a href="${data.repository.html_url}"><b>\[${data.repository.full_name}\] New Star Added</b></a>\nStarred by: ${data.sender.login}\nTotal Stars: <code>${data.repository.stargazers_count}</code>`,
"View Repository",
data.repository.html_url
);
} else if (data.head_commit) {
sendMessage(
`<a href="${data.repository.html_url}"><b>\[${data.repository.name}:${
data.ref.split("/")[2]
}\] 1 new commit</b></a>\n<code>${data.after.substring(0, 7)}</code> ${
data.head_commit.message
} - ${data.head_commit.author.username}`,
"View Commit",
data.head_commit.url
);
} else if (data.forkee) {
sendMessage(
`<a href="${data.repository.html_url}/network/members><b>[${data.repository.full_name}] Fork Created: [${data.forkee.full_name}]</b></a>"`,
"View Fork",
data.forkee.html_url
);
} else if (data.action && data.issue) {
if (
data.action === "opened" ||
data.action === "closed" ||
data.action === "reopened" ||
data.action === "locked" ||
data.action === "unlocked"
) {
const chatID = SUBSCRIPTED_REPOS.find(repo => repo.name.toLowerCase() === data.repository.name.toLowerCase())?.telegram_chat_id;

if(chatID){
if (data.starred_at) {
sendMessage(
`<a href="${data.issue.url}"><b>\[${
data.repository.full_name
}\] Issue ${data.action}: #${
data.issue.number
} ${data.issue.title.substring(0, 10)}...</b></a>\n${
data.issue.user.login
} - <code>${data.issue.body.substring(0, 120)}...</code>`,
"View Issue",
data.issue.html_url
chatID,
`<a href="${data.repository.html_url}"><b>\[${data.repository.full_name}\] New Star Added</b></a>\nStarred by: ${data.sender.login}\nTotal Stars: <code>${data.repository.stargazers_count}</code>`,
"View Repository",
data.repository.html_url
);
}
} else if (data.action && data.pull_request) {
if (
data.action === "opened" ||
data.action === "closed" ||
data.action === "locked" ||
data.action === "unlocked" ||
data.action === "reopened"
) {
} else if (data.head_commit) {
sendMessage(
`<a href="${data.pull_request.html_url}"><b>[${
data.repository.full_name
}] Pull request ${data.action}: #${
data.number
} ${data.pull_request.title.substring(0, 10)}...</b></a>\n${
data.pull_request.user.login
} - <code>${data.pull_request.body.substring(0, 120)}...</code>`,
"View Pull Request",
`${data.pull_request.html_url}`
chatID,
`<a href="${data.repository.html_url}"><b>\[${data.repository.name}:${
data.ref.split("/")[2]
}\] 1 new commit</b></a>\n<code>${data.after.substring(0, 7)}</code> ${
data.head_commit.message
} - ${data.head_commit.author.username}`,
"View Commit",
data.head_commit.url
);
}
} else if (data.action && data.release) {
if (
data.action === "published" ||
data.action === "unpublished" ||
data.action === "created" ||
data.action === "edited" ||
data.action === "deleted" ||
data.action === "prereleased" ||
data.action === "released"
) {
} else if (data.forkee) {
sendMessage(
`<a href="${data.release.html_url}"><b>[${data.repository.full_name}] New release ${data.action}: ${data.release.tag_name}</b></a>`,
"View Release",
data.release.html_url
chatID,
`<a href="${data.repository.html_url}/network/members><b>[${data.repository.full_name}] Fork Created: [${data.forkee.full_name}]</b></a>"`,
"View Fork",
data.forkee.html_url
);
}
} else if (
process.env.DISABLE_WORKFLOW_NOTIFICATIONS !== "true" ||
!process.env.DISABLE_WORKFLOW_NOTIFICATIONS
) {
if (process.env.DISABLE_WORKFLOW_NOTIFICATIONS_NAME) {
const workflowNames =
process.env.DISABLE_WORKFLOW_NOTIFICATIONS_NAME.split(",");
} else if (data.action && data.issue) {
if (
(data.action === "requested" && data.workflow_run) ||
(data.action === "completed" && data.workflow_run)
data.action === "opened" ||
data.action === "closed" ||
data.action === "reopened" ||
data.action === "locked" ||
data.action === "unlocked"
) {
if (!workflowNames.includes(data.workflow.name)) {
sendMessage(
`<a href="${data.workflow_run.html_url}"><b>[${data.repository.full_name}] Workflow ${data.action}: ${data.workflow.name}</b></a>`,
"View Workflow",
data.workflow_run.html_url
);
sendMessage(
chatID,
`<a href="${data.issue.url}"><b>\[${
data.repository.full_name
}\] Issue ${data.action}: #${
data.issue.number
} ${data.issue.title.substring(0, 10)}...</b></a>\n${
data.issue.user.login
} - <code>${data.issue.body.substring(0, 120)}...</code>`,
"View Issue",
data.issue.html_url
);
}
} else if (data.action && data.pull_request) {
if (
data.action === "opened" ||
data.action === "closed" ||
data.action === "locked" ||
data.action === "unlocked" ||
data.action === "reopened"
) {
sendMessage(
chatID,
`<a href="${data.pull_request.html_url}"><b>[${
data.repository.full_name
}] Pull request ${data.action}: #${
data.number
} ${data.pull_request.title.substring(0, 10)}...</b></a>\n${
data.pull_request.user.login
} - <code>${data.pull_request.body.substring(0, 120)}...</code>`,
"View Pull Request",
`${data.pull_request.html_url}`
);
}
} else if (data.action && data.release) {
if (
data.action === "published" ||
data.action === "unpublished" ||
data.action === "created" ||
data.action === "edited" ||
data.action === "deleted" ||
data.action === "prereleased" ||
data.action === "released"
) {
sendMessage(
chatID,
`<a href="${data.release.html_url}"><b>[${data.repository.full_name}] New release ${data.action}: ${data.release.tag_name}</b></a>`,
"View Release",
data.release.html_url
);
}
} else if (
process.env.DISABLE_WORKFLOW_NOTIFICATIONS !== "true" ||
!process.env.DISABLE_WORKFLOW_NOTIFICATIONS
) {
if (process.env.DISABLE_WORKFLOW_NOTIFICATIONS_NAME) {
const workflowNames =
process.env.DISABLE_WORKFLOW_NOTIFICATIONS_NAME.split(",");
if (
(data.action === "requested" && data.workflow_run) ||
(data.action === "completed" && data.workflow_run)
) {
if (!workflowNames.includes(data.workflow.name)) {
sendMessage(
chatID,
`<a href="${data.workflow_run.html_url}"><b>[${data.repository.full_name}] Workflow ${data.action}: ${data.workflow.name}</b></a>`,
"View Workflow",
data.workflow_run.html_url
);
}
}
}
}
res.sendStatus(200);
}else{
res
.status(404)
.send(
"Repository Not Found"
);
}
res.sendStatus(200);

});

app.get("/", (req, res) => {
Expand Down
7 changes: 7 additions & 0 deletions repos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const SUBSCRIPTED_REPOS = [
{
name: "",
telegram_chat_id: 0
},
// ...
]