|
addEventListener('fetch', event => { |
|
event.respondWith(handleRequest(event.request)); |
|
}) |
|
|
|
const SENDKEY = 'your_sendkey_here'; |
|
const MASTER_ID = 'your_master_id_here'; |
|
const TELEGRAM_BOT_TOKEN = 'your_telegram_bot_token_here'; |
|
|
|
async function handleRequest(request) { |
|
const url = new URL(request.url); |
|
const pathname = url.pathname; |
|
const searchParams = new URLSearchParams(url.search); |
|
|
|
if (!pathname.endsWith(`/${SENDKEY}.send`)) { |
|
return new Response('Unauthorized', { status: 401 }); |
|
} |
|
|
|
const title = searchParams.get('title'); |
|
const desp = searchParams.get('desp'); |
|
|
|
if (!title || !desp) { |
|
return new Response('Missing title or desp', { status: 400 }); |
|
} |
|
|
|
// Escape special characters for MarkdownV2 |
|
const escapeMarkdown = (text) => { |
|
return text.replace(/([*_[\]()~`>#+\-=|{}.!])/g, '\\$1'); |
|
} |
|
|
|
const escapedTitle = escapeMarkdown(title); |
|
const escapedDesp = escapeMarkdown(desp); |
|
|
|
// Use MarkdownV2 to format the title in bold |
|
const message = `*${escapedTitle}*\n\n${escapedDesp}`; |
|
const telegramApiUrl = `https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage`; |
|
|
|
const telegramResponse = await fetch(telegramApiUrl, { |
|
method: 'POST', |
|
headers: { |
|
'Content-Type': 'application/json' |
|
}, |
|
body: JSON.stringify({ |
|
chat_id: MASTER_ID, |
|
text: message, |
|
parse_mode: 'MarkdownV2' |
|
}) |
|
}); |
|
|
|
if (telegramResponse.ok) { |
|
return new Response('Message sent successfully', { status: 200 }); |
|
} else { |
|
const errorData = await telegramResponse.json(); |
|
return new Response(`Failed to send message: ${errorData.description}`, { status: 500 }); |
|
} |
|
} |
SENDKEY
MASTER_ID
andTELEGRAM_BOT_TOKEN