See BLUEPRINT.md for a full, script-generated index of every function, command, callback, and cron job in this codebase, with source line numbers and verification instructions.
Telegram bot running on Cloudflare Workers, backed by Workers KV, implementing role-based access control (General / Admin / Owner) per the TeamMarySy Deployment Roadmap.
npm installnpx wrangler kv namespace create "TEAMMARYSY_KV"
npx wrangler kv namespace create "TEAMMARYSY_KV" --previewCopy the returned id and preview_id into wrangler.toml.
npx wrangler secret put TELEGRAM_BOT_TOKEN # from @BotFather
npx wrangler secret put WEBHOOK_SECRET # any random string you choose
npx wrangler secret put OWNER_TELEGRAM_ID # your numeric Telegram user IDnpx wrangler deployNote the deployed URL, e.g. https://teammarysy-bot.<subdomain>.workers.dev.
node scripts/set-webhook.mjs https://teammarysy-bot.<subdomain>.workers.dev <BOT_TOKEN> <WEBHOOK_SECRET>Use the same WEBHOOK_SECRET value you set as a Worker secret.
- Send
/startto your bot in Telegram — you should get the welcome message. - Send
/help— command list should reflect your role (Owner sees all tiers). npx wrangler tail— watch live logs while testing.
src/
index.ts Worker entry point (fetch + scheduled)
router.ts Central update router (auth -> validate -> authorize -> dispatch -> audit)
lib/
types.ts Env bindings, roles, Telegram + KV record types
telegram.ts Telegram Bot API client wrapper
kv.ts Workers KV data access layer
middleware/
auth.ts Identifies user/chat, upserts user record
rbac.ts Permission map + authorization checks
validation.ts Structural validation, arg/callback parsing
audit.ts Writes audit log entries to KV
handlers/
general.ts /start /help /announcements /events /ticket + callbacks
admin.ts /announce /createevent /createsurvey /schedules /tickets
owner.ts /setrole /logs /report /broadcast
scheduled/
jobs.ts Cron job implementations (reminders, refresh, cleanup)
scripts/
set-webhook.mjs One-off CLI helper to register the webhook
wrangler.toml Worker config, KV binding, cron schedule
| Tier | Can do |
|---|---|
| General | /start, /help, /announcements, /info, /faq, /events (alias /event), /survey <id>, /docs [category], /status, /ticket <subject> (alias /submit), join events, vote in surveys, browse FAQ/docs via inline buttons |
| Admin | Everything General can, plus /announce, /createevent (with optional capacity), /event cancel <id>, /createsurvey, /closesurvey, /schedules (alias /schedule, toggle active/inactive via button), /createschedule, /tickets (resolve via button), /resolve <id> | notes, /adddoc Title | content | category (delete via button), /addfaq Question | Answer |
| Owner | Everything Admin can, plus /setrole, /roles (staff directory + role changes), /permissions (view the full command permission matrix), /config get/set <key>, /logs, /report, /broadcast; overrides all permission checks |
The single source of truth for this mapping is src/middleware/rbac.ts
(COMMAND_PERMISSIONS / CALLBACK_PREFIX_PERMISSIONS).
The account matching the OWNER_TELEGRAM_ID secret always resolves to the
owner role, even before it has an existing KV user record.
| Key pattern | Contents |
|---|---|
user:{userId} |
UserRecord — role, join date, block status |
config:{key} |
Arbitrary JSON runtime configuration |
announcement:{id} |
AnnouncementRecord |
event:{id} |
EventRecord — includes optional capacity; joins beyond capacity are rejected |
survey:{id} |
SurveyRecord (with votes) |
schedule:{id} |
ScheduleRecord — active toggled via inline button or /createschedule |
ticket:{id} |
TicketRecord (includes resolvedBy/resolution/resolvedAt once closed via /resolve or the "Mark resolved" button) |
doc:{id} |
DocumentRecord — shared docs, optionally tagged with a category |
faq:{id} |
FaqRecord — question/answer pairs browsed via /faq |
log:{yyyy-mm-dd}:{ulid} |
LogRecord — audit trail, partitioned by day |
counter:{name} |
Plain numeric counters |
Configured in wrangler.toml under [triggers] and dispatched in
src/scheduled/jobs.ts:
*/30 * * * *— event reminders to attendees starting within the hour0 */4 * * *— refresh externally-sourced data (placeholder, extend as needed)0 3 * * *— purge audit log entries older than 30 days0 9 * * 1— weekly maintenance summary (open tickets, active schedules, doc count) sent to the Owner
- New command: add a handler function in the matching
handlers/*.tsfile, register it inCOMMAND_PERMISSIONS(rbac.ts) with its minimum role, then wire it intoCOMMAND_HANDLERSinrouter.ts. - New callback action: namespace
callback_dataasgen:action:...,admin:action:..., orowner:action:..., add the handler toCALLBACK_HANDLERSinrouter.ts. - New KV entity: add its type to
lib/types.tsand CRUD helpers tolib/kv.tsfollowing the existing key-prefix pattern.
Anything read via getConfig / stored via setConfig in lib/kv.ts lives in
Workers KV and can be changed by adding an admin command that calls
setConfig, without touching source code or redeploying the Worker — in line
with the roadmap's configuration-management goals.