Last active
January 7, 2026 05:00
-
-
Save kopiro/23109a7e278906f8d0117f8421dab650 to your computer and use it in GitHub Desktop.
Claude Notify to Telegram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import json | |
| import subprocess | |
| import sys | |
| import urllib.parse | |
| import urllib.request | |
| CHAT_ID = "" | |
| BOT_TOKEN = "" | |
| if '--test' in sys.argv: | |
| with open('/tmp/claude-notify.json', 'r') as f: | |
| stdin = f.read() | |
| else: | |
| stdin = sys.stdin.read() | |
| with open('/tmp/claude-notify.json', 'w') as f: | |
| f.write(stdin) | |
| data = json.loads(stdin) | |
| question = data.get('tool_input', {}).get('questions', [{}])[0].get('question', '') | |
| # Build message with options if present | |
| options = data.get('tool_input', {}).get('questions', [{}])[0].get('options', []) | |
| if options: | |
| message_parts = [question, "", "Options:"] | |
| for i, opt in enumerate(options, 1): | |
| label = opt.get('label', '') | |
| desc = opt.get('description', '') | |
| message_parts.append(f"{i}. {label}") | |
| if desc: | |
| message_parts.append(f" {desc}") | |
| question = "\n".join(message_parts) | |
| question = f"Claude: {question}" | |
| print(f"Sending message to Telegram: {question}") | |
| url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage" | |
| post_data = urllib.parse.urlencode({ | |
| 'chat_id': CHAT_ID, | |
| 'text': question, | |
| }).encode() | |
| req = urllib.request.Request(url, data=post_data) | |
| try: | |
| with urllib.request.urlopen(req) as response: | |
| print(response.read().decode(), end='') | |
| except urllib.error.HTTPError as e: | |
| print(f"HTTP Error {e.code}: {e.reason}", file=sys.stderr) | |
| print(e.read().decode(), file=sys.stderr) | |
| except urllib.error.URLError as e: | |
| print(f"URL Error: {e.reason}", file=sys.stderr) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "hooks": { | |
| "PreToolUse": [ | |
| { | |
| "matcher": "AskUserQuestion", | |
| "hooks": [ | |
| { | |
| "type": "command", | |
| "command": "~/.bin/claude-notify.py" | |
| } | |
| ] | |
| } | |
| ] | |
| }, | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment