Sending Telegram Task Notifications
When the user says exactly tg notif enable:
- Persist the flag to a state file (since sessions/turns are otherwise stateless): write
.claude/tg_notif.jsonwith{"enabled": true, "bot_token": "...", "chat_id": "..."}. - If bot token / chat ID aren't known yet, ask once for them (or check environment variables
TG_BOT_TOKEN/TG_CHAT_ID), then save to the state file. - Confirm: "Telegram notifications enabled. I'll notify you at the start and finish of every task."
- For every task from here on (this session and future ones, as long as the state file says enabled), send a Telegram message via the Bot API at start and at finish.
Concrete send mechanism (use whichever matches the environment):
- Shell/curl (default, works anywhere with network access):
Bash
curl -s -X POST "https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage" \ -d chat_id="${TG_CHAT_ID}" \ -d text="๐ข Task started: Refactor auth module to use JWT" - MCP tool, if a Telegram MCP server is connected (e.g., tool name
telegram_send_message): call it with{chat_id, text}. - Python (if a script context is more natural):
Python
import requests, os requests.post( f"https://api.telegram.org/bot{os.environ['TG_BOT_TOKEN']}/sendMessage", data={"chat_id": os.environ["TG_CHAT_ID"], "text": "๐ข Task started: ..."} )
Prefer curl for simplicity unless an MCP Telegram tool is already available in the environment โ check available tools first before falling back to curl.
Progress:
- Detect
tg notif enable/tg notif disable - Read/write persistent state file for the flag + credentials
- Verify a send mechanism exists (MCP tool, curl+credentials, or script)
- Send "start" notification before beginning each task (if enabled)
- Do the actual task work
- Send "finish" notification after task completes, success or failure (if enabled)
Step-by-step:
-
Detect command. Watch for
tg notif enable/tg notif disable(case-insensitive), as a standalone message. -
Persist state. On enable, write to
.claude/tg_notif.json:JSON{"enabled": true, "bot_token": "123456:ABC-DEF...", "chat_id": "987654321"}On disable, set
"enabled": false(keep credentials so re-enabling doesn't require re-asking). At the start of any new task, read this file first to decide whether to notify โ this is how the flag survives across turns/sessions without relying on conversation memory. -
Verify send mechanism before first use:
- Check for an available MCP tool with "telegram" in its name/description.
- Else check
TG_BOT_TOKEN/TG_CHAT_IDare present (env vars or state file). - If neither exists, notifications cannot be sent โ see fallback example below. Do not silently skip.
-
On each new task/prompt while enabled:
- Before starting work, send:
๐ข Task started: <short task summary>
- Before starting work, send:
-
After the task ends (success or failure):
- Success:
โ Task finished: <short task summary> โ <one-line result> - Failure:
โ Task failed: <short task summary> โ <one-line error/reason>
- Success:
-
If disabled or state file missing/unreadable, do nothing extra โ proceed with the task silently.
Example 1: Enabling Input:
tg notif enable
Output:
Telegram notifications enabled. I'll notify you at the start and finish of each task.
(Saved config to .claude/tg_notif.json)
Example 2: Successful task with notifications Input (after enabling):
Refactor the auth module to use JWT
Output:
[curl โ Telegram] ๐ข Task started: Refactor auth module to use JWT
... performs the refactor ...
[curl โ Telegram] โ
Task finished: Refactor auth module to use JWT โ all tests passing, 3 files changed
Example 3: Failed task with notifications Input (after enabling):
Deploy the app to production
Output:
[curl โ Telegram] ๐ข Task started: Deploy app to production
... deployment fails due to missing env var ...
[curl โ Telegram] โ Task failed: Deploy app to production โ missing PROD_DB_URL environment variable
Example 4: No integration configured (fallback path) Input (after enabling, but no bot token found):
Write unit tests for the payment module
Output:
โ ๏ธ Telegram notifications are enabled, but no send method is configured โ no MCP Telegram tool found and TG_BOT_TOKEN/TG_CHAT_ID are not set.
Please provide a bot token and chat ID (or set TG_BOT_TOKEN / TG_CHAT_ID env vars) so I can send notifications.
Proceeding with the task now without notifications:
... writes unit tests ...
Task complete: added 12 unit tests for payment module.
Example 5: Disabling Input:
tg notif disable
Output:
Telegram notifications disabled. (Config updated: enabled=false)
- Check available tools for an existing Telegram MCP integration before defaulting to raw curl.
- Keep messages short: one line for start, one line for finish with outcome.
- Send the start notification before doing any work, not after.
- Always send a finish notification, even on failure โ use โ instead of โ and state the reason.
- Store credentials once in the state file so the user isn't asked repeatedly across sessions.
- For long multi-step tasks, one start/finish pair is enough unless the user asks for per-step updates.
- Don't hand-wave the send mechanism โ always use a concrete method (curl to Bot API, MCP tool call, or script) shown above.
- Don't rely on in-memory-only state โ conversation context resets; use the state file to persist the enabled flag and credentials.
- Don't skip the finish notification on failure; silence on error is worse than silence on success.
- Don't keep notifying after
tg notif disableโ always re-check the state file before sending. - Don't assume credentials exist โ verify and fall back to asking the user explicitly, then continue the task anyway.