tiny-tg 0.1.0__tar.gz
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.
- tiny_tg-0.1.0/.env.example +2 -0
- tiny_tg-0.1.0/.gitignore +76 -0
- tiny_tg-0.1.0/.python-version +1 -0
- tiny_tg-0.1.0/LICENSE +21 -0
- tiny_tg-0.1.0/PKG-INFO +210 -0
- tiny_tg-0.1.0/README.md +196 -0
- tiny_tg-0.1.0/pyproject.toml +26 -0
- tiny_tg-0.1.0/tiny_tg/__init__.py +4 -0
- tiny_tg-0.1.0/tiny_tg/cli.py +39 -0
- tiny_tg-0.1.0/tiny_tg/telegram.py +44 -0
- tiny_tg-0.1.0/tiny_tg/utils.py +24 -0
- tiny_tg-0.1.0/uv.lock +109 -0
tiny_tg-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# Python-generated files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[oc]
|
|
4
|
+
*.pyo
|
|
5
|
+
*.pyd
|
|
6
|
+
.Python
|
|
7
|
+
build/
|
|
8
|
+
develop-eggs/
|
|
9
|
+
dist/
|
|
10
|
+
downloads/
|
|
11
|
+
eggs/
|
|
12
|
+
.eggs/
|
|
13
|
+
lib/
|
|
14
|
+
lib64/
|
|
15
|
+
parts/
|
|
16
|
+
sdist/
|
|
17
|
+
var/
|
|
18
|
+
wheels/
|
|
19
|
+
*.egg-info/
|
|
20
|
+
*.egg
|
|
21
|
+
MANIFEST
|
|
22
|
+
|
|
23
|
+
# Virtual environments
|
|
24
|
+
.venv
|
|
25
|
+
venv/
|
|
26
|
+
ENV/
|
|
27
|
+
env/
|
|
28
|
+
|
|
29
|
+
# Environment variables (CRITICAL - contains API keys)
|
|
30
|
+
.env
|
|
31
|
+
.env.local
|
|
32
|
+
.env.*.local
|
|
33
|
+
|
|
34
|
+
# Log files
|
|
35
|
+
*.log
|
|
36
|
+
logs/
|
|
37
|
+
|
|
38
|
+
# Testing and coverage
|
|
39
|
+
.pytest_cache/
|
|
40
|
+
.coverage
|
|
41
|
+
.coverage.*
|
|
42
|
+
htmlcov/
|
|
43
|
+
.tox/
|
|
44
|
+
.nox/
|
|
45
|
+
coverage.xml
|
|
46
|
+
*.cover
|
|
47
|
+
|
|
48
|
+
# Type checking and linting
|
|
49
|
+
.mypy_cache/
|
|
50
|
+
.dmypy.json
|
|
51
|
+
dmypy.json
|
|
52
|
+
.ruff_cache/
|
|
53
|
+
.pytype/
|
|
54
|
+
|
|
55
|
+
# IDE/Editor files
|
|
56
|
+
.vscode/
|
|
57
|
+
.idea/
|
|
58
|
+
*.swp
|
|
59
|
+
*.swo
|
|
60
|
+
*~
|
|
61
|
+
.project
|
|
62
|
+
.pydevproject
|
|
63
|
+
|
|
64
|
+
# Jupyter Notebook
|
|
65
|
+
.ipynb_checkpoints/
|
|
66
|
+
*.ipynb_checkpoints
|
|
67
|
+
|
|
68
|
+
# OS files
|
|
69
|
+
.DS_Store
|
|
70
|
+
Thumbs.db
|
|
71
|
+
|
|
72
|
+
# Local development
|
|
73
|
+
*.local
|
|
74
|
+
.cache/
|
|
75
|
+
tmp/
|
|
76
|
+
temp/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.11
|
tiny_tg-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 rickhehe
|
|
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.
|
tiny_tg-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tiny-tg
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Simple Telegram notification library
|
|
5
|
+
Project-URL: Homepage, https://github.com/rickhehe/tiny-tg
|
|
6
|
+
Project-URL: Repository, https://github.com/rickhehe/tiny-tg
|
|
7
|
+
Author-email: rickhehe <rick@rickhehe.com>
|
|
8
|
+
License: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Python: >=3.11
|
|
11
|
+
Requires-Dist: httpx
|
|
12
|
+
Requires-Dist: python-dotenv
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# tiny-tg
|
|
16
|
+
|
|
17
|
+
[](https://badge.fury.io/py/tiny-tg)
|
|
18
|
+
[](https://www.python.org/downloads/)
|
|
19
|
+
[](https://opensource.org/licenses/MIT)
|
|
20
|
+
|
|
21
|
+
A simple, lightweight Python library for sending Telegram notifications. Perfect for automation scripts, server monitoring, and quick alerts.
|
|
22
|
+
|
|
23
|
+
## Features
|
|
24
|
+
|
|
25
|
+
- 🚀 **Simple** - One command to send messages
|
|
26
|
+
- 📦 **Tiny** - Minimal dependencies (just `httpx` and `python-dotenv`)
|
|
27
|
+
- 🔧 **Flexible** - Use as CLI tool or Python library
|
|
28
|
+
- ⚡ **Fast** - Direct API calls, no bloat
|
|
29
|
+
- 🐍 **Modern** - Python 3.11+ with type hints
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
### As a Library (for Python projects)
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
# Using uv (recommended)
|
|
37
|
+
uv add tiny-tg
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### As a CLI Tool (system-wide)
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
# Using uv (recommended - isolated environment)
|
|
44
|
+
uv tool install tiny-tg
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
After `uv tool install`, the `tg` command is available globally without activating a virtual environment.
|
|
48
|
+
|
|
49
|
+
## Setup
|
|
50
|
+
|
|
51
|
+
1. **Create a Telegram Bot:**
|
|
52
|
+
- Message [@BotFather](https://t.me/botfather) on Telegram
|
|
53
|
+
- Send `/newbot` and follow the prompts
|
|
54
|
+
- Copy your API token
|
|
55
|
+
|
|
56
|
+
2. **Get Your Chat ID:**
|
|
57
|
+
- Message [@userinfobot](https://t.me/userinfobot) to get your chat ID
|
|
58
|
+
- Or message your bot and visit: `https://api.telegram.org/bot<YOUR_TOKEN>/getUpdates`
|
|
59
|
+
|
|
60
|
+
3. **Configure Environment:**
|
|
61
|
+
|
|
62
|
+
Create a `.env` file in your project root:
|
|
63
|
+
|
|
64
|
+
```env
|
|
65
|
+
TELEGRAM_API_KEY=your_bot_token_here
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Usage
|
|
69
|
+
|
|
70
|
+
### Command Line
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
# Send a message
|
|
74
|
+
tg CHAT_ID "Hello from tiny-tg!"
|
|
75
|
+
|
|
76
|
+
# With custom timeout
|
|
77
|
+
tg CHAT_ID "Server is down!" --timeout 30
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Python API
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from tiny_tg import send_message
|
|
84
|
+
|
|
85
|
+
# Send a notification
|
|
86
|
+
send_message(
|
|
87
|
+
chat_id=123456789,
|
|
88
|
+
text="Deployment complete! ✅"
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
# With custom timeout
|
|
92
|
+
send_message(
|
|
93
|
+
chat_id=123456789,
|
|
94
|
+
text="Critical alert!",
|
|
95
|
+
timeout=30
|
|
96
|
+
)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Examples
|
|
100
|
+
|
|
101
|
+
### Cron Job Notifications
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
# Daily reminder at 09:00
|
|
105
|
+
0 9 * * * /path/to/venv/bin/tg 123456789 "Daily backup complete"
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Script Integration
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
from tiny_tg import send_message
|
|
112
|
+
|
|
113
|
+
def backup_database():
|
|
114
|
+
try:
|
|
115
|
+
# ... backup logic ...
|
|
116
|
+
send_message(123456789, "✅ Backup successful")
|
|
117
|
+
except Exception as e:
|
|
118
|
+
send_message(123456789, f"❌ Backup failed: {e}")
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Server Monitoring
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
import psutil
|
|
125
|
+
from tiny_tg import send_message
|
|
126
|
+
|
|
127
|
+
CHAT_ID = 123456789
|
|
128
|
+
|
|
129
|
+
# Check disk space
|
|
130
|
+
disk = psutil.disk_usage('/')
|
|
131
|
+
if disk.percent > 90:
|
|
132
|
+
send_message(CHAT_ID, f"⚠️ Disk usage: {disk.percent}%")
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Raspberry Pi Alerts
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
from tiny_tg import send_message
|
|
139
|
+
import subprocess
|
|
140
|
+
|
|
141
|
+
def check_temperature():
|
|
142
|
+
temp = subprocess.check_output(['vcgencmd', 'measure_temp'])
|
|
143
|
+
temp_c = float(temp.decode().split('=')[1].split("'")[0])
|
|
144
|
+
|
|
145
|
+
if temp_c > 70:
|
|
146
|
+
send_message(123456789, f"🌡️ High temp: {temp_c}°C")
|
|
147
|
+
|
|
148
|
+
check_temperature()
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Project Structure
|
|
152
|
+
|
|
153
|
+
```
|
|
154
|
+
tiny-tg/
|
|
155
|
+
├── tiny_tg/
|
|
156
|
+
│ ├── __init__.py # Package exports
|
|
157
|
+
│ ├── telegram.py # Core messaging logic
|
|
158
|
+
│ ├── utils.py # Config utilities
|
|
159
|
+
│ └── cli.py # Command-line interface
|
|
160
|
+
├── pyproject.toml # Project configuration
|
|
161
|
+
├── .env # API credentials (gitignored)
|
|
162
|
+
└── README.md
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Configuration
|
|
166
|
+
|
|
167
|
+
### Environment Variables
|
|
168
|
+
|
|
169
|
+
| Variable | Required | Description |
|
|
170
|
+
|----------|----------|-------------|
|
|
171
|
+
| `TELEGRAM_API_KEY` | Yes | Your Telegram bot token from @BotFather |
|
|
172
|
+
|
|
173
|
+
### Function Parameters
|
|
174
|
+
|
|
175
|
+
#### `send_message(chat_id, text, timeout=10)`
|
|
176
|
+
|
|
177
|
+
| Parameter | Type | Default | Description |
|
|
178
|
+
|-----------|------|---------|-------------|
|
|
179
|
+
| `chat_id` | `int \| str` | - | Telegram chat ID or username |
|
|
180
|
+
| `text` | `str` | - | Message text to send |
|
|
181
|
+
| `timeout` | `int` | `10` | Request timeout in seconds |
|
|
182
|
+
|
|
183
|
+
**Returns:** `bool` - `True` if successful, `False` otherwise
|
|
184
|
+
|
|
185
|
+
**Raises:** `httpx.RequestError` - If the API request fails
|
|
186
|
+
|
|
187
|
+
## Requirements
|
|
188
|
+
|
|
189
|
+
- Python 3.11+
|
|
190
|
+
- `httpx` - Modern HTTP client
|
|
191
|
+
- `python-dotenv` - Environment variable management
|
|
192
|
+
|
|
193
|
+
## License
|
|
194
|
+
|
|
195
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
|
196
|
+
|
|
197
|
+
## Contributing
|
|
198
|
+
|
|
199
|
+
Contributions welcome! Please feel free to submit a Pull Request.
|
|
200
|
+
|
|
201
|
+
## Links
|
|
202
|
+
|
|
203
|
+
- **Homepage:** https://github.com/rickhehe/tiny-tg
|
|
204
|
+
- **Issues:** https://github.com/rickhehe/tiny-tg/issues
|
|
205
|
+
- **PyPI:** https://pypi.org/project/tiny-tg/
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
Keep it simple.
|
|
210
|
+
Make it happen.
|
tiny_tg-0.1.0/README.md
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
# tiny-tg
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/py/tiny-tg)
|
|
4
|
+
[](https://www.python.org/downloads/)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
A simple, lightweight Python library for sending Telegram notifications. Perfect for automation scripts, server monitoring, and quick alerts.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- 🚀 **Simple** - One command to send messages
|
|
12
|
+
- 📦 **Tiny** - Minimal dependencies (just `httpx` and `python-dotenv`)
|
|
13
|
+
- 🔧 **Flexible** - Use as CLI tool or Python library
|
|
14
|
+
- ⚡ **Fast** - Direct API calls, no bloat
|
|
15
|
+
- 🐍 **Modern** - Python 3.11+ with type hints
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
### As a Library (for Python projects)
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# Using uv (recommended)
|
|
23
|
+
uv add tiny-tg
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### As a CLI Tool (system-wide)
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
# Using uv (recommended - isolated environment)
|
|
30
|
+
uv tool install tiny-tg
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
After `uv tool install`, the `tg` command is available globally without activating a virtual environment.
|
|
34
|
+
|
|
35
|
+
## Setup
|
|
36
|
+
|
|
37
|
+
1. **Create a Telegram Bot:**
|
|
38
|
+
- Message [@BotFather](https://t.me/botfather) on Telegram
|
|
39
|
+
- Send `/newbot` and follow the prompts
|
|
40
|
+
- Copy your API token
|
|
41
|
+
|
|
42
|
+
2. **Get Your Chat ID:**
|
|
43
|
+
- Message [@userinfobot](https://t.me/userinfobot) to get your chat ID
|
|
44
|
+
- Or message your bot and visit: `https://api.telegram.org/bot<YOUR_TOKEN>/getUpdates`
|
|
45
|
+
|
|
46
|
+
3. **Configure Environment:**
|
|
47
|
+
|
|
48
|
+
Create a `.env` file in your project root:
|
|
49
|
+
|
|
50
|
+
```env
|
|
51
|
+
TELEGRAM_API_KEY=your_bot_token_here
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Usage
|
|
55
|
+
|
|
56
|
+
### Command Line
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
# Send a message
|
|
60
|
+
tg CHAT_ID "Hello from tiny-tg!"
|
|
61
|
+
|
|
62
|
+
# With custom timeout
|
|
63
|
+
tg CHAT_ID "Server is down!" --timeout 30
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Python API
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
from tiny_tg import send_message
|
|
70
|
+
|
|
71
|
+
# Send a notification
|
|
72
|
+
send_message(
|
|
73
|
+
chat_id=123456789,
|
|
74
|
+
text="Deployment complete! ✅"
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
# With custom timeout
|
|
78
|
+
send_message(
|
|
79
|
+
chat_id=123456789,
|
|
80
|
+
text="Critical alert!",
|
|
81
|
+
timeout=30
|
|
82
|
+
)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Examples
|
|
86
|
+
|
|
87
|
+
### Cron Job Notifications
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
# Daily reminder at 09:00
|
|
91
|
+
0 9 * * * /path/to/venv/bin/tg 123456789 "Daily backup complete"
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Script Integration
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
from tiny_tg import send_message
|
|
98
|
+
|
|
99
|
+
def backup_database():
|
|
100
|
+
try:
|
|
101
|
+
# ... backup logic ...
|
|
102
|
+
send_message(123456789, "✅ Backup successful")
|
|
103
|
+
except Exception as e:
|
|
104
|
+
send_message(123456789, f"❌ Backup failed: {e}")
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Server Monitoring
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
import psutil
|
|
111
|
+
from tiny_tg import send_message
|
|
112
|
+
|
|
113
|
+
CHAT_ID = 123456789
|
|
114
|
+
|
|
115
|
+
# Check disk space
|
|
116
|
+
disk = psutil.disk_usage('/')
|
|
117
|
+
if disk.percent > 90:
|
|
118
|
+
send_message(CHAT_ID, f"⚠️ Disk usage: {disk.percent}%")
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Raspberry Pi Alerts
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
from tiny_tg import send_message
|
|
125
|
+
import subprocess
|
|
126
|
+
|
|
127
|
+
def check_temperature():
|
|
128
|
+
temp = subprocess.check_output(['vcgencmd', 'measure_temp'])
|
|
129
|
+
temp_c = float(temp.decode().split('=')[1].split("'")[0])
|
|
130
|
+
|
|
131
|
+
if temp_c > 70:
|
|
132
|
+
send_message(123456789, f"🌡️ High temp: {temp_c}°C")
|
|
133
|
+
|
|
134
|
+
check_temperature()
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Project Structure
|
|
138
|
+
|
|
139
|
+
```
|
|
140
|
+
tiny-tg/
|
|
141
|
+
├── tiny_tg/
|
|
142
|
+
│ ├── __init__.py # Package exports
|
|
143
|
+
│ ├── telegram.py # Core messaging logic
|
|
144
|
+
│ ├── utils.py # Config utilities
|
|
145
|
+
│ └── cli.py # Command-line interface
|
|
146
|
+
├── pyproject.toml # Project configuration
|
|
147
|
+
├── .env # API credentials (gitignored)
|
|
148
|
+
└── README.md
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Configuration
|
|
152
|
+
|
|
153
|
+
### Environment Variables
|
|
154
|
+
|
|
155
|
+
| Variable | Required | Description |
|
|
156
|
+
|----------|----------|-------------|
|
|
157
|
+
| `TELEGRAM_API_KEY` | Yes | Your Telegram bot token from @BotFather |
|
|
158
|
+
|
|
159
|
+
### Function Parameters
|
|
160
|
+
|
|
161
|
+
#### `send_message(chat_id, text, timeout=10)`
|
|
162
|
+
|
|
163
|
+
| Parameter | Type | Default | Description |
|
|
164
|
+
|-----------|------|---------|-------------|
|
|
165
|
+
| `chat_id` | `int \| str` | - | Telegram chat ID or username |
|
|
166
|
+
| `text` | `str` | - | Message text to send |
|
|
167
|
+
| `timeout` | `int` | `10` | Request timeout in seconds |
|
|
168
|
+
|
|
169
|
+
**Returns:** `bool` - `True` if successful, `False` otherwise
|
|
170
|
+
|
|
171
|
+
**Raises:** `httpx.RequestError` - If the API request fails
|
|
172
|
+
|
|
173
|
+
## Requirements
|
|
174
|
+
|
|
175
|
+
- Python 3.11+
|
|
176
|
+
- `httpx` - Modern HTTP client
|
|
177
|
+
- `python-dotenv` - Environment variable management
|
|
178
|
+
|
|
179
|
+
## License
|
|
180
|
+
|
|
181
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
|
182
|
+
|
|
183
|
+
## Contributing
|
|
184
|
+
|
|
185
|
+
Contributions welcome! Please feel free to submit a Pull Request.
|
|
186
|
+
|
|
187
|
+
## Links
|
|
188
|
+
|
|
189
|
+
- **Homepage:** https://github.com/rickhehe/tiny-tg
|
|
190
|
+
- **Issues:** https://github.com/rickhehe/tiny-tg/issues
|
|
191
|
+
- **PyPI:** https://pypi.org/project/tiny-tg/
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
Keep it simple.
|
|
196
|
+
Make it happen.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "tiny-tg"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Simple Telegram notification library"
|
|
5
|
+
authors = [{name = "rickhehe", email = "rick@rickhehe.com"}]
|
|
6
|
+
license = {text = "MIT"}
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
requires-python = ">=3.11"
|
|
9
|
+
dependencies = [
|
|
10
|
+
"httpx",
|
|
11
|
+
"python-dotenv",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
[project.urls]
|
|
15
|
+
Homepage = "https://github.com/rickhehe/tiny-tg"
|
|
16
|
+
Repository = "https://github.com/rickhehe/tiny-tg"
|
|
17
|
+
|
|
18
|
+
[project.scripts]
|
|
19
|
+
tg = "tiny_tg.cli:main"
|
|
20
|
+
|
|
21
|
+
[build-system]
|
|
22
|
+
requires = ["hatchling"]
|
|
23
|
+
build-backend = "hatchling.build"
|
|
24
|
+
|
|
25
|
+
[tool.hatch.build.targets.wheel]
|
|
26
|
+
packages = ["tiny_tg"]
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""Command-line interface for tiny-tg."""
|
|
2
|
+
import sys
|
|
3
|
+
import argparse
|
|
4
|
+
from typing import NoReturn
|
|
5
|
+
|
|
6
|
+
from .telegram import send_message
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def main() -> NoReturn:
|
|
10
|
+
"""Main CLI entry point."""
|
|
11
|
+
parser = argparse.ArgumentParser(
|
|
12
|
+
prog='tg',
|
|
13
|
+
description='Send notifications via Telegram'
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
parser.add_argument('chat_id', help='Telegram chat ID')
|
|
17
|
+
parser.add_argument('text', help='Message text to send')
|
|
18
|
+
parser.add_argument(
|
|
19
|
+
'--timeout',
|
|
20
|
+
type=int,
|
|
21
|
+
default=10,
|
|
22
|
+
help='API request timeout in seconds (default: 10)'
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
args = parser.parse_args()
|
|
26
|
+
|
|
27
|
+
try:
|
|
28
|
+
success = send_message(
|
|
29
|
+
chat_id=args.chat_id,
|
|
30
|
+
text=args.text,
|
|
31
|
+
timeout=args.timeout
|
|
32
|
+
)
|
|
33
|
+
sys.exit(0 if success else 1)
|
|
34
|
+
except Exception as e:
|
|
35
|
+
print(f"Error: {e}", file=sys.stderr)
|
|
36
|
+
sys.exit(1)
|
|
37
|
+
|
|
38
|
+
if __name__ == '__main__':
|
|
39
|
+
main()
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import httpx
|
|
2
|
+
|
|
3
|
+
from .utils import get_telegram_api_key
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def send_message(
|
|
7
|
+
chat_id: int | str,
|
|
8
|
+
text: str,
|
|
9
|
+
timeout: int = 10
|
|
10
|
+
) -> bool:
|
|
11
|
+
|
|
12
|
+
"""
|
|
13
|
+
Send a message to a Telegram chat using the Bot API.
|
|
14
|
+
Args:
|
|
15
|
+
chat_id (int | str): The unique identifier for the target chat or username of the target channel.
|
|
16
|
+
text (str): The message text to send.
|
|
17
|
+
timeout (int, optional): Timeout for the API request in seconds. Defaults to 10.
|
|
18
|
+
Returns:
|
|
19
|
+
bool: True if the message was sent successfully, False otherwise.
|
|
20
|
+
Raises:
|
|
21
|
+
httpx.RequestError: If there was an error with the API request.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
api_key = get_telegram_api_key()
|
|
25
|
+
url = f'https://api.telegram.org/bot{api_key}/sendMessage'
|
|
26
|
+
|
|
27
|
+
params = {
|
|
28
|
+
'chat_id': chat_id,
|
|
29
|
+
'text': text
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
try:
|
|
33
|
+
response = httpx.get(url, params=params, timeout=timeout)
|
|
34
|
+
response.raise_for_status()
|
|
35
|
+
|
|
36
|
+
result = response.json()
|
|
37
|
+
if result.get('ok'):
|
|
38
|
+
return True
|
|
39
|
+
else:
|
|
40
|
+
return False
|
|
41
|
+
|
|
42
|
+
except httpx.RequestError as e:
|
|
43
|
+
print(f"✗ Error sending message: {e}")
|
|
44
|
+
raise
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# utils.py
|
|
2
|
+
import os
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from dotenv import load_dotenv, find_dotenv
|
|
5
|
+
|
|
6
|
+
# Load .env file once at module level
|
|
7
|
+
env_path = find_dotenv()
|
|
8
|
+
if env_path:
|
|
9
|
+
load_dotenv(env_path)
|
|
10
|
+
else:
|
|
11
|
+
raise FileNotFoundError(
|
|
12
|
+
"No .env file found. Please create a .env file in the project root with required configuration."
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
def get_config(key: str, required: bool = True) -> str | None:
|
|
16
|
+
"""Get configuration value from environment"""
|
|
17
|
+
value = os.getenv(key)
|
|
18
|
+
if required and not value:
|
|
19
|
+
raise ValueError(f"{key} not found in environment variables. Check .env file.")
|
|
20
|
+
return value
|
|
21
|
+
|
|
22
|
+
def get_telegram_api_key() -> str:
|
|
23
|
+
"""Get Telegram API key from environment"""
|
|
24
|
+
return get_config("TELEGRAM_API_KEY")
|
tiny_tg-0.1.0/uv.lock
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
version = 1
|
|
2
|
+
revision = 3
|
|
3
|
+
requires-python = ">=3.11"
|
|
4
|
+
resolution-markers = [
|
|
5
|
+
"python_full_version >= '3.14'",
|
|
6
|
+
"python_full_version == '3.13.*'",
|
|
7
|
+
"python_full_version < '3.13'",
|
|
8
|
+
]
|
|
9
|
+
|
|
10
|
+
[[package]]
|
|
11
|
+
name = "anyio"
|
|
12
|
+
version = "4.12.1"
|
|
13
|
+
source = { registry = "https://pypi.org/simple" }
|
|
14
|
+
dependencies = [
|
|
15
|
+
{ name = "idna" },
|
|
16
|
+
{ name = "typing-extensions", marker = "python_full_version < '3.13'" },
|
|
17
|
+
]
|
|
18
|
+
sdist = { url = "https://files.pythonhosted.org/packages/96/f0/5eb65b2bb0d09ac6776f2eb54adee6abe8228ea05b20a5ad0e4945de8aac/anyio-4.12.1.tar.gz", hash = "sha256:41cfcc3a4c85d3f05c932da7c26d0201ac36f72abd4435ba90d0464a3ffed703", size = 228685, upload-time = "2026-01-06T11:45:21.246Z" }
|
|
19
|
+
wheels = [
|
|
20
|
+
{ url = "https://files.pythonhosted.org/packages/38/0e/27be9fdef66e72d64c0cdc3cc2823101b80585f8119b5c112c2e8f5f7dab/anyio-4.12.1-py3-none-any.whl", hash = "sha256:d405828884fc140aa80a3c667b8beed277f1dfedec42ba031bd6ac3db606ab6c", size = 113592, upload-time = "2026-01-06T11:45:19.497Z" },
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
[[package]]
|
|
24
|
+
name = "certifi"
|
|
25
|
+
version = "2026.1.4"
|
|
26
|
+
source = { registry = "https://pypi.org/simple" }
|
|
27
|
+
sdist = { url = "https://files.pythonhosted.org/packages/e0/2d/a891ca51311197f6ad14a7ef42e2399f36cf2f9bd44752b3dc4eab60fdc5/certifi-2026.1.4.tar.gz", hash = "sha256:ac726dd470482006e014ad384921ed6438c457018f4b3d204aea4281258b2120", size = 154268, upload-time = "2026-01-04T02:42:41.825Z" }
|
|
28
|
+
wheels = [
|
|
29
|
+
{ url = "https://files.pythonhosted.org/packages/e6/ad/3cc14f097111b4de0040c83a525973216457bbeeb63739ef1ed275c1c021/certifi-2026.1.4-py3-none-any.whl", hash = "sha256:9943707519e4add1115f44c2bc244f782c0249876bf51b6599fee1ffbedd685c", size = 152900, upload-time = "2026-01-04T02:42:40.15Z" },
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[[package]]
|
|
33
|
+
name = "h11"
|
|
34
|
+
version = "0.16.0"
|
|
35
|
+
source = { registry = "https://pypi.org/simple" }
|
|
36
|
+
sdist = { url = "https://files.pythonhosted.org/packages/01/ee/02a2c011bdab74c6fb3c75474d40b3052059d95df7e73351460c8588d963/h11-0.16.0.tar.gz", hash = "sha256:4e35b956cf45792e4caa5885e69fba00bdbc6ffafbfa020300e549b208ee5ff1", size = 101250, upload-time = "2025-04-24T03:35:25.427Z" }
|
|
37
|
+
wheels = [
|
|
38
|
+
{ url = "https://files.pythonhosted.org/packages/04/4b/29cac41a4d98d144bf5f6d33995617b185d14b22401f75ca86f384e87ff1/h11-0.16.0-py3-none-any.whl", hash = "sha256:63cf8bbe7522de3bf65932fda1d9c2772064ffb3dae62d55932da54b31cb6c86", size = 37515, upload-time = "2025-04-24T03:35:24.344Z" },
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[[package]]
|
|
42
|
+
name = "httpcore"
|
|
43
|
+
version = "1.0.9"
|
|
44
|
+
source = { registry = "https://pypi.org/simple" }
|
|
45
|
+
dependencies = [
|
|
46
|
+
{ name = "certifi" },
|
|
47
|
+
{ name = "h11" },
|
|
48
|
+
]
|
|
49
|
+
sdist = { url = "https://files.pythonhosted.org/packages/06/94/82699a10bca87a5556c9c59b5963f2d039dbd239f25bc2a63907a05a14cb/httpcore-1.0.9.tar.gz", hash = "sha256:6e34463af53fd2ab5d807f399a9b45ea31c3dfa2276f15a2c3f00afff6e176e8", size = 85484, upload-time = "2025-04-24T22:06:22.219Z" }
|
|
50
|
+
wheels = [
|
|
51
|
+
{ url = "https://files.pythonhosted.org/packages/7e/f5/f66802a942d491edb555dd61e3a9961140fd64c90bce1eafd741609d334d/httpcore-1.0.9-py3-none-any.whl", hash = "sha256:2d400746a40668fc9dec9810239072b40b4484b640a8c38fd654a024c7a1bf55", size = 78784, upload-time = "2025-04-24T22:06:20.566Z" },
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
[[package]]
|
|
55
|
+
name = "httpx"
|
|
56
|
+
version = "0.28.1"
|
|
57
|
+
source = { registry = "https://pypi.org/simple" }
|
|
58
|
+
dependencies = [
|
|
59
|
+
{ name = "anyio" },
|
|
60
|
+
{ name = "certifi" },
|
|
61
|
+
{ name = "httpcore" },
|
|
62
|
+
{ name = "idna" },
|
|
63
|
+
]
|
|
64
|
+
sdist = { url = "https://files.pythonhosted.org/packages/b1/df/48c586a5fe32a0f01324ee087459e112ebb7224f646c0b5023f5e79e9956/httpx-0.28.1.tar.gz", hash = "sha256:75e98c5f16b0f35b567856f597f06ff2270a374470a5c2392242528e3e3e42fc", size = 141406, upload-time = "2024-12-06T15:37:23.222Z" }
|
|
65
|
+
wheels = [
|
|
66
|
+
{ url = "https://files.pythonhosted.org/packages/2a/39/e50c7c3a983047577ee07d2a9e53faf5a69493943ec3f6a384bdc792deb2/httpx-0.28.1-py3-none-any.whl", hash = "sha256:d909fcccc110f8c7faf814ca82a9a4d816bc5a6dbfea25d6591d6985b8ba59ad", size = 73517, upload-time = "2024-12-06T15:37:21.509Z" },
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
[[package]]
|
|
70
|
+
name = "idna"
|
|
71
|
+
version = "3.11"
|
|
72
|
+
source = { registry = "https://pypi.org/simple" }
|
|
73
|
+
sdist = { url = "https://files.pythonhosted.org/packages/6f/6d/0703ccc57f3a7233505399edb88de3cbd678da106337b9fcde432b65ed60/idna-3.11.tar.gz", hash = "sha256:795dafcc9c04ed0c1fb032c2aa73654d8e8c5023a7df64a53f39190ada629902", size = 194582, upload-time = "2025-10-12T14:55:20.501Z" }
|
|
74
|
+
wheels = [
|
|
75
|
+
{ url = "https://files.pythonhosted.org/packages/0e/61/66938bbb5fc52dbdf84594873d5b51fb1f7c7794e9c0f5bd885f30bc507b/idna-3.11-py3-none-any.whl", hash = "sha256:771a87f49d9defaf64091e6e6fe9c18d4833f140bd19464795bc32d966ca37ea", size = 71008, upload-time = "2025-10-12T14:55:18.883Z" },
|
|
76
|
+
]
|
|
77
|
+
|
|
78
|
+
[[package]]
|
|
79
|
+
name = "python-dotenv"
|
|
80
|
+
version = "1.2.1"
|
|
81
|
+
source = { registry = "https://pypi.org/simple" }
|
|
82
|
+
sdist = { url = "https://files.pythonhosted.org/packages/f0/26/19cadc79a718c5edbec86fd4919a6b6d3f681039a2f6d66d14be94e75fb9/python_dotenv-1.2.1.tar.gz", hash = "sha256:42667e897e16ab0d66954af0e60a9caa94f0fd4ecf3aaf6d2d260eec1aa36ad6", size = 44221, upload-time = "2025-10-26T15:12:10.434Z" }
|
|
83
|
+
wheels = [
|
|
84
|
+
{ url = "https://files.pythonhosted.org/packages/14/1b/a298b06749107c305e1fe0f814c6c74aea7b2f1e10989cb30f544a1b3253/python_dotenv-1.2.1-py3-none-any.whl", hash = "sha256:b81ee9561e9ca4004139c6cbba3a238c32b03e4894671e181b671e8cb8425d61", size = 21230, upload-time = "2025-10-26T15:12:09.109Z" },
|
|
85
|
+
]
|
|
86
|
+
|
|
87
|
+
[[package]]
|
|
88
|
+
name = "tiny-tg"
|
|
89
|
+
version = "0.1.0"
|
|
90
|
+
source = { editable = "." }
|
|
91
|
+
dependencies = [
|
|
92
|
+
{ name = "httpx" },
|
|
93
|
+
{ name = "python-dotenv" },
|
|
94
|
+
]
|
|
95
|
+
|
|
96
|
+
[package.metadata]
|
|
97
|
+
requires-dist = [
|
|
98
|
+
{ name = "httpx" },
|
|
99
|
+
{ name = "python-dotenv" },
|
|
100
|
+
]
|
|
101
|
+
|
|
102
|
+
[[package]]
|
|
103
|
+
name = "typing-extensions"
|
|
104
|
+
version = "4.15.0"
|
|
105
|
+
source = { registry = "https://pypi.org/simple" }
|
|
106
|
+
sdist = { url = "https://files.pythonhosted.org/packages/72/94/1a15dd82efb362ac84269196e94cf00f187f7ed21c242792a923cdb1c61f/typing_extensions-4.15.0.tar.gz", hash = "sha256:0cea48d173cc12fa28ecabc3b837ea3cf6f38c6d1136f85cbaaf598984861466", size = 109391, upload-time = "2025-08-25T13:49:26.313Z" }
|
|
107
|
+
wheels = [
|
|
108
|
+
{ url = "https://files.pythonhosted.org/packages/18/67/36e9267722cc04a6b9f15c7f3441c2363321a3ea07da7ae0c0707beb2a9c/typing_extensions-4.15.0-py3-none-any.whl", hash = "sha256:f0fa19c6845758ab08074a0cfa8b7aecb71c999ca73d62883bc25cc018c4e548", size = 44614, upload-time = "2025-08-25T13:49:24.86Z" },
|
|
109
|
+
]
|