-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker.js
111 lines (97 loc) · 2.94 KB
/
worker.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const resolutionMap = {
"1080": "1920x1080.jpg",
"4K": "UHD.jpg",
};
addEventListener("scheduled", (event) => {
event.waitUntil(handleScheduledEvent());
});
async function handleScheduledEvent() {
try {
const wallpaper = await fetchBingWallpaper('cn', '4K');
await sendToTelegram(wallpaper);
await triggerGitHubWorkflow();
} catch (error) {
console.error(`Error: `, error);
}
}
async function sendToTelegram(wallpaper) {
const botToken = TG_BOT_TOKEN;
const chatId = TG_CHAT_ID;
const message = `${wallpaper.title}\n\n${wallpaper.copyright}\n\n原图:${wallpaper.imageUrl}`;
await fetch(
`https://api.telegram.org/bot${botToken}/sendPhoto`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
chat_id: chatId,
photo: wallpaper.imageUrl,
caption: message,
}),
}
);
}
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
try {
const params = new URL(request.url).searchParams;
const region = params.get("region") || "cn";
const resolution = params.get("resolution") || "4K";
const wallpaper = await fetchBingWallpaper(region, resolution);
return new Response(JSON.stringify({
"text": wallpaper.title + wallpaper.copyright.split(' \(©')[0],
"img": wallpaper.imageUrl
}), {
headers: { "Content-Type": "application/json" },
});
} catch (error) {
console.error(`Error handling request:`, error);
return new Response("Error handling request", { status: 500 });
}
}
async function fetchBingWallpaper(region, resolution) {
const languagePreferences = ['en-US', 'zh-CN'];
const headers = new Headers({
'Accept-Language': languagePreferences.join(', ')
});
const response = await fetch(
`https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&cc=${region}`,
{ headers }
);
const data = await response.json();
if (data.images.length === 0) {
throw new Error('No wallpaper available');
}
return {
imageUrl: "https://www.bing.com" + data.images[0].urlbase + "_" + resolutionMap[resolution],
date: data.images[0].enddate,
title: data.images[0].title,
copyright: data.images[0].copyright,
};
}
async function triggerGitHubWorkflow() {
const owner = OWNER;
const repo = REPO;
const workflowId = WORKFLOW_ID;
const token = GH_TOKEN;
const data = JSON.stringify({
"ref": "master",
"inputs": {
"info": "Cloudflare Workers"
}
});
const options = {
method: 'POST',
headers: {
'Accept': 'application/vnd.github+json',
'Authorization': `Bearer ${token}`,
'X-GitHub-Api-Version': '2022-11-28',
'Content-Type': 'application/json',
'User-Agent': 'Cloudflare Workers'
},
body: data
};
await fetch(`https://api.github.com/repos/${owner}/${repo}/actions/workflows/${workflowId}/dispatches`, options);
}