English | 中文
Implement notifications to Telegram when GitHub repository star, issue, pull request, and fork events occur using GitHub Webhooks, Cloudflare Worker, and Telegram Bot.
The event notification styles are as follows
Issue event message
Pull Request event message
Star event message
Fork event message
Stars milestone message
- Configure Stars milestones, send congratulatory messages when milestones are reached
-
Optimize the issue of too many messages when events occur frequently(Pseudo-requirement — If events are frequent, you can resolve this by turning off TG notifications) - Add data statistics functionality, send statistical data periodically
Core idea: Star event occurs → GitHub Webhooks → Cloudflare Workers → Telegram Bot API → Send message to Telegram channel/individual
-
When someone stars a GitHub repository, GitHub triggers a Star event.
-
Use GitHub Webhooks to send HTTP POST requests
GitHub Webhooks will send the information of this Star event via an HTTP POST request to a pre-configured URL, which points to the address of a Cloudflare Worker.
-
Use Cloudflare Workers to process requests:
After receiving the request from GitHub Webhook, the Cloudflare Worker parses the details of the Star event (e.g., the user who starred, repository name, etc.) and constructs a notification message.
-
Call Telegram Bot API:
The Cloudflare Worker uses the Telegram Bot API, calling the sendMessage method to send the constructed notification message to your specified Telegram channel or personal chat.
-
Receive new Star notifications through the Telegram channel.
Specific implementation: index.js
Here are the detailed steps:
The purpose of this step is to obtain the API Token
of the Telegram Bot
- Open Telegram, search for BotFather, then send
/start
. - Use the
/newbot
command to create a new bot, and follow the prompts to name your bot. - After successful creation, BotFather will provide an
API Token
. Note down this Token, it will be used later.
There's not much to say about creating a channel, the main thing is to get the chat_id
of the channel or group, which will be used later.
-
Invite the Telegram Bot to your created personal channel or group (search for the bot using its
username
), and send a message in the group, such as "Hello World". -
Visit https://api.telegram.org/bot<Your_Bot_Token>/getUpdates in your browser. You will see the message you just sent in the results. Find the corresponding
chat_id
(thechat_id
starts with -100).
Steps:
- Clone the project
- Install dependencies
- configure environment variables
- Deploy the application to Cloudflare (after entering the deployment command, a web page will pop up for authorization login to Cloudflare)
# Clone the project
git clone https://github.com/byodian/notifier
cd notifier
# Install dependencies
pnpm install
# Configure environment variables (secrets)
# 1. Telegram Bot API Token
npx wrangler secret put TELEGRAM_TOKEN
# 2. Channel chat_id
npx wrangler secret put TELEGRAM_CHAT_ID
# Deploy Cloudflare worker
pnpm run deploy
Configure GitHub Stars milestones in wrangler.toml
[vars]
MILESTONES = [10,50,100,500,1000,5000,100000]
Open the Cloudflare website to get the Worker URL, which will be used later.
-
Go to your open-source GitHub repository.
-
Click Settings > Webhooks > Add webhook.
-
Fill in the Cloudflare Worker URL in the Payload URL.
-
Choose
application/json
for Content type. -
Secret is optional
-
In "Which events would you like to trigger this webhook?", select "Let me select individual events", then check Stars, Issues, Pull Requests, and Forks. Click Add webhook.