A Cloudflare Worker that integrates with a Telegram Bot to filter spam and manage silence consensus polls.
-
Install dependencies:
npm install
-
Configure your environment variables in
wrangler.toml:[vars] TG_BOT_USERNAME = "your bot username" TG_SPAM_RE = "regular expression to detect spam message on username and text message" TG_ALLOWED_CHAT_USERNAMES = "allowed chat room to proceed" TG_SILENCE_CONSENSUS_COMMAND = "/silence" TG_SILENCE_CONSENSUS_MIN_COUNT = 3 TG_SILENCE_CONSENSUS_POSITIVE_RATIO = 0.7 TG_SILENCE_CONSENSUS_RESTRICT_DURATION = 259200 TG_SILENCE_CONSENSUS_POLL_DURATION = 86400
-
Set up your secrets using Wrangler:
npx wrangler secret put TG_BOT_TOKEN npx wrangler secret put TG_HOOK_SECRET
-
Set up D1 SQL
create D1 database on cloudflare
npx wrangler d1 create tg-bot-d1 # paste the result to wrangler.tomlcreate schema
# local npx wrangler d1 execute tg-bot-d1 --local --file=./schema/schema.sql # remote npx wrangler d1 execute tg-bot-d1 --remote --file=./schema/schema.sql
To run the worker locally:
npm run devTo deploy the worker to Cloudflare:
npm run deployThis requires you to be logged in with wrangler login or have your API token configured in ~/.wrangler/config.toml.
- Create a bot using BotFather (https://t.me/botfather) to get the token
- The Bot ID is the username of your bot without the @ symbol
- Hook Secret is a value you create to secure webhook endpoints (https://core.telegram.org/bots/api#setwebhook)
- Spam Regex is a regular expression pattern used to identify spam messages