Skip to content

Instantly share code, notes, and snippets.

@IceCodeNew
Forked from moesoha/worker.js
Created September 8, 2025 04:00
Show Gist options
  • Select an option

  • Save IceCodeNew/77bc57a68ebb8be75ddd4b52ed06f9a8 to your computer and use it in GitHub Desktop.

Select an option

Save IceCodeNew/77bc57a68ebb8be75ddd4b52ed06f9a8 to your computer and use it in GitHub Desktop.

Revisions

  1. @moesoha moesoha revised this gist Sep 10, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion worker.js
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,7 @@ const RequestHandlers = {
    if(message.new_chat_members) {
    for(const i in message.new_chat_members) {
    const u = message.new_chat_members[i];
    await requestTelegramBotAPI('kickChatMember', {
    await requestTelegramBotAPI('banChatMember', {
    chat_id: message.chat.id,
    user_id: u.id,
    until_date: Math.floor(new Date().getTime() / 1000) + 86400
  2. @moesoha moesoha revised this gist Oct 1, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions worker.js
    Original file line number Diff line number Diff line change
    @@ -18,14 +18,14 @@ const RequestHandlers = {
    if(!ManageToken || url.search !== `?${ManageToken}`) {
    return new Response('Access Denied', {status: 403});
    }
    const result = await TelegramBot.__request('deleteWebhook');
    const result = await requestTelegramBotAPI('deleteWebhook');
    return new Response(await result.text());
    },
    async '/tgWebhookSet'(_, url) {
    if(!ManageToken || url.search !== `?${ManageToken}`) {
    return new Response('Access Denied', {status: 403});
    }
    const result = await TelegramBot.__request('setWebhook', {
    const result = await requestTelegramBotAPI('setWebhook', {
    url: `https://${url.host}/tgPush`,
    allowed_updates: ['message']
    });
  3. @moesoha moesoha revised this gist Oct 1, 2020. No changes.
  4. @moesoha moesoha created this gist Oct 1, 2020.
    69 changes: 69 additions & 0 deletions worker.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    addEventListener('fetch', event => event.respondWith(handleRequest(event.request)));

    // Add environment variable `TGBOT_TOKEN` via Worker-Settings
    async function requestTelegramBotAPI(method, payload) {
    return fetch(`https://api.telegram.org/bot${TGBOT_TOKEN}/${method}`, {
    method: "POST",
    headers: {
    "Content-Type": "application/json; charset=utf-8"
    },
    body: !payload ? undefined : JSON.stringify(payload)
    });
    }

    // Add environment variable `MANAGE_TOKEN` via Worker-Settings
    const ManageToken = typeof MANAGE_TOKEN !== 'undefined' ? MANAGE_TOKEN : null;
    const RequestHandlers = {
    async '/tgWebhookDelete'(_, url) {
    if(!ManageToken || url.search !== `?${ManageToken}`) {
    return new Response('Access Denied', {status: 403});
    }
    const result = await TelegramBot.__request('deleteWebhook');
    return new Response(await result.text());
    },
    async '/tgWebhookSet'(_, url) {
    if(!ManageToken || url.search !== `?${ManageToken}`) {
    return new Response('Access Denied', {status: 403});
    }
    const result = await TelegramBot.__request('setWebhook', {
    url: `https://${url.host}/tgPush`,
    allowed_updates: ['message']
    });
    return new Response(await result.text());
    },
    async '/tgPush'(request) {
    if(request.method !== "POST") {
    return new Response('Bad Request', {status: 400});
    }
    const body = await request.json().catch(_ => ({}));
    const message = body.message;
    if(!message) {
    return;
    }
    if(message.new_chat_members) {
    for(const i in message.new_chat_members) {
    const u = message.new_chat_members[i];
    await requestTelegramBotAPI('kickChatMember', {
    chat_id: message.chat.id,
    user_id: u.id,
    until_date: Math.floor(new Date().getTime() / 1000) + 86400
    });
    }
    }
    if(message.left_chat_member || message.new_chat_members) {
    await requestTelegramBotAPI('deleteMessage', {
    chat_id: message.chat.id,
    message_id: message.message_id
    });
    }
    }
    }

    async function handleRequest(request) {
    const url = new URL(request.url);
    if(typeof RequestHandlers[url.pathname] !== 'function') {
    return new Response('Not Found', {status: 404});
    }
    const response = await RequestHandlers[url.pathname](request, url);
    return response || (new Response('WOW', {status: 200}));
    }