Building a Telegram Keyword Monitoring System with Cloudflare Workers and AI Agent
Preface
Previously, I implemented a Telegram keyword monitoring system.
It runs well.
https://t.me/FuckGFWNewbieNeedHelpToday I feel that all keyword messages are tiled together without distinction of importance, which is worth optimizing.
Prerequisites
Find @BotFather to apply for a bot, get the token, for example: 8984238292:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw
Already have a source of information, for example channel https://t.me/FuckGFWNewbieNeedHelp (-1002118991610)
Create a forum (which is the group topic feature) for example https://t.me/FuckGFWNewbieNeedHelpChat (-1002133460865)
Add the bot to the forum, set as admin
Agent-oriented development
muse.ai
Already learned Cloudflare CLI cf tool, omitted.
https://zelikk.blogspot.com/2026/10/cloudflare-cli-cf-oauth-agent-cloudflare.html
The content in the quote box below is the natural language command I sent to the Agent
Deploy a worker, operate the bot (8984238292:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw),Listen for new messages in channel (-1002118991610),Forward to forum (-1002133460865).Among them,If the message contains specific excluded keywords, do not forward to the forum,If the message contains specific keywords, forward to the corresponding topic,Otherwise forward to #General.The worker maintains a keyword list, stored using KV.The bot supports the following commands:/start Reply with command list/add_keyword <regex> - Add watch keyword/remove_keyword <regex> - Remove watch keyword/add_exclude <regex> - Add exclude keyword/remove_exclude <regex> - Remove exclude keyword/list_keywords - View all keywordsRegular expressions support the /keyword1|keyword2|keyword3/i syntax, the i at the end means case-insensitiveIt also supports the Keyword1|keyWord2|keyword3 syntax, meaning case-sensitiveWhen /add_keyword is called, create the corresponding topic in the forumWhen /remove_keyword is called, do not delete the topic in the forum. Just modify the topic name and prepend 🚫
Then it was roughly developed and deployed successfully.
Usage:
Private chat with the bot, send
/add_keyword crazypeace
/add_keyword /hermes|openclaw|dsh|爱马仕|龙虾/i
/add_keyword 链式代理|代理链|前置代理|落地代理
等等
Then just wait for the bot to forward the messages.
Issues found during testing and some detail adjustments
telebram bot cannot send messages anonymously in group/forum
Telegram bot can be set as admin with anonymous permissions in group/forum.
But in reality, the bot cannot send messages anonymously
Muse checked the Telegram official documentation and said it is an official restriction.
Sending /list_keywords gets a response, sending /start gets no response
Muse checked it, because the response text contains greater than and less than signs <>, an error occurred when replying in HTML format.
After escaping, the problem was solved.
Save KV, only save 1 KV record for watch keywords and exclude keywords
Asked muse to report how the entire system uses KV.
Muse said
Used 1 KV to save whether the message forwarding was successful, to handle retries in case forwarding fails;
Used 1 KV to save watch keywords,
Used 1 KV to save exclude keywords.
I requested to change it to:
Do not save whether forwarding is successful;
Use 1 KV to save watch keywords and exclude keywords.
This way,
for every message processed, KV is only read once;
KV is only written once when modifying watch keywords and exclude keywords.
When privately messaging the bot to send commands, the bot reported an error failing to create topic
After clearly describing the problem, muse self-checked and solved the problem.
The reason is: the code was written so that whichever session receives the command, the topic is created in that session.
I think my natural language instructions were quite clear. Whether sending commands to the bot in the forum or privately messaging the bot, the topic should be created in the forum. I think muse made a silly mistake when writing the code.
The Agent willfully called a non-existent Telegram API interface to modify the topic name
Building a Telegram bot via webhook makes debugging very convenient
Building a Telegram bot via webhook, the bot is passively called by the Telegram server via an HTTP interface to push messages over.
So when the agent is debugging such a bot, it can actively call the bot's HTTP interface to simulate "fake" messages. This is very convenient.
Github
https://github.com/crazypeace/tg-keyword-forward-bot-worker
Postscript
During the entire development process, I think muse made 2 silly mistakes.
1. Whichever session sends the command, create the topic in that session.
This is clearly a misunderstanding. No matter where the command is received, the topic should be created in the forum
2. When modifying the Telegram forum topic name, used a self-assumed API interface, causing hallucinations.
If the Telegram official API documentation was input during model training, then the model's "knowledge" should have the correct interface.
If not trained, then it should search the Telegram official documentation/knowledge base to find the correct interface.
I have a guess that the product side of muse deliberately adjusted the model's temperature and top k or some other settings, causing it to occasionally output some silly mistakes, like the 2 above, and then let the user correct them, extending the rounds of the entire task, and collecting training data for long tasks.





