Auto-Delete Telegram Bot Messages with Cloudflare Workers

Preface

I set up a keyword-reply bot in the group. There are tons of projects on Github. Skipping that.
But none of them had a feature to auto-delay-delete reply messages.
I always assumed that a Worker only runs when there's an HTTP request — meaning it only runs when someone sends a message in the group. So naturally, it couldn't handle delayed message deletion.

Inspiration

Today an idea suddenly hit me, so I asked the AI, "how do you implement scheduled tasks with a Cloudflare Worker?" 

Then came the boring process of GPT-driven development.
The pasted code is a Telegram bot based on Cloudflare Worker.  
I want to implement a feature that deletes the bot's reply messages after a delay of 5–10 minutes.

I bound a KV namespace to this Worker:  BOT_MSG, used to store data about sent messages.

I set up a cron trigger on this Worker running every 5 minutes.

Please improve this code.

Let me paste the code here.
const TOKEN = 'your_bot_token'
const WEBHOOK = '/endpoint'
const SECRET = 'you_should_generate_random_string'

const DELETE_AFTER_MS = 5 * 60 * 1000 // 5 minutes

/**
* Store sent messages in KV
* key: msg:{delete_at}:{chat_id}:{message_id}
*/
async function saveMessage(env, chatId, messageId) {
const deleteAt = String(Date.now() + DELETE_AFTER_MS).padStart(16, '0')
const key = `msg:${deleteAt}:${chatId}:${messageId}`
await env.BOT_MSG.put(key, '1', {
expirationTtl: 20 * 60 // 20-minute fallback cleanup
})
}

/**
* Cron trigger: delete all expired messages
*/
async function handleScheduled(env) {
const now = Date.now()
const { keys } = await env.BOT_MSG.list({ prefix: 'msg:' })

for (const { name } of keys) {
// key format: msg:{delete_at}:{chat_id}:{message_id}
const [, deleteAt, chatId, messageId] = name.split(':')
if (parseInt(deleteAt) <= now) {
await deleteMessage(chatId, messageId)
await env.BOT_MSG.delete(name)
}
}
}

/**
* Call Telegram API to delete a message
*/
async function deleteMessage(chatId, messageId) {
return (await fetch(apiUrl('deleteMessage', {
chat_id: chatId,
message_id: messageId
}))).json()
}

// ─── Export ────

export default {
async fetch(request, env, ctx) {
const url = new URL(request.url)
if (url.pathname === WEBHOOK) {
return handleWebhook(request, env)
} else if (url.pathname === '/registerWebhook') {
return registerWebhook(url, WEBHOOK, SECRET)
} else if (url.pathname === '/unRegisterWebhook') {
return unRegisterWebhook()
} else {
return new Response('No handler for this request')
}
},

async scheduled(event, env, ctx) {
ctx.waitUntil(handleScheduled(env))
}
}

// ─── Webhook ────

async function handleWebhook(request, env) {
if (request.headers.get('X-Telegram-Bot-Api-Secret-Token') !== SECRET) {
return new Response('Unauthorized', { status: 403 })
}
const update = await request.json()
await onUpdate(update, env)
return new Response('Ok')
}

async function onUpdate(update, env) {
if ('message' in update) {
await onMessage(update.message, env)
}
}

// ─── Message Handler ────
async function onMessage(message, env) {
const text = message.text.toLowerCase();
const originalText = message.text;

// Detect English keywords
const hasHappy = text.includes('happy');
const hasNew = text.includes('new');
const hasYear = text.includes('year');

if (hasHappy && hasNew && hasYear) {
const result = await sendPlainText(message.chat.id, 'Happy New Year to you, too! 🎁', message.message_id);
if (result?.ok && result?.result?.message_id) {
await saveMessage(env, message.chat.id, result.result.message_id)
}
return;
}

// Detect Chinese keywords
const has新 = originalText.includes('新');
const has年 = originalText.includes('年');
const has快 = originalText.includes('快');
const has乐 = originalText.includes('乐');

if (has新 && has年 && has快 && has乐) {
const result = await sendPlainText(message.chat.id, 'Also wishing you a Happy New Year! 🧧', message.message_id);
if (result?.ok && result?.result?.message_id) {
await saveMessage(env, message.chat.id, result.result.message_id)
}
return;
}

// 233 group script keyword
const has脚本 = originalText.includes('脚本');
if (has脚本) {
const replyText = `
Install sing-box script (first run installs Reality protocol by default):
<code>bash &lt;(wget -qO- -o- https://github.com/233boy/sing-box/raw/main/install.sh)</code>

Install Xray script (first run installs Reality protocol by default):
<code>bash &lt;(wget -qO- -o- https://github.com/233boy/Xray/raw/main/install.sh)</code>
<tg-spoiler>
Install V2Ray script (do not use; first run installs VMESS+TCP by default):
bash &lt;(wget -qO- -o- https://github.com/233boy/v2ray/raw/master/install.sh)
</tg-spoiler>
`;
const result = await sendPlainText(message.chat.id, replyText, message.message_id);
if (result?.ok && result?.result?.message_id) {
await saveMessage(env, message.chat.id, result.result.message_id)
}
return;
}
}

// ─── Telegram API Helpers ────

async function sendPlainText(chatId, text, replyToMessageId = null) {
return (await fetch(apiUrl('sendMessage', {
chat_id: chatId,
text,
reply_to_message_id: replyToMessageId,
parse_mode: 'HTML'
}))).json()
}

async function registerWebhook(requestUrl, suffix, secret) {
const webhookUrl = `${requestUrl.protocol}//${requestUrl.hostname}${suffix}`
const r = await (await fetch(apiUrl('setWebhook', { url: webhookUrl, secret_token: secret }))).json()
return new Response('ok' in r && r.ok ? 'Ok' : JSON.stringify(r, null, 2))
}

async function unRegisterWebhook() {
const r = await (await fetch(apiUrl('setWebhook', { url: '' }))).json()
return new Response('ok' in r && r.ok ? 'Ok' : JSON.stringify(r, null, 2))
}

function apiUrl(methodName, params = null) {
let query = ''
if (params) {
query = '?' + new URLSearchParams(params).toString()
}
return `https://api.telegram.org/bot${TOKEN}/${methodName}${query}`
}

========

Postscript

The GPT used this time was



Comments