-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
76 lines (67 loc) · 2.34 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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 })
}
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 })
}
}
}
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
}
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
}