Telegram anti-ad bot: migrating tg-send-msg-exam-bot-worker to Cloudflare Workers to implement posting verification

Introduction

Previously, I implemented a simple personal anti-spam bot in Python with a speaking-verification feature.

Recently, my server subscription expired, and I suddenly had the idea: could it be converted into a Cloudflare Worker?

Developing for Agents

Connecting Hermes to DeepSeek-v4-flash from a free public service

* This development was not a simple one-shot. I hadn’t figured out the final result from the very beginning, either. I gradually clarified my requirements throughout the process.

Here are some notes on the development approach.

The issues considered for the group-join verification bot are omitted here.

My original Python implementation had a whitelist containing the users who had passed verification. When the bot detected a new message in the group, it checked whether the sender was on the whitelist.

This information needs to be stored; there’s no getting around that. So I used a KV store.

However, during testing, I discovered that KV has state-propagation latency issues for Workers.

Specifically:

  1. A user completed the entire verification process. The whitelist record was in KV.
  2. I deleted the user from the KV whitelist through the Cloudflare dashboard, then triggered another test.
  3. The user sent a message in the group but was not muted.
  4. A few minutes later, the user sent another message and was muted (the correct, expected result).

The good news is that in the normal flow, the Worker saves the user to the KV whitelist, and the user can then send messages normally.
In other words, when the Worker accesses KV again shortly after modifying it, it uses the KV data from its own most recent operation.

But! Here comes the “but”!

Who knows what was going on in my crazy brain. I actually remembered that Telegram users can set custom tags. This is a new feature introduced this year: previously, only group administrators could set custom tags, but now the feature is available to all group members.
So I can use the presence of a custom tag to determine whether someone is on the whitelist.

That eliminates the need to use KV. Ha! Ha! Ha!

* My group is for users who bypass internet censorship, so I have the script set the custom tag to FQer. If your group is a fan group, you could set it to TUTU's Friend or something similar. Or, if your group is for people who use Agents, you could set it to CLAWer, haha.

image

One feature of my original Python implementation was that, after muting a user, it forwarded the user’s messages in the group to an archive channel. Once the user completed verification, it forwarded the messages from the archive channel back to the group. Saving the mapping between the user userid and the message id in the archive channel requires writing to KV. I really like this feature, so I kept it despite the KV usage.

If you want KV operations to incur zero usage, simply don’t bind this KV namespace. (Accordingly, you also don’t need to configure the STORAGE_CHANNEL_ID variable.)

GitHub

1 Like

Positive reviews, but I’m getting annoyed by all the private messages.