openbot-api 1.0.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.
- openbot_api-1.0.0/LICENSE +21 -0
- openbot_api-1.0.0/PKG-INFO +52 -0
- openbot_api-1.0.0/README.md +40 -0
- openbot_api-1.0.0/pyproject.toml +18 -0
- openbot_api-1.0.0/setup.cfg +4 -0
- openbot_api-1.0.0/src/openbot_api/__init__.py +5 -0
- openbot_api-1.0.0/src/openbot_api/client.py +169 -0
- openbot_api-1.0.0/src/openbot_api.egg-info/PKG-INFO +52 -0
- openbot_api-1.0.0/src/openbot_api.egg-info/SOURCES.txt +9 -0
- openbot_api-1.0.0/src/openbot_api.egg-info/dependency_links.txt +1 -0
- openbot_api-1.0.0/src/openbot_api.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kafnik
|
|
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,52 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: openbot_api
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Python client for the Openbot ID authorization API
|
|
5
|
+
Author: Kafnik
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: telegram,authorization,fastapi,telebot
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Dynamic: license-file
|
|
12
|
+
|
|
13
|
+
# Openbot API 1.0.0
|
|
14
|
+
|
|
15
|
+
Python library to check Telegram user roles and levels against your Openbot ID server.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install openbot_api
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Until the new package is published to PyPI, install the local wheel included in the release archive.
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
import os
|
|
29
|
+
import telebot
|
|
30
|
+
import openbot_api
|
|
31
|
+
|
|
32
|
+
bot = telebot.TeleBot(os.environ["TELEGRAM_BOT_TOKEN"])
|
|
33
|
+
openbot = openbot_api.OpenbotAPI(api_key=os.environ["OPENBOT_API_KEY"], bot=bot)
|
|
34
|
+
|
|
35
|
+
@bot.message_handler(commands=["start"])
|
|
36
|
+
@openbot.check_level("tester", level=2)
|
|
37
|
+
@openbot.check_level("developer", level=2)
|
|
38
|
+
def start(message):
|
|
39
|
+
bot.reply_to(message, "✅ Доступ разрешён!")
|
|
40
|
+
|
|
41
|
+
bot.infinity_polling()
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Two stacked `check_level` decorators grant access if **either** status and its required level match.
|
|
45
|
+
|
|
46
|
+
Other methods: `get_user(id)`, `get_status(id)`, `get_level(id)`, `has_level(id, role, level)`, `check_access(id)`, and `require_access()`.
|
|
47
|
+
|
|
48
|
+
The API key is a secret. Do not commit it to Git, place it in bot source, or share it publicly. The client is intentionally **read-only**: new keys may only be issued by an authorized administrator on the server, not through an unauthenticated public endpoint.
|
|
49
|
+
|
|
50
|
+
## API keys on Linux server
|
|
51
|
+
|
|
52
|
+
See `server-key-tools/README.md` in the release archive. Back up your existing key database before replacing any existing files. `api_keys.py` and `manage_keys.py` are server-side tools and must **not** be published as part of the Python package.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Openbot API 1.0.0
|
|
2
|
+
|
|
3
|
+
Python library to check Telegram user roles and levels against your Openbot ID server.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install openbot_api
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Until the new package is published to PyPI, install the local wheel included in the release archive.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
import os
|
|
17
|
+
import telebot
|
|
18
|
+
import openbot_api
|
|
19
|
+
|
|
20
|
+
bot = telebot.TeleBot(os.environ["TELEGRAM_BOT_TOKEN"])
|
|
21
|
+
openbot = openbot_api.OpenbotAPI(api_key=os.environ["OPENBOT_API_KEY"], bot=bot)
|
|
22
|
+
|
|
23
|
+
@bot.message_handler(commands=["start"])
|
|
24
|
+
@openbot.check_level("tester", level=2)
|
|
25
|
+
@openbot.check_level("developer", level=2)
|
|
26
|
+
def start(message):
|
|
27
|
+
bot.reply_to(message, "✅ Доступ разрешён!")
|
|
28
|
+
|
|
29
|
+
bot.infinity_polling()
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Two stacked `check_level` decorators grant access if **either** status and its required level match.
|
|
33
|
+
|
|
34
|
+
Other methods: `get_user(id)`, `get_status(id)`, `get_level(id)`, `has_level(id, role, level)`, `check_access(id)`, and `require_access()`.
|
|
35
|
+
|
|
36
|
+
The API key is a secret. Do not commit it to Git, place it in bot source, or share it publicly. The client is intentionally **read-only**: new keys may only be issued by an authorized administrator on the server, not through an unauthenticated public endpoint.
|
|
37
|
+
|
|
38
|
+
## API keys on Linux server
|
|
39
|
+
|
|
40
|
+
See `server-key-tools/README.md` in the release archive. Back up your existing key database before replacing any existing files. `api_keys.py` and `manage_keys.py` are server-side tools and must **not** be published as part of the Python package.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "openbot_api"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Python client for the Openbot ID authorization API"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
dependencies = []
|
|
12
|
+
authors = [{name = "Kafnik"}]
|
|
13
|
+
license = "MIT"
|
|
14
|
+
license-files = ["LICENSE"]
|
|
15
|
+
keywords = ["telegram", "authorization", "fastapi", "telebot"]
|
|
16
|
+
|
|
17
|
+
[tool.setuptools.packages.find]
|
|
18
|
+
where = ["src"]
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"""HTTPS-only client for Openbot ID API."""
|
|
2
|
+
|
|
3
|
+
import functools
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
from urllib.error import HTTPError, URLError
|
|
7
|
+
from urllib.parse import urlencode
|
|
8
|
+
from urllib.request import Request, urlopen
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
DEFAULT_API_URL = "https://server.tail116c85.ts.net"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class APIError(Exception):
|
|
15
|
+
"""The API could not complete the verification."""
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class AccessDenied(Exception):
|
|
19
|
+
"""The checked user does not have permission."""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class OpenbotAPI:
|
|
23
|
+
def __init__(self, api_key=None, *, api_url=DEFAULT_API_URL, timeout=5, bot=None):
|
|
24
|
+
self.api_key = api_key or os.getenv("OPENBOT_API_KEY")
|
|
25
|
+
if not self.api_key:
|
|
26
|
+
raise ValueError("An API key is required")
|
|
27
|
+
self.api_url = api_url.rstrip("/")
|
|
28
|
+
if not self.api_url.startswith("https://"):
|
|
29
|
+
raise ValueError("API URL must use HTTPS")
|
|
30
|
+
if timeout <= 0:
|
|
31
|
+
raise ValueError("timeout must be positive")
|
|
32
|
+
self.timeout = timeout
|
|
33
|
+
self.bot = bot
|
|
34
|
+
|
|
35
|
+
def _get(self, path, params=None):
|
|
36
|
+
url = self.api_url + path
|
|
37
|
+
if params:
|
|
38
|
+
url += "?" + urlencode(params)
|
|
39
|
+
request = Request(url, headers={"X-API-Key": self.api_key, "Accept": "application/json"})
|
|
40
|
+
try:
|
|
41
|
+
with urlopen(request, timeout=self.timeout) as response:
|
|
42
|
+
result = json.load(response)
|
|
43
|
+
except HTTPError as exc:
|
|
44
|
+
raise APIError(f"API returned HTTP {exc.code}") from exc
|
|
45
|
+
except (URLError, TimeoutError, OSError, ValueError) as exc:
|
|
46
|
+
raise APIError("Openbot ID API is unavailable or returned invalid JSON") from exc
|
|
47
|
+
if not isinstance(result, dict):
|
|
48
|
+
raise APIError("Unexpected API response")
|
|
49
|
+
return result
|
|
50
|
+
|
|
51
|
+
@staticmethod
|
|
52
|
+
def _valid_user_id(user_id):
|
|
53
|
+
if type(user_id) is not int or user_id <= 0:
|
|
54
|
+
raise ValueError("user_id must be a positive integer")
|
|
55
|
+
return user_id
|
|
56
|
+
|
|
57
|
+
def get_user(self, user_id):
|
|
58
|
+
return self._get(f"/api/user/{self._valid_user_id(user_id)}")
|
|
59
|
+
|
|
60
|
+
def get_status(self, user_id):
|
|
61
|
+
return self.get_user(user_id)["status"]
|
|
62
|
+
|
|
63
|
+
def get_level(self, user_id):
|
|
64
|
+
return self.get_user(user_id)["level"]
|
|
65
|
+
|
|
66
|
+
def check_access(self, user_id):
|
|
67
|
+
result = self._get(f"/api/check_access/{self._valid_user_id(user_id)}")
|
|
68
|
+
if type(result.get("allowed")) is not bool:
|
|
69
|
+
raise APIError("API response lacks an allowed flag")
|
|
70
|
+
return result["allowed"]
|
|
71
|
+
|
|
72
|
+
def has_level(self, user_id, status, level=1):
|
|
73
|
+
if not isinstance(status, str) or not status.strip():
|
|
74
|
+
raise ValueError("status must be a non-empty string")
|
|
75
|
+
if type(level) is not int or level < 1:
|
|
76
|
+
raise ValueError("level must be a positive integer")
|
|
77
|
+
result = self._get(
|
|
78
|
+
f"/api/check_level/{self._valid_user_id(user_id)}",
|
|
79
|
+
{"status": status, "level": level},
|
|
80
|
+
)
|
|
81
|
+
if type(result.get("allowed")) is not bool:
|
|
82
|
+
raise APIError("API response lacks an allowed flag")
|
|
83
|
+
return result["allowed"]
|
|
84
|
+
|
|
85
|
+
def check_level(self, status, level=1, *, on_denied=None, on_error=None):
|
|
86
|
+
"""Stacked check_level decorators use OR across roles (same client).
|
|
87
|
+
|
|
88
|
+
For example, tester 2+ OR developer 2+ will grant access.
|
|
89
|
+
Failed/unavailable API checks never execute the protected handler.
|
|
90
|
+
"""
|
|
91
|
+
if not isinstance(status, str) or not status.strip():
|
|
92
|
+
raise ValueError("status must be a non-empty string")
|
|
93
|
+
if type(level) is not int or level < 1:
|
|
94
|
+
raise ValueError("level must be a positive integer")
|
|
95
|
+
|
|
96
|
+
def decorate(handler):
|
|
97
|
+
# The inner decorator already created the only guard. Attach another
|
|
98
|
+
# acceptable status instead of wrapping it in a second AND-check.
|
|
99
|
+
if getattr(handler, "_openbot_level_client", None) is self:
|
|
100
|
+
handler._openbot_level_requirements.append((status.strip(), level))
|
|
101
|
+
if on_denied is not None:
|
|
102
|
+
handler._openbot_on_denied = on_denied
|
|
103
|
+
if on_error is not None:
|
|
104
|
+
handler._openbot_on_error = on_error
|
|
105
|
+
return handler
|
|
106
|
+
|
|
107
|
+
@functools.wraps(handler)
|
|
108
|
+
def wrapped(message, *args, **kwargs):
|
|
109
|
+
try:
|
|
110
|
+
user_id = message.from_user.id
|
|
111
|
+
allowed = any(
|
|
112
|
+
self.has_level(user_id, required_status, required_level)
|
|
113
|
+
for required_status, required_level in wrapped._openbot_level_requirements
|
|
114
|
+
)
|
|
115
|
+
except APIError as exc:
|
|
116
|
+
callback = wrapped._openbot_on_error
|
|
117
|
+
if callback:
|
|
118
|
+
return callback(message, exc)
|
|
119
|
+
if self.bot:
|
|
120
|
+
return self.bot.reply_to(message, "⚠️ Openbot ID недоступен. Попробуйте позже.")
|
|
121
|
+
raise
|
|
122
|
+
if not allowed:
|
|
123
|
+
callback = wrapped._openbot_on_denied
|
|
124
|
+
if callback:
|
|
125
|
+
return callback(message)
|
|
126
|
+
if self.bot:
|
|
127
|
+
return self.bot.reply_to(message, "⛔ Недостаточно прав для этой команды.")
|
|
128
|
+
raise AccessDenied("User is not permitted")
|
|
129
|
+
return handler(message, *args, **kwargs)
|
|
130
|
+
|
|
131
|
+
wrapped._openbot_level_client = self
|
|
132
|
+
wrapped._openbot_level_requirements = [(status.strip(), level)]
|
|
133
|
+
wrapped._openbot_on_denied = on_denied
|
|
134
|
+
wrapped._openbot_on_error = on_error
|
|
135
|
+
return wrapped
|
|
136
|
+
|
|
137
|
+
return decorate
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def require_access(self, *, on_denied=None, on_error=None):
|
|
141
|
+
"""Decorator requiring the server's general access policy."""
|
|
142
|
+
return self._decorate_permission(self.check_access, on_denied=on_denied, on_error=on_error)
|
|
143
|
+
|
|
144
|
+
def _decorate_permission(self, verify, *, on_denied, on_error):
|
|
145
|
+
def decorate(handler):
|
|
146
|
+
@functools.wraps(handler)
|
|
147
|
+
def wrapped(message, *args, **kwargs):
|
|
148
|
+
try:
|
|
149
|
+
user_id = message.from_user.id
|
|
150
|
+
allowed = verify(user_id)
|
|
151
|
+
except APIError as exc:
|
|
152
|
+
if on_error:
|
|
153
|
+
return on_error(message, exc)
|
|
154
|
+
if self.bot:
|
|
155
|
+
return self.bot.reply_to(message, "⚠️ Openbot ID недоступен. Попробуйте позже.")
|
|
156
|
+
raise
|
|
157
|
+
if not allowed:
|
|
158
|
+
if on_denied:
|
|
159
|
+
return on_denied(message)
|
|
160
|
+
if self.bot:
|
|
161
|
+
return self.bot.reply_to(message, "⛔ Недостаточно прав для этой команды.")
|
|
162
|
+
raise AccessDenied("User is not permitted")
|
|
163
|
+
return handler(message, *args, **kwargs)
|
|
164
|
+
return wrapped
|
|
165
|
+
return decorate
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
# Optional backwards-compatible class name for migrated projects.
|
|
169
|
+
OpenbotID = OpenbotAPI
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: openbot_api
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Python client for the Openbot ID authorization API
|
|
5
|
+
Author: Kafnik
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: telegram,authorization,fastapi,telebot
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Dynamic: license-file
|
|
12
|
+
|
|
13
|
+
# Openbot API 1.0.0
|
|
14
|
+
|
|
15
|
+
Python library to check Telegram user roles and levels against your Openbot ID server.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install openbot_api
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Until the new package is published to PyPI, install the local wheel included in the release archive.
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
import os
|
|
29
|
+
import telebot
|
|
30
|
+
import openbot_api
|
|
31
|
+
|
|
32
|
+
bot = telebot.TeleBot(os.environ["TELEGRAM_BOT_TOKEN"])
|
|
33
|
+
openbot = openbot_api.OpenbotAPI(api_key=os.environ["OPENBOT_API_KEY"], bot=bot)
|
|
34
|
+
|
|
35
|
+
@bot.message_handler(commands=["start"])
|
|
36
|
+
@openbot.check_level("tester", level=2)
|
|
37
|
+
@openbot.check_level("developer", level=2)
|
|
38
|
+
def start(message):
|
|
39
|
+
bot.reply_to(message, "✅ Доступ разрешён!")
|
|
40
|
+
|
|
41
|
+
bot.infinity_polling()
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Two stacked `check_level` decorators grant access if **either** status and its required level match.
|
|
45
|
+
|
|
46
|
+
Other methods: `get_user(id)`, `get_status(id)`, `get_level(id)`, `has_level(id, role, level)`, `check_access(id)`, and `require_access()`.
|
|
47
|
+
|
|
48
|
+
The API key is a secret. Do not commit it to Git, place it in bot source, or share it publicly. The client is intentionally **read-only**: new keys may only be issued by an authorized administrator on the server, not through an unauthenticated public endpoint.
|
|
49
|
+
|
|
50
|
+
## API keys on Linux server
|
|
51
|
+
|
|
52
|
+
See `server-key-tools/README.md` in the release archive. Back up your existing key database before replacing any existing files. `api_keys.py` and `manage_keys.py` are server-side tools and must **not** be published as part of the Python package.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
openbot_api
|