feat(telegram): load config from telegram.toml

This commit is contained in:
banteg
2025-12-28 20:37:54 +04:00
parent 0a984e228a
commit 6338c9f635
8 changed files with 187 additions and 34 deletions
+28 -5
View File
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.10"
# dependencies = ["requests", "markdown-it-py", "sulguk"]
# dependencies = ["requests", "markdown-it-py", "sulguk", "tomli; python_version < '3.11'"]
# ///
import json
import re
@@ -12,14 +12,37 @@ import requests
from markdown_it import MarkdownIt
from sulguk import transform_html
CREDS_PATH = Path.home() / ".codex" / "telegram.json"
CONFIG_PATH = Path.home() / ".codex" / "telegram.toml"
ERR_PATH = Path.home() / ".codex" / "telegram_last_error.txt"
def _load_toml(path: Path) -> dict:
if not path.exists():
return {}
try:
import tomllib # type: ignore[attr-defined]
except ModuleNotFoundError:
import tomli as tomllib # type: ignore[import-not-found]
return tomllib.loads(path.read_text(encoding="utf-8"))
def _config_get(config: dict, key: str):
if key in config:
return config[key]
nested = config.get("telegram")
if isinstance(nested, dict) and key in nested:
return nested[key]
return None
def main() -> None:
creds = json.loads(CREDS_PATH.read_text(encoding="utf-8"))
bot_token = creds["bot_token"]
chat_id = str(creds["chat_id"])
config = _load_toml(CONFIG_PATH)
bot_token = _config_get(config, "bot_token")
chat_id = _config_get(config, "chat_id")
if not bot_token or chat_id is None:
raise KeyError("telegram.toml must include bot_token and chat_id")
bot_token = str(bot_token)
chat_id = str(chat_id)
event = json.loads(sys.argv[1])
+4 -6
View File
@@ -6,15 +6,13 @@ Send Codex completion summaries to Telegram with safe Markdown rendering and sta
1. Ensure `uv` is installed.
2. Copy the script to `~/.codex/notify_telegram.py`.
3. Create your [Telegram creds](https://t.me/botfather) file at `~/.codex/telegram.json`.
3. Create your [Telegram creds](https://t.me/botfather) file at `~/.codex/telegram.toml`.
Example:
```json
{
"bot_token": "123456:ABCDEF...",
"chat_id": "462722"
}
```toml
bot_token = "123456:ABCDEF..."
chat_id = 462722
```
## Configure