feat(telegram): load config from telegram.toml
This commit is contained in:
@@ -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])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user