-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
121 lines (110 loc) · 3.92 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
112
113
114
115
116
117
118
119
120
121
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const allowedOrigin = 'valid_url_here'; //your url goes here
// Handle CORS preflight request
if (request.method === 'OPTIONS') {
return handleOptions(request, allowedOrigin);
}
// Check if the request method is POST
if (request.method !== 'POST') {
return new Response('Method not allowed', {
status: 405,
headers: { 'Access-Control-Allow-Origin': allowedOrigin }
});
}
// Verify the origin of the request
const origin = request.headers.get('Origin');
if (origin !== allowedOrigin) {
return new Response('Forbidden', {
status: 403,
headers: { 'Access-Control-Allow-Origin': allowedOrigin }
});
}
// Parse the request body
const contentType = request.headers.get('Content-Type');
if (contentType.includes('application/json')) {
const json = await request.json();
return processFormData(json, allowedOrigin);
} else if (contentType.includes('application/x-www-form-urlencoded')) {
const formData = await request.formData();
const data = Object.fromEntries(formData);
return processFormData(data, allowedOrigin);
} else {
return new Response('Unsupported Media Type', {
status: 415,
headers: { 'Access-Control-Allow-Origin': allowedOrigin }
});
}
}
async function processFormData(data, allowedOrigin) {
// Format the message for Telegram using Markdown
let message = `*New contact form submission from ${data.pageTitle}*\n`;
message += `*Page URL:* [${data.pageTitle}](${data.pageUrl})\n`;
for (const [key, value] of Object.entries(data)) {
if (key !== 'pageUrl' && key !== 'pageTitle') {
message += `*${key}:*\n\`\`\`\n${value}\n\`\`\`\n`;
}
}
// Inline keyboard button
const inlineKeyboard = {
inline_keyboard: [
[
{
text: `Visit ${data.pageTitle}`,
url: data.pageUrl
}
]
]
};
// Send the message to Telegram
const telegramToken = TELEGRAM_API_KEY;
const chatId = TELEGRAM_CHAT_ID;
const telegramApiUrl = `https://api.telegram.org/bot${telegramToken}/sendMessage`;
try {
const response = await fetch(telegramApiUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_id: chatId,
text: message,
parse_mode: 'Markdown', // Specify Markdown formatting
reply_markup: inlineKeyboard // Attach inline keyboard
})
});
if (response.ok) {
return new Response(JSON.stringify({ message: 'Message sent successfully' }), {
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': allowedOrigin
},
status: 200
});
} else {
return new Response(JSON.stringify({ message: 'Failed to send message' }), {
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': allowedOrigin
},
status: 500
});
}
} catch (error) {
return new Response(JSON.stringify({ message: 'Failed to send message' }), {
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': allowedOrigin
},
status: 500
});
}
}
function handleOptions(request, allowedOrigin) {
const headers = {
'Access-Control-Allow-Origin': allowedOrigin,
'Access-Control-Allow-Methods': 'POST',
'Access-Control-Allow-Headers': 'Content-Type'
};
return new Response(null, { headers });
}