diff --git a/.env.sample b/.env.sample index 2e15e38..a0d5799 100644 --- a/.env.sample +++ b/.env.sample @@ -1,5 +1,4 @@ TELEGRAM_BOT_TOKEN= -TELEGRAM_CHAT_ID= WEBHOOK_SECRET= DISABLE_WORKFLOW_NOTIFICATIONS=false DISABLE_WORKFLOW_NOTIFICATIONS_NAMES=workflow1,workflow2,workflow3 \ No newline at end of file diff --git a/README.md b/README.md index fd3ea29..fbfd0d1 100644 --- a/README.md +++ b/README.md @@ -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:///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:///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. diff --git a/index.js b/index.js index 26353d7..dad1f69 100644 --- a/index.js +++ b/index.js @@ -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(); @@ -13,8 +14,6 @@ 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; @@ -22,8 +21,8 @@ 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: { @@ -52,106 +51,124 @@ router.post("/webhook", (req, res) => { return res.sendStatus(403); } let data = req.body; - if (data.starred_at) { - sendMessage( - `\[${data.repository.full_name}\] New Star Added\nStarred by: ${data.sender.login}\nTotal Stars: ${data.repository.stargazers_count}`, - "View Repository", - data.repository.html_url - ); - } else if (data.head_commit) { - sendMessage( - `\[${data.repository.name}:${ - data.ref.split("/")[2] - }\] 1 new commit\n${data.after.substring(0, 7)} ${ - data.head_commit.message - } - ${data.head_commit.author.username}`, - "View Commit", - data.head_commit.url - ); - } else if (data.forkee) { - sendMessage( - ` repo.name.toLowerCase() === data.repository.name.toLowerCase())?.telegram_chat_id; + + if(chatID){ + if (data.starred_at) { sendMessage( - `\[${ - data.repository.full_name - }\] Issue ${data.action}: #${ - data.issue.number - } ${data.issue.title.substring(0, 10)}...\n${ - data.issue.user.login - } - ${data.issue.body.substring(0, 120)}...`, - "View Issue", - data.issue.html_url + chatID, + `\[${data.repository.full_name}\] New Star Added\nStarred by: ${data.sender.login}\nTotal Stars: ${data.repository.stargazers_count}`, + "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( - `[${ - data.repository.full_name - }] Pull request ${data.action}: #${ - data.number - } ${data.pull_request.title.substring(0, 10)}...\n${ - data.pull_request.user.login - } - ${data.pull_request.body.substring(0, 120)}...`, - "View Pull Request", - `${data.pull_request.html_url}` + chatID, + `\[${data.repository.name}:${ + data.ref.split("/")[2] + }\] 1 new commit\n${data.after.substring(0, 7)} ${ + 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( - `[${data.repository.full_name}] New release ${data.action}: ${data.release.tag_name}`, - "View Release", - data.release.html_url + chatID, + `[${data.repository.full_name}] Workflow ${data.action}: ${data.workflow.name}`, - "View Workflow", - data.workflow_run.html_url - ); + sendMessage( + chatID, + `\[${ + data.repository.full_name + }\] Issue ${data.action}: #${ + data.issue.number + } ${data.issue.title.substring(0, 10)}...\n${ + data.issue.user.login + } - ${data.issue.body.substring(0, 120)}...`, + "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, + `[${ + data.repository.full_name + }] Pull request ${data.action}: #${ + data.number + } ${data.pull_request.title.substring(0, 10)}...\n${ + data.pull_request.user.login + } - ${data.pull_request.body.substring(0, 120)}...`, + "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, + `[${data.repository.full_name}] New release ${data.action}: ${data.release.tag_name}`, + "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, + `[${data.repository.full_name}] Workflow ${data.action}: ${data.workflow.name}`, + "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) => { diff --git a/repos.js b/repos.js new file mode 100644 index 0000000..be76d8f --- /dev/null +++ b/repos.js @@ -0,0 +1,7 @@ +const SUBSCRIPTED_REPOS = [ + { + name: "", + telegram_chat_id: 0 + }, + // ... +] \ No newline at end of file