由于部分网络原因,使用tg的推送API不是很稳定,网络问题第一想到的就是Cloudflare,那就叫Chatgpt写一个吧。
const telegramBotToken = "2222222:xxxxxxxxx";
const telegramBotUserId = "11111111";
const MODE = 'HTML';
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
try {
const params = new URL(request.url).searchParams;
const pushMessage = params.get('pushMessage') || "Default push message";
const url = `https://api.telegram.org/bot${telegramBotToken}/sendMessage`;
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: `chat_id=${telegramBotUserId}&parse_mode=${MODE}&text=${pushMessage}`
};
const response = await fetch(url, requestOptions);
if (response.ok) {
return new Response("TG推送成功", { status: 200 });
} else {
throw new Error(`TG推送失败,HTTP状态码: ${response.status}`);
}
} catch (error) {
console.error("Error in Worker:", error);
return new Response(`TG_api请求失败: ${error.message}`, { status: 500 });
}
}
用AI做AI的事,2分钟完事。
当然,如果有需要,再给这个worker添加一个自定义域名,使用起来更方便。