Convert Telegram Anti-Ad Bot to Cloudflare Worker
Foreword
Previously I implemented a simple anti-advertising bot for personal use in Python, implementing join-group verification functionality.
Recently my server expired, and suddenly a thought came to me: can it be converted to a Cloudflare Worker?
Agent-Oriented Development
Hermes connects to the free public-interest site agentrouter's deepseek-v4-flash* This development was not a simple one-shot. As for the final effect itself, I did not have it all figured out from the beginning. I gradually clarified my requirements throughout the process.
The following records some ideas for development approaches.
Cloudflare's Worker is a stateless service; if you need to record information, you have to use either KV or D1.
The free plan's KV allows 1,000 writes per day.
My original Python solution was a question bank (although it only had 3 questions, haha), from which questions are randomly selected. When selecting a question for each user, the answer is generated, and each time the user's response is checked against the answer.
Then, saving this 答案 requires writing KV, which means each user requires 1 KV write
At first I thought, what if I calculate this answer in real time every time? Then the answer would not be saved. But then I would need to save 用户正在回答哪个题目, which still requires 1 KV write
Okay, going further, make 用户回答哪个题目 pseudo-random, so it can be computed from, for example, 用户的userid.
Going further, combine 当前日期 and 用户的userid as a pseudo-random seed, so that after a day the question may change.
My original Python solution would send notification messages in the group, notifying users to perform verification, and would also send a notification explaining the result after verification passed. It also had a feature to automatically delete these notification messages on a timer.
In the Worker solution, I must write KV to save the IDs of messages to be deleted in the future. Then use the Worker's cron timer to drive the Worker to check KV and delete the messages.
If 1 notification is sent when a user joins and 1 notification is sent when verification passes, then each user requires 2 KV writes.
The solution I ultimately chose is:
1. For join notifications, use Telegram group settings Welcome Message only the person joining will see it; other group members will not see it.
2. The verification-passed message is very short. Of course, you can also choose not to send this notification message.
In summary, the entire join-group verification Telegram anti-advertising bot uses no KV at all. Then the free plan Worker's 100K daily invocation quota is completely worry-free.
Github
https://github.com/crazypeace/tg-join-group-exam-bot-worker