telert 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- telert/__init__.py +2 -0
- telert/cli.py +167 -0
- telert-0.1.1.dist-info/METADATA +167 -0
- telert-0.1.1.dist-info/RECORD +8 -0
- telert-0.1.1.dist-info/WHEEL +5 -0
- telert-0.1.1.dist-info/entry_points.txt +2 -0
- telert-0.1.1.dist-info/licenses/LICENSE +21 -0
- telert-0.1.1.dist-info/top_level.txt +1 -0
telert/__init__.py
ADDED
telert/cli.py
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
telert – Send Telegram alerts from shell commands.
|
|
4
|
+
Supports two modes:
|
|
5
|
+
• **run** mode wraps a command, captures exit status & timing.
|
|
6
|
+
• **filter** mode reads stdin so you can pipe long jobs.
|
|
7
|
+
|
|
8
|
+
Run `telert --help` or `telert help` for full usage.
|
|
9
|
+
"""
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
import argparse, json, pathlib, subprocess, sys, textwrap, time, requests, os
|
|
12
|
+
|
|
13
|
+
CFG_DIR = pathlib.Path(os.path.expanduser("~/.config/telert"))
|
|
14
|
+
CFG_FILE = CFG_DIR / "config.json"
|
|
15
|
+
|
|
16
|
+
# ───────────────────────────────── helpers ──────────────────────────────────
|
|
17
|
+
|
|
18
|
+
def _save(token: str, chat_id: str):
|
|
19
|
+
CFG_DIR.mkdir(parents=True, exist_ok=True)
|
|
20
|
+
CFG_FILE.write_text(json.dumps({"token": token.strip(), "chat_id": str(chat_id).strip()}))
|
|
21
|
+
print("✔ Configuration saved →", CFG_FILE)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _load():
|
|
25
|
+
if not CFG_FILE.exists():
|
|
26
|
+
sys.exit("❌ telert is unconfigured – run `telert config …` first.")
|
|
27
|
+
return json.loads(CFG_FILE.read_text())
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _send(msg: str):
|
|
31
|
+
cfg = _load()
|
|
32
|
+
url = f"https://api.telegram.org/bot{cfg['token']}/sendMessage"
|
|
33
|
+
r = requests.post(url, json={"chat_id": cfg["chat_id"], "text": msg})
|
|
34
|
+
if r.status_code != 200:
|
|
35
|
+
sys.exit(f"❌ Telegram API error {r.status_code}: {r.text}")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _human(sec: float) -> str:
|
|
39
|
+
m, s = divmod(int(sec), 60)
|
|
40
|
+
return f"{m} m {s} s" if m else f"{s} s"
|
|
41
|
+
|
|
42
|
+
# ────────────────────────────── sub‑commands ───────────────────────────────
|
|
43
|
+
|
|
44
|
+
def do_config(a):
|
|
45
|
+
_save(a.token, a.chat_id)
|
|
46
|
+
|
|
47
|
+
def do_status(_):
|
|
48
|
+
cfg = _load()
|
|
49
|
+
print("token :", cfg['token'][:8] + "…")
|
|
50
|
+
print("chat :", cfg['chat_id'])
|
|
51
|
+
_send("✅ telert status OK")
|
|
52
|
+
print("sent : test message")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def do_hook(a):
|
|
56
|
+
t = a.longer_than
|
|
57
|
+
print(textwrap.dedent(f"""
|
|
58
|
+
telert_preexec() {{ TELERT_CMD=\"$BASH_COMMAND\"; TELERT_START=$EPOCHSECONDS; }}
|
|
59
|
+
telert_precmd() {{ local st=$?; local d=$((EPOCHSECONDS-TELERT_START));
|
|
60
|
+
if (( d >= {t} )); then telert send \"$TELERT_CMD exited $st in $(printf '%dm%02ds' $((d/60)) $((d%60)))\"; fi; }}
|
|
61
|
+
trap telert_preexec DEBUG
|
|
62
|
+
PROMPT_COMMAND=telert_precmd:$PROMPT_COMMAND
|
|
63
|
+
""").strip())
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def do_send(a):
|
|
67
|
+
_send(a.text)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def do_run(a):
|
|
71
|
+
start = time.time()
|
|
72
|
+
proc = subprocess.run(a.cmd, text=True, capture_output=True)
|
|
73
|
+
dur = _human(time.time() - start)
|
|
74
|
+
status = proc.returncode
|
|
75
|
+
label = a.label or " ".join(a.cmd)
|
|
76
|
+
if a.only_fail and status == 0:
|
|
77
|
+
sys.stdout.write(proc.stdout)
|
|
78
|
+
sys.stderr.write(proc.stderr)
|
|
79
|
+
sys.exit(status)
|
|
80
|
+
|
|
81
|
+
msg = a.message or f"{label} finished with exit {status} in {dur}"
|
|
82
|
+
if proc.stdout.strip():
|
|
83
|
+
msg += "
|
|
84
|
+
|
|
85
|
+
--- stdout ---
|
|
86
|
+
" + "
|
|
87
|
+
".join(proc.stdout.splitlines()[:20])[:3900]
|
|
88
|
+
if proc.stderr.strip():
|
|
89
|
+
msg += "
|
|
90
|
+
|
|
91
|
+
--- stderr ---
|
|
92
|
+
" + "
|
|
93
|
+
".join(proc.stderr.splitlines()[:20])[:3900]
|
|
94
|
+
_send(msg)
|
|
95
|
+
|
|
96
|
+
sys.stdout.write(proc.stdout)
|
|
97
|
+
sys.stderr.write(proc.stderr)
|
|
98
|
+
sys.exit(status)
|
|
99
|
+
|
|
100
|
+
# ───────────────────────────── pipeline filter ─────────────────────────────
|
|
101
|
+
|
|
102
|
+
def piped_mode():
|
|
103
|
+
data = sys.stdin.read()
|
|
104
|
+
msg = sys.argv[1] if len(sys.argv) > 1 else "Pipeline finished"
|
|
105
|
+
if len(sys.argv) > 2:
|
|
106
|
+
msg += f" (exit {sys.argv[2]})"
|
|
107
|
+
if data.strip():
|
|
108
|
+
msg += "
|
|
109
|
+
|
|
110
|
+
--- output ---
|
|
111
|
+
" + "
|
|
112
|
+
".join(data.splitlines()[:20])[:3900]
|
|
113
|
+
_send(msg)
|
|
114
|
+
|
|
115
|
+
# ──────────────────────────────── entrypoint ───────────────────────────────
|
|
116
|
+
|
|
117
|
+
def main():
|
|
118
|
+
if not sys.stdin.isatty():
|
|
119
|
+
piped_mode()
|
|
120
|
+
return
|
|
121
|
+
|
|
122
|
+
p = argparse.ArgumentParser(
|
|
123
|
+
prog="telert",
|
|
124
|
+
description="Send Telegram alerts when commands finish (supports exit status).",
|
|
125
|
+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
126
|
+
)
|
|
127
|
+
sp = p.add_subparsers(dest="cmd", required=True)
|
|
128
|
+
|
|
129
|
+
# config
|
|
130
|
+
c = sp.add_parser("config", help="store bot token & chat‑id")
|
|
131
|
+
c.add_argument("--token", required=True)
|
|
132
|
+
c.add_argument("--chat-id", required=True)
|
|
133
|
+
c.set_defaults(func=do_config)
|
|
134
|
+
|
|
135
|
+
# status
|
|
136
|
+
st = sp.add_parser("status", help="send test message & show config")
|
|
137
|
+
st.set_defaults(func=do_status)
|
|
138
|
+
|
|
139
|
+
# hook
|
|
140
|
+
hk = sp.add_parser("hook", help="emit Bash hook for all commands")
|
|
141
|
+
hk.add_argument("--longer-than", "-l", type=int, default=10)
|
|
142
|
+
hk.set_defaults(func=do_hook)
|
|
143
|
+
|
|
144
|
+
# send
|
|
145
|
+
sd = sp.add_parser("send", help="send arbitrary text")
|
|
146
|
+
sd.add_argument("text")
|
|
147
|
+
sd.set_defaults(func=do_send)
|
|
148
|
+
|
|
149
|
+
# run
|
|
150
|
+
rn = sp.add_parser("run", help="run a command & notify when done")
|
|
151
|
+
rn.add_argument("--label", "-L", help="friendly name")
|
|
152
|
+
rn.add_argument("--message", "-m", help="override default text")
|
|
153
|
+
rn.add_argument("--only-fail", action="store_true", help="notify only on non‑zero exit")
|
|
154
|
+
rn.add_argument("cmd", nargs=argparse.REMAINDER, help="command to execute -- required")
|
|
155
|
+
rn.set_defaults(func=do_run)
|
|
156
|
+
|
|
157
|
+
# help alias
|
|
158
|
+
hp = sp.add_parser("help", help="show global help")
|
|
159
|
+
hp.set_defaults(func=lambda _a: p.print_help())
|
|
160
|
+
|
|
161
|
+
args = p.parse_args()
|
|
162
|
+
if getattr(args, "cmd", None) == [] and getattr(args, "func", None) is do_run:
|
|
163
|
+
p.error("run: missing command – use telert run -- <cmd> …")
|
|
164
|
+
args.func(args)
|
|
165
|
+
|
|
166
|
+
if __name__ == "__main__":
|
|
167
|
+
main()
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: telert
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: CLI Telegram notifier: run commands or use as pipeline
|
|
5
|
+
Author-email: Mihir Khandekar <mihirkhandekar@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/navig-me/telert
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/navig-me/telert/issues
|
|
9
|
+
Keywords: telegram,notification,cli,terminal,alert
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Environment :: Console
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Intended Audience :: System Administrators
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Topic :: Communications
|
|
21
|
+
Classifier: Topic :: System :: Monitoring
|
|
22
|
+
Classifier: Topic :: Utilities
|
|
23
|
+
Requires-Python: >=3.8
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: requests>=2.25
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: build; extra == "dev"
|
|
29
|
+
Requires-Dist: twine; extra == "dev"
|
|
30
|
+
Requires-Dist: pytest; extra == "dev"
|
|
31
|
+
Dynamic: license-file
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
# telert – Telegram Alerts for Your Terminal
|
|
35
|
+
|
|
36
|
+
**Version 0.1.1**
|
|
37
|
+
|
|
38
|
+
Telert is a tiny command‑line utility that shoots you a Telegram message the moment a command (or an entire shell pipeline) finishes.
|
|
39
|
+
It preserves **exit status**, captures **run‑time**, and can optionally include the first lines of _stdout_ / _stderr_.
|
|
40
|
+
|
|
41
|
+
[](https://www.buymeacoffee.com/mihirk)
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## ✨ Features
|
|
46
|
+
|
|
47
|
+
| Mode | What it does | Example |
|
|
48
|
+
|----------------|--------------|---------|
|
|
49
|
+
| **Run** | Wraps a command, times it, sends notification with exit code. | `telert run --label "RSYNC" -- rsync -a /src /dst` |
|
|
50
|
+
| **Filter** | Reads from stdin so you can _pipe_ output. | `long_job | telert "compile done"` |
|
|
51
|
+
| **Hook** | Generates a Bash snippet so **every** command > *N* seconds notifies automatically. | `eval "$(telert hook -l 30)"` |
|
|
52
|
+
| **Send** | Low‑level "send arbitrary text to myself" helper. | `telert send "server rebooted"` |
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## 🚀 Quick install
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
# Any OS (PyPI)
|
|
60
|
+
pip install telert
|
|
61
|
+
|
|
62
|
+
# Fedora / RHEL / Alma / Rocky
|
|
63
|
+
sudo dnf install telert # yum works too
|
|
64
|
+
|
|
65
|
+
# Debian / Ubuntu / Linux Mint
|
|
66
|
+
sudo apt install telert
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
RPM/DEB repositories are published by GitHub Actions on every release tag.
|
|
70
|
+
If you prefer, you can also grab the single‑file wheel from Releases.
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## 🤖 Bot set‑up (one‑time)
|
|
75
|
+
|
|
76
|
+
1. Chat with **@BotFather → /newbot →** copy the *API token*.
|
|
77
|
+
2. Send any throwaway message to the new bot **from the account/channel you want alerts in**.
|
|
78
|
+
3. Fetch the chat‑ID:
|
|
79
|
+
```bash
|
|
80
|
+
curl -s "https://api.telegram.org/bot<token>/getUpdates" | jq .
|
|
81
|
+
```
|
|
82
|
+
Copy the integer in `result[0].message.chat.id` (for channels it starts with `-100`).
|
|
83
|
+
4. Configure Telert:
|
|
84
|
+
```bash
|
|
85
|
+
telert config --token <token> --chat-id <chat_id>
|
|
86
|
+
telert status # sends a ✅ test message
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Telert stores creds in `~/.config/telert/config.json`.
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
## 🛠 Usage
|
|
94
|
+
|
|
95
|
+
### Run mode (recommended)
|
|
96
|
+
```bash
|
|
97
|
+
# Notify regardless of outcome
|
|
98
|
+
$ telert run --label " nightly backup " -- rsync -a /src /dst
|
|
99
|
+
# custom message & only if the command fails
|
|
100
|
+
telert run --only-fail -m "backup failed" -- rsync -a /src /dst
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Filter mode (legacy piping)
|
|
104
|
+
```bash
|
|
105
|
+
make big‑pdf | telert "LaTeX finished"
|
|
106
|
+
```
|
|
107
|
+
> ⚠️ Exit status is **unknown** here – a shell can’t pass `$?` inside a pipe.
|
|
108
|
+
> If you need exit status, stick to **run** mode or explicit `; telert send "failed $?"`.
|
|
109
|
+
|
|
110
|
+
### Shell hook
|
|
111
|
+
```bash
|
|
112
|
+
echo 'eval "$(telert hook -l 20)"' >> ~/.bashrc # any cmd >20 s notifies
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Help
|
|
116
|
+
```bash
|
|
117
|
+
telert --help # global help
|
|
118
|
+
telert run --help # sub‑command help
|
|
119
|
+
telert help # alias to the same
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
## 🌿 Environment variables
|
|
125
|
+
|
|
126
|
+
| Variable | Effect |
|
|
127
|
+
|---------------------|---------------------------------------------|
|
|
128
|
+
| `TELERT_LONG` | Default threshold (seconds) for `hook`. |
|
|
129
|
+
| `TELERT_SILENT=1` | Suppress stdout/stderr echo in `run`. |
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
## 👩💻 Development
|
|
134
|
+
```bash
|
|
135
|
+
git clone https://github.com/navig-me/telert
|
|
136
|
+
cd telert
|
|
137
|
+
python -m pip install -e .[dev]
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Releasing to PyPI
|
|
141
|
+
|
|
142
|
+
The project is automatically published to PyPI when a new GitHub release is created:
|
|
143
|
+
|
|
144
|
+
1. Update version in both `pyproject.toml` and `telert/__init__.py`
|
|
145
|
+
2. Commit the changes and push to main
|
|
146
|
+
3. Create a new GitHub release with a tag like `v0.1.1`
|
|
147
|
+
4. The GitHub Actions workflow will automatically build and publish to PyPI
|
|
148
|
+
|
|
149
|
+
To manually publish to PyPI if needed:
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
# Install build tools
|
|
153
|
+
pip install build twine
|
|
154
|
+
|
|
155
|
+
# Build the package
|
|
156
|
+
python -m build
|
|
157
|
+
|
|
158
|
+
# Upload to PyPI
|
|
159
|
+
twine upload dist/*
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## 🤝 Contributing / License
|
|
165
|
+
|
|
166
|
+
PRs & issues welcome!
|
|
167
|
+
Licensed under the MIT License – see `LICENSE`.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
telert/__init__.py,sha256=DPHyWzMNBvG0_yw7E6c_1kjVGI6yVJgRCagiNsKhBj8,47
|
|
2
|
+
telert/cli.py,sha256=qe9u0lW2_Zx9Xts22k3JGPLwoIDpCAcr5IrIHAb73J0,5696
|
|
3
|
+
telert-0.1.1.dist-info/licenses/LICENSE,sha256=2-giacH55b-0BuoZuavQksuKUJCYqqUOcW1KhLYbpkc,1065
|
|
4
|
+
telert-0.1.1.dist-info/METADATA,sha256=HlKGsUnKb2qkrjYk_7Wmv-t3dG9YOysDBK4EFJv0998,5085
|
|
5
|
+
telert-0.1.1.dist-info/WHEEL,sha256=pxyMxgL8-pra_rKaQ4drOZAegBVuX-G_4nRHjjgWbmo,91
|
|
6
|
+
telert-0.1.1.dist-info/entry_points.txt,sha256=aVekCXlGpyRpH396szbgKYSTN-4fXGoH_YUjuuW40lM,43
|
|
7
|
+
telert-0.1.1.dist-info/top_level.txt,sha256=hRI01ONcgODxE3yZEW0l3kRgt8nzyvq0H3gQrRg_OeU,7
|
|
8
|
+
telert-0.1.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 navig.me
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
telert
|