Telegram Anti-Ad Bot: Python to Cloudflare Worker
Foreword
Previously, I implemented a simple anti-advertising bot for personal use with Python, adding a message verification feature.
Recently, my server expired, and a thought suddenly occurred to me: can it be converted into 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. I did not have the final effect fully planned from the beginning either. I gradually clarified my requirements throughout the process.
The following records some thoughts on the development approach.
The issues considered for the join-group verification bot are omitted here.
My original Python solution had a whitelist list, and 用户userid values that passed verification were saved in it. This way, when the bot found a new message in the group, it would check whether the sender was in the whitelist.
This information had to be saved; there was no way around it. So I used a KV.
As a result, during testing, I found that KV has a state-latency problem for Workers.
Specifically:
1. The user completed the entire verification process. There was a whitelist record in KV.
2. I used the Cloudflare dashboard to remove the user from the KV whitelist record, then triggered another test.
3. The user spoke in the group and was not muted.
4. After a few minutes, when the user spoke again, they were muted (the correct, expected result).
The good news is that in the normal flow, after the Worker saved the user in the KV whitelist record and the user then sent a message, everything worked normally.
In other words, after the Worker operated on KV, if it accessed KV again within a very short time, it used the same KV data it had just operated on.
But! I have to say but!
Heaven knows how my weird brain worked. I actually thought of Telegram users being able to set custom tags. This is a new feature opened this year; previously only group admins could set custom tags, but this year it was opened to all group members.
So, I can use whether a custom tag exists as the information for determining whitelist status.
Then there is no need to operate KV. Ha! Ha! Ha!
* My group is a VPN group, so I let the script set the FQer custom tag. If your group is a fan group, you can also set it to TUTU's Friend or something like that. Or if your group is for playing with Agents, you can set it to CLAWer haha
My original Python solution had a feature: after muting a user, it forwarded the messages the user sent in the group to a storage channel; after the user completed verification, it forwarded the messages from the storage channel back into the group. To save the correspondence between
用户userid and 仓库频道中的消息id, writing to KV was required. I really like this feature, so I kept it, even though it consumes KV.If you want zero KV operations, just don't bind this KV. (Correspondingly, you also don't need to configure the STORAGE_CHANNEL_ID variable.)
Github
https://github.com/crazypeace/tg-send-msg-exam-bot-worker