-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add stars milestone notification
- Loading branch information
Showing
11 changed files
with
1,799 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
"wrangler": "^3.60.3" | ||
}, | ||
"dependencies": { | ||
"bumpp": "^9.7.1" | ||
"bumpp": "^9.7.1", | ||
"svg2png-wasm": "^1.4.1" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,76 @@ | ||
import getEventMessage from "./github" | ||
import { stores } from "./stores" | ||
import { getMilestonePng } from "./utils" | ||
|
||
/** | ||
* Telegram Bot API Docs - https://core.telegram.org/bots/api#sendmessage | ||
*/ | ||
export default { | ||
async fetch(request, env, ctx) { | ||
if (request.method !== 'POST') { | ||
return new Response('Invalid request method', { status: 405 }) | ||
} | ||
async fetch(request, env, ctx) { | ||
if (request.method !== 'POST') { | ||
return new Response('Invalid request method', { status: 405 }) | ||
} | ||
|
||
if (!request.headers.has('X-GitHub-Event')) { | ||
return new Response('Invalid github event', { status: 404 }) | ||
} | ||
|
||
if (!request.headers.has('X-GitHub-Event')) { | ||
return new Response('Invalid github event', { status: 404 }) | ||
} | ||
const githubEvent = request.headers.get('X-GitHub-Event') | ||
|
||
const githubEvent = request.headers.get('X-GitHub-Event') | ||
try { | ||
const body = await request.json() | ||
const message = getEventMessage(githubEvent, body) | ||
|
||
// send message to Telegram | ||
// api docs - https://core.telegram.org/bots/api#sendmessage | ||
const telegramUrl = `https://api.telegram.org/bot${env.TELEGRAM_TOKEN}/sendMessage` | ||
|
||
const telegramPayload = { | ||
chat_id: env.TELEGRAM_CHAT_ID, | ||
text: message, | ||
parse_mode: "Markdown", | ||
link_preview_options: { is_disabled: true } | ||
} | ||
|
||
const telegramResponse = await fetch(telegramUrl, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify(telegramPayload), | ||
}) | ||
|
||
if (telegramResponse.ok) { | ||
return new Response('Message sent to Telegram', { status: 200 }) | ||
} else { | ||
return new Response(`Failed to send message to Telegram`, { status: 500 }) | ||
} | ||
// send message | ||
const body = await request.json() | ||
const message = getEventMessage(githubEvent, body) | ||
await sendTelegramMessage(env.TELEGRAM_TOKEN, env.TELEGRAM_CHAT_ID, message) | ||
|
||
// send milestone photo | ||
if (githubEvent === stores.eventType && | ||
stores.action === 'created' && | ||
env.MILESTONES.includes(stores.stars)) { | ||
try { | ||
const photoBuffer = await getMilestonePng(stores.stars) | ||
await sendTelegramPhoto(env.TELEGRAM_TOKEN, env.TELEGRAM_CHAT_ID, photoBuffer) | ||
} catch (error) { | ||
console.log('Failed to send milestone photo:', error.message) | ||
} | ||
} | ||
|
||
return new Response('Message sent to Telegram', { status: 200 }) | ||
} catch (error) { | ||
return new Response("Failed to process webhook event.", { | ||
status: 500, | ||
}); | ||
console.error('Error send message:', error.message) | ||
return new Response("Failed to handle github event.", { status: 500 }) | ||
} | ||
} | ||
} | ||
} | ||
|
||
async function sendTelegramMessage(token, chatId, message) { | ||
const response = await fetch(`https://api.telegram.org/bot${token}/sendMessage`, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
chat_id: chatId, | ||
text: message, | ||
parse_mode: "Markdown", | ||
link_preview_options: { is_disabled: true } | ||
}), | ||
}) | ||
if (!response.ok) { | ||
throw new Error('Failed to send message to Telegram') | ||
} | ||
return response | ||
} | ||
|
||
function makeMessage(body) { | ||
const { | ||
name: repo, | ||
html_url: repoUrl, | ||
stargazers_count, | ||
forks_count | ||
} = body.repository | ||
|
||
const { login: follwer, html_url: follwerUrl } = body.sender | ||
|
||
return ` | ||
New Github star for [${repo}](${repoUrl}) repo! | ||
The *${repo}* repo now has *${stargazers_count}* stars and *${forks_count}* forks!🎉 | ||
Your new fan is [${follwer}](${follwerUrl}) | ||
` | ||
async function sendTelegramPhoto(token, chatId, photoBuffer) { | ||
const formData = new FormData() | ||
formData.append('chat_id', chatId) | ||
formData.append('photo', new Blob([photoBuffer], { type: 'image/png' }), 'image.png') | ||
|
||
const response = await fetch(`https://api.telegram.org/bot${token}/sendPhoto`, { | ||
method: 'POST', | ||
body: formData | ||
}) | ||
if (!response.ok) { | ||
throw new Error('Failed to send photo to Telegram') | ||
} | ||
return response | ||
} |
Oops, something went wrong.