Skip to content

Instantly share code, notes, and snippets.

@moesoha
Last active September 8, 2025 04:00
Show Gist options
  • Select an option

  • Save moesoha/feb6041b85cc7a9a960ff333d5220677 to your computer and use it in GitHub Desktop.

Select an option

Save moesoha/feb6041b85cc7a9a960ff333d5220677 to your computer and use it in GitHub Desktop.
Auto kick any joining users from Telegram group (deployed on Cloudflare Worker)
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 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 requestTelegramBotAPI('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('banChatMember', {
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}));
}
@moesoha
Copy link
Copy Markdown
Author

moesoha commented Oct 2, 2020

How to use

  1. Set up a new bot at @BotFather in Telegram or use an existing one
  2. Create a worker on https://workers.cloudflare.com/
  3. In the "Settings" panel of the worker, set two environment variable TGBOT_TOKEN (Telegram bot token generated by @BotFather ) and MANAGE_TOKEN (we will use it in the next step)
  4. Use quick edit of the worker, then paste the code into the code area
  5. Request https://<worker_address>/tgWebhookSet?<MANAGE_TOKEN> to set the Telegram bot webhook URL
  6. Add the bot to the group and promote it with the two permissions: "Delete messages" and "Ban users"

@IceCodeNew
Copy link
Copy Markdown

How to use

  1. Create a worker on workers.cloudflare.com
  2. Set two environment variable TGBOT_TOKEN and MANAGE_TOKEN in the "Settings" panel of the worker
  3. Use quick edit of the worker, then paste the code into the code area
  4. Request https://<worker_address>/tgWebhookSet?<MANAGE_TOKEN> to set the Telegram bot webhook url.

Should also turn off Privacy Mode so that the bot can have access to the group message.

image
image

@ovrase
Copy link
Copy Markdown

ovrase commented Oct 3, 2020

如何使用

  1. worker.cloudflare.com上创建工人
  2. 设置两个环境变量,TGBOT_TOKENMANAGE_TOKEN在工作人员的“设置”面板中
  3. 使用辅助程序的快速编辑,然后将代码粘贴到代码区域中
  4. 要求https://<worker_address>/tgWebhookSet?<MANAGE_TOKEN>设置Telegram机器人Webhook网址。

还应该关闭,Privacy Mode以便漫游器可以访问组消息。

图片
图片

When the BOT is an admin, it will have this permission by default.

@moesoha
Copy link
Copy Markdown
Author

moesoha commented Oct 3, 2020

我倒是忘了还要给 bot 加上管理员这一句了(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment