-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (37 loc) · 1.04 KB
/
index.js
File metadata and controls
41 lines (37 loc) · 1.04 KB
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
/**
* Telegram API Forwarder for Cloudflare Workers
* @author Max Base
* @version 1.0
*/
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
/**
* Handles incoming requests, forwards them to the Telegram API,
* and returns the API's response.
*
* @param {Request} request - The incoming fetch request.
* @returns {Promise<Response>} The response from the Telegram API, or an error response.
*/
async function handleRequest(request) {
try {
const url = new URL(request.url);
url.hostname = 'api.telegram.org';
const newRequest = new Request(url.toString(), {
method: request.method,
headers: request.headers,
body: request.body,
redirect: 'follow'
});
const response = await fetch(newRequest);
return response;
} catch (error) {
console.error('Error forwarding request:', error);
return new Response('Telegram API Forwarder Error', {
status: 500,
headers: {
'content-type': 'text/plain; charset=utf-8',
},
});
}
}