This repository has been archived on 2025-08-02. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
flexpatrol.ru-form-handler/index.ts
2024-02-04 13:48:24 +03:00

51 lines
1.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const PORT = process.env.PORT
const BOT_TOKEN = process.env.BOT_TOKEN as string
const CHAT_ID = process.env.CHAT_ID as string
const server = Bun.serve({
port: PORT,
async fetch(req) {
if (!req.body) {
return new Response(
'Bad request',
{
status: 400,
headers: {
"Content-Type": "text/html",
},
}
)
}
const submittedData = await req.json();
const text = encodeURIComponent(`
*Новое письмо с flexpatrol.ru*
\`\`\`
${JSON.stringify(submittedData, null, 2)}
\`\`\``)
try {
await fetch(`https://api.telegram.org/bot${BOT_TOKEN}/sendMessage?chat_id=${CHAT_ID}&text=${text}&parse_mode=markdown`)
} catch (error) {
return new Response(
'Internal server error',
{
status: 500,
headers: {
"Content-Type": "text/html",
},
}
)
}
return new Response('success', {
headers: {
'Access-Control-Allow-Origin': '*'
}
});
}
})
console.log(`Listening on http://localhost:${server.port}...`);