post-on-telegram 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.
- post_on_telegram-0.1.0/.gitignore +35 -0
- post_on_telegram-0.1.0/CHANGELOG.md +22 -0
- post_on_telegram-0.1.0/LICENSE +21 -0
- post_on_telegram-0.1.0/PKG-INFO +297 -0
- post_on_telegram-0.1.0/README.md +260 -0
- post_on_telegram-0.1.0/pyproject.toml +196 -0
- post_on_telegram-0.1.0/src/post_on_telegram/__init__.py +23 -0
- post_on_telegram-0.1.0/src/post_on_telegram/__main__.py +9 -0
- post_on_telegram-0.1.0/src/post_on_telegram/_version.py +2 -0
- post_on_telegram-0.1.0/src/post_on_telegram/chunking.py +159 -0
- post_on_telegram-0.1.0/src/post_on_telegram/cli.py +162 -0
- post_on_telegram-0.1.0/src/post_on_telegram/credentials.py +221 -0
- post_on_telegram-0.1.0/src/post_on_telegram/errors.py +6 -0
- post_on_telegram-0.1.0/src/post_on_telegram/formatting.py +50 -0
- post_on_telegram-0.1.0/src/post_on_telegram/poster.py +534 -0
- post_on_telegram-0.1.0/src/post_on_telegram/py.typed +1 -0
- post_on_telegram-0.1.0/tests/test_chunking.py +109 -0
- post_on_telegram-0.1.0/tests/test_cli.py +226 -0
- post_on_telegram-0.1.0/tests/test_credentials.py +212 -0
- post_on_telegram-0.1.0/tests/test_formatting.py +18 -0
- post_on_telegram-0.1.0/tests/test_poster.py +923 -0
- post_on_telegram-0.1.0/tests/test_readme_examples.py +125 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
*~
|
|
6
|
+
venv/
|
|
7
|
+
.venv/
|
|
8
|
+
.env
|
|
9
|
+
|
|
10
|
+
# Distribution / packaging
|
|
11
|
+
dist/
|
|
12
|
+
build/
|
|
13
|
+
*.egg-info/
|
|
14
|
+
|
|
15
|
+
# Coverage / Testing
|
|
16
|
+
.coverage
|
|
17
|
+
htmlcov/
|
|
18
|
+
.pytest_cache/
|
|
19
|
+
.mypy_cache/
|
|
20
|
+
.ruff_cache/
|
|
21
|
+
|
|
22
|
+
# IDEs
|
|
23
|
+
.idea/
|
|
24
|
+
.vscode/
|
|
25
|
+
|
|
26
|
+
# Secrets & logs
|
|
27
|
+
credentials*.json
|
|
28
|
+
!examples/credentials.example.json
|
|
29
|
+
tokens
|
|
30
|
+
*.log
|
|
31
|
+
|
|
32
|
+
# Temporary build specification and progress logs
|
|
33
|
+
.progress.md
|
|
34
|
+
AUDIT.md
|
|
35
|
+
SPEC.md
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [0.1.0] - 2026-12-04
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Initial release of Post on Telegram.
|
|
13
|
+
- `Poster` class with `send`, `send_document`, and `check` methods.
|
|
14
|
+
- Credential resolution from explicit args, credentials file, and environment variables.
|
|
15
|
+
- Support for plain text, HTML, and MarkdownV2 parse modes.
|
|
16
|
+
- Automatic long-message chunking (default max 4000 chars).
|
|
17
|
+
- Retry logic with exponential backoff and jitter.
|
|
18
|
+
- Thread-safe sending via internal lock.
|
|
19
|
+
- CLI (`post-on-telegram`) with `--check`, `--stdin`, `--file`, etc.
|
|
20
|
+
- Formatting helpers: `escape_html`, `escape_markdown_v2`, `format_pre`.
|
|
21
|
+
- Strict and non-strict configuration modes.
|
|
22
|
+
- Secure credential handling with token redaction in logs and error messages.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sourav B. Roy
|
|
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,297 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: post-on-telegram
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Send messages and documents to Telegram chats through a bot. Designed for long-running programs: never crashes the caller, never leaks the bot token.
|
|
5
|
+
Project-URL: Homepage, https://github.com/s13rw81/post-on-telegram
|
|
6
|
+
Project-URL: Issues, https://github.com/s13rw81/post-on-telegram/issues
|
|
7
|
+
Project-URL: Changelog, https://github.com/s13rw81/post-on-telegram/blob/main/CHANGELOG.md
|
|
8
|
+
Project-URL: Source, https://github.com/s13rw81/post-on-telegram
|
|
9
|
+
Author: Sourav B. Roy
|
|
10
|
+
License: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: alerts,bot,messaging,notifications,telegram,trading-bot
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Topic :: Communications :: Chat
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Classifier: Typing :: Typed
|
|
25
|
+
Requires-Python: >=3.10
|
|
26
|
+
Requires-Dist: requests<3,>=2.31
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: build>=1.2; extra == 'dev'
|
|
29
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
30
|
+
Requires-Dist: pytest-cov>=4.1; extra == 'dev'
|
|
31
|
+
Requires-Dist: pytest>=7.4; extra == 'dev'
|
|
32
|
+
Requires-Dist: responses>=0.25; extra == 'dev'
|
|
33
|
+
Requires-Dist: ruff==0.17.6; extra == 'dev'
|
|
34
|
+
Requires-Dist: twine>=5.0; extra == 'dev'
|
|
35
|
+
Requires-Dist: types-requests>=2.31; extra == 'dev'
|
|
36
|
+
Description-Content-Type: text/markdown
|
|
37
|
+
|
|
38
|
+
# Post on Telegram
|
|
39
|
+
|
|
40
|
+
[](https://github.com/s13rw81/post-on-telegram/actions/workflows/ci.yml)
|
|
41
|
+
[](https://www.python.org/downloads/)
|
|
42
|
+
[](https://opensource.org/licenses/MIT)
|
|
43
|
+
[](https://peps.python.org/pep-0487/)
|
|
44
|
+
|
|
45
|
+
**Post on Telegram** is a Python library that sends text messages (and documents) to a Telegram chat through a bot. It is designed to be called from long-running programs such as trading bots: it never crashes the caller and never leaks the bot token.
|
|
46
|
+
|
|
47
|
+
> **Disclaimer:** This is an unofficial project and is not affiliated with Telegram.
|
|
48
|
+
|
|
49
|
+
## Table of Contents
|
|
50
|
+
|
|
51
|
+
- [What is this?](#what-is-this)
|
|
52
|
+
- [Features](#features)
|
|
53
|
+
- [Installation](#installation)
|
|
54
|
+
- [Two-minute setup](#two-minute-setup)
|
|
55
|
+
- [Quick start](#quick-start)
|
|
56
|
+
- [Formatting](#formatting)
|
|
57
|
+
- [Long messages and rate limits](#long-messages-and-rate-limits)
|
|
58
|
+
- [Sending files](#sending-files)
|
|
59
|
+
- [CLI usage](#cli-usage)
|
|
60
|
+
- [Logging](#logging)
|
|
61
|
+
- [Using it inside long-running apps](#using-it-inside-long-running-apps)
|
|
62
|
+
- [Troubleshooting / FAQ](#troubleshooting--faq)
|
|
63
|
+
- [Security notes](#security-notes)
|
|
64
|
+
- [API reference](#api-reference)
|
|
65
|
+
- [Development](#development)
|
|
66
|
+
- [Contributing](#contributing)
|
|
67
|
+
- [Security policy](#security-policy)
|
|
68
|
+
- [License](#license)
|
|
69
|
+
|
|
70
|
+
## What is this?
|
|
71
|
+
|
|
72
|
+
Post on Telegram sends messages to Telegram chats via a bot. It is built for
|
|
73
|
+
people who run bots or notification services from Python — for example, a
|
|
74
|
+
trading bot that sends alerts, a scraper that posts updates, or a server that
|
|
75
|
+
sends system notifications.
|
|
76
|
+
|
|
77
|
+
It is designed to be safe to call from long-running programs:
|
|
78
|
+
|
|
79
|
+
- It **never raises** (unless `strict=True` is used for configuration errors).
|
|
80
|
+
- It **never crashes** the caller – all errors are caught, logged, and stored in
|
|
81
|
+
`last_error`.
|
|
82
|
+
- It **never leaks** the bot token – all logs, error messages, and representations
|
|
83
|
+
are redacted.
|
|
84
|
+
|
|
85
|
+
## Features
|
|
86
|
+
|
|
87
|
+
- ✅ Send text messages to any Telegram chat (private, group, channel).
|
|
88
|
+
- ✅ Send documents/files (up to 50 MB) with captions.
|
|
89
|
+
- ✅ Automatic long-message chunking (default max 4000 chars, safely below
|
|
90
|
+
Telegram's 4096 limit).
|
|
91
|
+
- ✅ Multiple parse modes: plain text, HTML, MarkdownV2 (with automatic fallback
|
|
92
|
+
to plain text on parse errors).
|
|
93
|
+
- ✅ Retry logic with exponential backoff and jitter; honours `retry_after` on
|
|
94
|
+
rate limits; enforces an overall timeout.
|
|
95
|
+
- ✅ Thread-safe sending (lock prevents interleaved chunks).
|
|
96
|
+
- ✅ CLI (`post-on-telegram`) with `--check`, `--stdin`, `--file`, etc.
|
|
97
|
+
- ✅ Strict and non-strict configuration modes.
|
|
98
|
+
- ✅ Secure credential handling (file, env vars, explicit args).
|
|
99
|
+
|
|
100
|
+
## Installation
|
|
101
|
+
|
|
102
|
+
From PyPI (once published):
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
pip install post-on-telegram
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
From a Git tag:
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
pip install "git+https://github.com/s13rw81/post-on-telegram.git@v0.1.0"
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
From a wheel/sdist file:
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
pip install dist/post_on_telegram-0.1.0-py3-none-any.whl
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
For development:
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
git clone https://github.com/s13rw81/post-on-telegram.git
|
|
124
|
+
cd post-on-telegram
|
|
125
|
+
python -m venv .venv
|
|
126
|
+
source .venv/bin/activate # or .venv\Scripts\activate on Windows
|
|
127
|
+
pip install -e ".[dev]"
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Two-minute setup
|
|
131
|
+
|
|
132
|
+
1. Create a bot with [@BotFather](https://t.me/BotFather) and copy the token.
|
|
133
|
+
2. Start a chat with the bot (or add it to a group; for channels add it as an
|
|
134
|
+
administrator).
|
|
135
|
+
3. Find the chat ID (use the `getUpdates` method via @BotFather or a small script).
|
|
136
|
+
4. Create a credentials JSON file (e.g. `~/.post-on-telegram-credentials.json`):
|
|
137
|
+
|
|
138
|
+
```json
|
|
139
|
+
{
|
|
140
|
+
"bot_token": "123456789:AAFakeTokenForTestsOnly_abcdefghijklmnopq",
|
|
141
|
+
"chat_id": "-1001234567890"
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
5. Lock down file permissions (chmod 600 on Linux/macOS, icacls on Windows).
|
|
146
|
+
6. Verify with:
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
post-on-telegram --check
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Quick start
|
|
153
|
+
|
|
154
|
+
Basic send:
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
from post_on_telegram import Poster
|
|
158
|
+
|
|
159
|
+
poster = Poster()
|
|
160
|
+
poster.send("Hello, world!")
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Using env vars:
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
import os
|
|
167
|
+
|
|
168
|
+
os.environ["POST_ON_TELEGRAM_TOKEN"] = "123456789:AAFakeTokenForTestsOnly_abcdefghijklmnopq"
|
|
169
|
+
os.environ["POST_ON_TELEGRAM_CHAT_ID"] = "-1001234567890"
|
|
170
|
+
|
|
171
|
+
poster = Poster()
|
|
172
|
+
poster.send("Hello from env vars!")
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Explicit token/chat_id:
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
poster = Poster(
|
|
179
|
+
token="123456789:AAFakeTokenForTestsOnly_abcdefghijklmnopq",
|
|
180
|
+
chat_id="-1001234567890",
|
|
181
|
+
)
|
|
182
|
+
poster.send("Hello with explicit credentials!")
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
Check boolean result and last error:
|
|
186
|
+
|
|
187
|
+
```python
|
|
188
|
+
if poster.send("Hello!"):
|
|
189
|
+
print("Sent!")
|
|
190
|
+
else:
|
|
191
|
+
print(f"Failed: {poster.last_error}")
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## Formatting
|
|
195
|
+
|
|
196
|
+
- Plain text (default): `poster.send("Hello!")`
|
|
197
|
+
- HTML: `poster.send("Hello <b>world</b>!", parse_mode="HTML")`
|
|
198
|
+
- MarkdownV2: `poster.send("**Hello** world!", parse_mode="MarkdownV2")`
|
|
199
|
+
|
|
200
|
+
Use `format_pre` for monospace blocks:
|
|
201
|
+
|
|
202
|
+
```python
|
|
203
|
+
from post_on_telegram import format_pre, escape_html
|
|
204
|
+
|
|
205
|
+
poster.send(format_pre("a | b\n--- | ---\n1 | 2"), parse_mode="HTML")
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
Automatic fallback: if Telegram returns a 400 parse error, the library retries
|
|
209
|
+
the chunk as plain text (HTML tags stripped for HTML mode).
|
|
210
|
+
|
|
211
|
+
## Long messages and rate limits
|
|
212
|
+
|
|
213
|
+
Messages longer than `max_message_length` (default 4000) are split into chunks.
|
|
214
|
+
Retries use exponential backoff with jitter; honour `retry_after` on 429. If the
|
|
215
|
+
required wait exceeds the remaining `total_timeout` budget, the request gives up
|
|
216
|
+
immediately without sleeping. Invalid `retry_after` values (NaN, negative,
|
|
217
|
+
non-numeric, or HTTP-date headers) are ignored and fall back to exponential
|
|
218
|
+
backoff.
|
|
219
|
+
|
|
220
|
+
## Sending files
|
|
221
|
+
|
|
222
|
+
```python
|
|
223
|
+
poster.send_document("/path/to/file.pdf", caption="Report")
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## CLI usage
|
|
227
|
+
|
|
228
|
+
```bash
|
|
229
|
+
post-on-telegram "Hello from CLI!"
|
|
230
|
+
cat report.txt | post-on-telegram -
|
|
231
|
+
post-on-telegram --check
|
|
232
|
+
post-on-telegram --file report.pdf --caption "Report"
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
## Logging
|
|
236
|
+
|
|
237
|
+
```python
|
|
238
|
+
import logging
|
|
239
|
+
|
|
240
|
+
logging.basicConfig(level=logging.INFO)
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## Using it inside long-running apps
|
|
244
|
+
|
|
245
|
+
```python
|
|
246
|
+
from post_on_telegram import Poster
|
|
247
|
+
|
|
248
|
+
poster = Poster()
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def send_alert(message: str) -> None:
|
|
252
|
+
if not poster.send(message):
|
|
253
|
+
# log poster.last_error
|
|
254
|
+
pass
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
## Troubleshooting / FAQ
|
|
258
|
+
|
|
259
|
+
- 401 Unauthorized: token invalid or revoked.
|
|
260
|
+
- 400 chat not found: chat_id wrong or bot not in chat.
|
|
261
|
+
- 403 bot was blocked/kicked: unblock the bot.
|
|
262
|
+
- "can't parse entities": automatic fallback to plain text handles this.
|
|
263
|
+
|
|
264
|
+
## Security notes
|
|
265
|
+
|
|
266
|
+
- Token is never logged or exposed in error messages.
|
|
267
|
+
- Credentials file should have chmod 600.
|
|
268
|
+
|
|
269
|
+
## API reference
|
|
270
|
+
|
|
271
|
+
See the docstrings in `post_on_telegram`.
|
|
272
|
+
|
|
273
|
+
## Development
|
|
274
|
+
|
|
275
|
+
```bash
|
|
276
|
+
python -m venv .venv
|
|
277
|
+
source .venv/bin/activate
|
|
278
|
+
pip install -e ".[dev]"
|
|
279
|
+
python -m ruff check .
|
|
280
|
+
python -m ruff format --check .
|
|
281
|
+
python -m mypy src
|
|
282
|
+
python -m pytest --cov=post_on_telegram
|
|
283
|
+
python -m build
|
|
284
|
+
python -m twine check dist/*
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
## Contributing
|
|
288
|
+
|
|
289
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
290
|
+
|
|
291
|
+
## Security policy
|
|
292
|
+
|
|
293
|
+
See [SECURITY.md](SECURITY.md).
|
|
294
|
+
|
|
295
|
+
## License
|
|
296
|
+
|
|
297
|
+
MIT – see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# Post on Telegram
|
|
2
|
+
|
|
3
|
+
[](https://github.com/s13rw81/post-on-telegram/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.python.org/downloads/)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
[](https://peps.python.org/pep-0487/)
|
|
7
|
+
|
|
8
|
+
**Post on Telegram** is a Python library that sends text messages (and documents) to a Telegram chat through a bot. It is designed to be called from long-running programs such as trading bots: it never crashes the caller and never leaks the bot token.
|
|
9
|
+
|
|
10
|
+
> **Disclaimer:** This is an unofficial project and is not affiliated with Telegram.
|
|
11
|
+
|
|
12
|
+
## Table of Contents
|
|
13
|
+
|
|
14
|
+
- [What is this?](#what-is-this)
|
|
15
|
+
- [Features](#features)
|
|
16
|
+
- [Installation](#installation)
|
|
17
|
+
- [Two-minute setup](#two-minute-setup)
|
|
18
|
+
- [Quick start](#quick-start)
|
|
19
|
+
- [Formatting](#formatting)
|
|
20
|
+
- [Long messages and rate limits](#long-messages-and-rate-limits)
|
|
21
|
+
- [Sending files](#sending-files)
|
|
22
|
+
- [CLI usage](#cli-usage)
|
|
23
|
+
- [Logging](#logging)
|
|
24
|
+
- [Using it inside long-running apps](#using-it-inside-long-running-apps)
|
|
25
|
+
- [Troubleshooting / FAQ](#troubleshooting--faq)
|
|
26
|
+
- [Security notes](#security-notes)
|
|
27
|
+
- [API reference](#api-reference)
|
|
28
|
+
- [Development](#development)
|
|
29
|
+
- [Contributing](#contributing)
|
|
30
|
+
- [Security policy](#security-policy)
|
|
31
|
+
- [License](#license)
|
|
32
|
+
|
|
33
|
+
## What is this?
|
|
34
|
+
|
|
35
|
+
Post on Telegram sends messages to Telegram chats via a bot. It is built for
|
|
36
|
+
people who run bots or notification services from Python — for example, a
|
|
37
|
+
trading bot that sends alerts, a scraper that posts updates, or a server that
|
|
38
|
+
sends system notifications.
|
|
39
|
+
|
|
40
|
+
It is designed to be safe to call from long-running programs:
|
|
41
|
+
|
|
42
|
+
- It **never raises** (unless `strict=True` is used for configuration errors).
|
|
43
|
+
- It **never crashes** the caller – all errors are caught, logged, and stored in
|
|
44
|
+
`last_error`.
|
|
45
|
+
- It **never leaks** the bot token – all logs, error messages, and representations
|
|
46
|
+
are redacted.
|
|
47
|
+
|
|
48
|
+
## Features
|
|
49
|
+
|
|
50
|
+
- ✅ Send text messages to any Telegram chat (private, group, channel).
|
|
51
|
+
- ✅ Send documents/files (up to 50 MB) with captions.
|
|
52
|
+
- ✅ Automatic long-message chunking (default max 4000 chars, safely below
|
|
53
|
+
Telegram's 4096 limit).
|
|
54
|
+
- ✅ Multiple parse modes: plain text, HTML, MarkdownV2 (with automatic fallback
|
|
55
|
+
to plain text on parse errors).
|
|
56
|
+
- ✅ Retry logic with exponential backoff and jitter; honours `retry_after` on
|
|
57
|
+
rate limits; enforces an overall timeout.
|
|
58
|
+
- ✅ Thread-safe sending (lock prevents interleaved chunks).
|
|
59
|
+
- ✅ CLI (`post-on-telegram`) with `--check`, `--stdin`, `--file`, etc.
|
|
60
|
+
- ✅ Strict and non-strict configuration modes.
|
|
61
|
+
- ✅ Secure credential handling (file, env vars, explicit args).
|
|
62
|
+
|
|
63
|
+
## Installation
|
|
64
|
+
|
|
65
|
+
From PyPI (once published):
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
pip install post-on-telegram
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
From a Git tag:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
pip install "git+https://github.com/s13rw81/post-on-telegram.git@v0.1.0"
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
From a wheel/sdist file:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
pip install dist/post_on_telegram-0.1.0-py3-none-any.whl
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
For development:
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
git clone https://github.com/s13rw81/post-on-telegram.git
|
|
87
|
+
cd post-on-telegram
|
|
88
|
+
python -m venv .venv
|
|
89
|
+
source .venv/bin/activate # or .venv\Scripts\activate on Windows
|
|
90
|
+
pip install -e ".[dev]"
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Two-minute setup
|
|
94
|
+
|
|
95
|
+
1. Create a bot with [@BotFather](https://t.me/BotFather) and copy the token.
|
|
96
|
+
2. Start a chat with the bot (or add it to a group; for channels add it as an
|
|
97
|
+
administrator).
|
|
98
|
+
3. Find the chat ID (use the `getUpdates` method via @BotFather or a small script).
|
|
99
|
+
4. Create a credentials JSON file (e.g. `~/.post-on-telegram-credentials.json`):
|
|
100
|
+
|
|
101
|
+
```json
|
|
102
|
+
{
|
|
103
|
+
"bot_token": "123456789:AAFakeTokenForTestsOnly_abcdefghijklmnopq",
|
|
104
|
+
"chat_id": "-1001234567890"
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
5. Lock down file permissions (chmod 600 on Linux/macOS, icacls on Windows).
|
|
109
|
+
6. Verify with:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
post-on-telegram --check
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Quick start
|
|
116
|
+
|
|
117
|
+
Basic send:
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
from post_on_telegram import Poster
|
|
121
|
+
|
|
122
|
+
poster = Poster()
|
|
123
|
+
poster.send("Hello, world!")
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Using env vars:
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
import os
|
|
130
|
+
|
|
131
|
+
os.environ["POST_ON_TELEGRAM_TOKEN"] = "123456789:AAFakeTokenForTestsOnly_abcdefghijklmnopq"
|
|
132
|
+
os.environ["POST_ON_TELEGRAM_CHAT_ID"] = "-1001234567890"
|
|
133
|
+
|
|
134
|
+
poster = Poster()
|
|
135
|
+
poster.send("Hello from env vars!")
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Explicit token/chat_id:
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
poster = Poster(
|
|
142
|
+
token="123456789:AAFakeTokenForTestsOnly_abcdefghijklmnopq",
|
|
143
|
+
chat_id="-1001234567890",
|
|
144
|
+
)
|
|
145
|
+
poster.send("Hello with explicit credentials!")
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Check boolean result and last error:
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
if poster.send("Hello!"):
|
|
152
|
+
print("Sent!")
|
|
153
|
+
else:
|
|
154
|
+
print(f"Failed: {poster.last_error}")
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Formatting
|
|
158
|
+
|
|
159
|
+
- Plain text (default): `poster.send("Hello!")`
|
|
160
|
+
- HTML: `poster.send("Hello <b>world</b>!", parse_mode="HTML")`
|
|
161
|
+
- MarkdownV2: `poster.send("**Hello** world!", parse_mode="MarkdownV2")`
|
|
162
|
+
|
|
163
|
+
Use `format_pre` for monospace blocks:
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
from post_on_telegram import format_pre, escape_html
|
|
167
|
+
|
|
168
|
+
poster.send(format_pre("a | b\n--- | ---\n1 | 2"), parse_mode="HTML")
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Automatic fallback: if Telegram returns a 400 parse error, the library retries
|
|
172
|
+
the chunk as plain text (HTML tags stripped for HTML mode).
|
|
173
|
+
|
|
174
|
+
## Long messages and rate limits
|
|
175
|
+
|
|
176
|
+
Messages longer than `max_message_length` (default 4000) are split into chunks.
|
|
177
|
+
Retries use exponential backoff with jitter; honour `retry_after` on 429. If the
|
|
178
|
+
required wait exceeds the remaining `total_timeout` budget, the request gives up
|
|
179
|
+
immediately without sleeping. Invalid `retry_after` values (NaN, negative,
|
|
180
|
+
non-numeric, or HTTP-date headers) are ignored and fall back to exponential
|
|
181
|
+
backoff.
|
|
182
|
+
|
|
183
|
+
## Sending files
|
|
184
|
+
|
|
185
|
+
```python
|
|
186
|
+
poster.send_document("/path/to/file.pdf", caption="Report")
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## CLI usage
|
|
190
|
+
|
|
191
|
+
```bash
|
|
192
|
+
post-on-telegram "Hello from CLI!"
|
|
193
|
+
cat report.txt | post-on-telegram -
|
|
194
|
+
post-on-telegram --check
|
|
195
|
+
post-on-telegram --file report.pdf --caption "Report"
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## Logging
|
|
199
|
+
|
|
200
|
+
```python
|
|
201
|
+
import logging
|
|
202
|
+
|
|
203
|
+
logging.basicConfig(level=logging.INFO)
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## Using it inside long-running apps
|
|
207
|
+
|
|
208
|
+
```python
|
|
209
|
+
from post_on_telegram import Poster
|
|
210
|
+
|
|
211
|
+
poster = Poster()
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def send_alert(message: str) -> None:
|
|
215
|
+
if not poster.send(message):
|
|
216
|
+
# log poster.last_error
|
|
217
|
+
pass
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## Troubleshooting / FAQ
|
|
221
|
+
|
|
222
|
+
- 401 Unauthorized: token invalid or revoked.
|
|
223
|
+
- 400 chat not found: chat_id wrong or bot not in chat.
|
|
224
|
+
- 403 bot was blocked/kicked: unblock the bot.
|
|
225
|
+
- "can't parse entities": automatic fallback to plain text handles this.
|
|
226
|
+
|
|
227
|
+
## Security notes
|
|
228
|
+
|
|
229
|
+
- Token is never logged or exposed in error messages.
|
|
230
|
+
- Credentials file should have chmod 600.
|
|
231
|
+
|
|
232
|
+
## API reference
|
|
233
|
+
|
|
234
|
+
See the docstrings in `post_on_telegram`.
|
|
235
|
+
|
|
236
|
+
## Development
|
|
237
|
+
|
|
238
|
+
```bash
|
|
239
|
+
python -m venv .venv
|
|
240
|
+
source .venv/bin/activate
|
|
241
|
+
pip install -e ".[dev]"
|
|
242
|
+
python -m ruff check .
|
|
243
|
+
python -m ruff format --check .
|
|
244
|
+
python -m mypy src
|
|
245
|
+
python -m pytest --cov=post_on_telegram
|
|
246
|
+
python -m build
|
|
247
|
+
python -m twine check dist/*
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
## Contributing
|
|
251
|
+
|
|
252
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
253
|
+
|
|
254
|
+
## Security policy
|
|
255
|
+
|
|
256
|
+
See [SECURITY.md](SECURITY.md).
|
|
257
|
+
|
|
258
|
+
## License
|
|
259
|
+
|
|
260
|
+
MIT – see [LICENSE](LICENSE).
|