-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
45 lines (39 loc) · 1.49 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
import { getEventMessage } from "./github"
import { stores } from "./stores"
import { getMilestonePng } from "./utils"
import { sendTelegramMessage, sendTelegramPhoto } from "./telegram"
/**
* 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 })
}
if (!request.headers.has('X-GitHub-Event')) {
return new Response('Invalid github event', { status: 404 })
}
const githubEvent = request.headers.get('X-GitHub-Event')
try {
// 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)
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) {
console.error('Error send message:', error.message)
return new Response("Failed to handle github event.", { status: 500 })
}
}
}