Convert Telegram Anti-Ad Bot to Cloudflare Worker
Preface
Previously I implemented a simple anti-advertising bot for personal use in Python, featuring a join-group verification function.
Recently my server expired, and suddenly an idea crossed my mind — can I convert it to a Cloudflare Worker?
Agent-Oriented Development
Hermes connects to the public welfare site agentrouter's deepseek-v4-flash* This development wasn't a simple one-shot. In fact, I didn't have the final outcome in mind from the start. I gradually figured out my requirements throughout the entire process.
The following records some thoughts on the development approach.
Cloudflare's Worker is a stateless service. If you need to record any information, you have to use either KV or D1.
The free plan's KV allows 1000 writes per day.
My original Python approach uses a question bank (although it only has 3 questions, haha), randomly picking a question from it. When picking a question for each user, it generates the answer, then checks each time whether the user's answer matches the stored answer.
So, saving this answer requires writing to KV, meaning 1 KV write per user.
At first I thought, what if I compute the answer in real-time every time? That way I don't need to save the answer. But then I'd need to save which question the user is answering, which still requires 1 KV write.
Alright, going further, I could make which question the user answers pseudo-random, so it can be derived from something like the user's userid.
Going even further, I could combine the current date with the user's userid to form a pseudo-random seed, so the question might change after a day.
My original Python approach sends notification messages in the group, notifying users to complete the verification, and also sends a notification with the result once verification is passed. It also has a feature to automatically delete these notification messages on a schedule.
In the Worker approach, I have to write to KV to save the IDs of messages to be deleted in the future. Then use Worker's scheduled cron trigger to drive the Worker to check KV and delete messages.
If there's 1 notification when joining the group and 1 notification when verification is passed, that's 2 KV writes per user.
The approach I ultimately chose:
1. For the join-group notification, use Telegram Group Welcome Message Setup — only the person joining can see it, other group members won't.
2. The verification-passed message is very short. Of course, you can also choose not to send this notification.
In summary, the entire join-group verification Telegram anti-advertising bot doesn't use KV at all. So with the free plan Worker's daily quota of 100K invocations, there's nothing to worry about.
Github
https://github.com/crazypeace/tg-join-group-exam-bot-worker