django-telegram-bot-api 0.1.0__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.
- django_telegram/__init__.py +7 -0
- django_telegram/conf.py +33 -0
- django_telegram/dispatcher.py +62 -0
- django_telegram/exceptions.py +20 -0
- django_telegram/handlers.py +10 -0
- django_telegram/py.typed +0 -0
- django_telegram/telegram.py +5195 -0
- django_telegram/telegram_models.py +3433 -0
- django_telegram_bot_api-0.1.0.dist-info/METADATA +351 -0
- django_telegram_bot_api-0.1.0.dist-info/RECORD +11 -0
- django_telegram_bot_api-0.1.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
from .dispatcher import Dispatcher # noqa: F401, I001
|
|
2
|
+
from .handlers import (
|
|
3
|
+
CommandHandler, MessageHandler, CallbackQueryHandler, # noqa: F401
|
|
4
|
+
InlineQueryHandler, ChosenInlineResultHandler, # noqa: F401
|
|
5
|
+
)
|
|
6
|
+
from .telegram import Telegram # noqa: F401
|
|
7
|
+
from .telegram_models import Update # noqa: W292, F401
|
django_telegram/conf.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from django.conf import settings
|
|
2
|
+
from django.core.exceptions import ImproperlyConfigured
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Conf:
|
|
6
|
+
@property
|
|
7
|
+
def _s(self) -> dict:
|
|
8
|
+
return getattr(settings, "TELEGRAM", {})
|
|
9
|
+
|
|
10
|
+
def _require(self, name):
|
|
11
|
+
try:
|
|
12
|
+
return self._s[name]
|
|
13
|
+
except KeyError:
|
|
14
|
+
raise ImproperlyConfigured(
|
|
15
|
+
f"settings.TELEGRAM['{name}'] is required"
|
|
16
|
+
) from None
|
|
17
|
+
|
|
18
|
+
# ---- اجباری (ایندکس) ----
|
|
19
|
+
@property
|
|
20
|
+
def BOT_TOKEN(self) -> str:
|
|
21
|
+
return self._require("BOT_TOKEN")
|
|
22
|
+
|
|
23
|
+
@property
|
|
24
|
+
def TM_WEBHOOK_URL(self) -> str:
|
|
25
|
+
return self._require("TM_WEBHOOK_URL")
|
|
26
|
+
|
|
27
|
+
# ---- اختیاری (get با پیشفرض) ----
|
|
28
|
+
@property
|
|
29
|
+
def PROXY_SOCKS(self) -> str:
|
|
30
|
+
return self._s.get("PROXY_SOCKS", "")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
conf = Conf()
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
from .handlers import (
|
|
2
|
+
CallbackQueryHandler,
|
|
3
|
+
ChosenInlineResultHandler,
|
|
4
|
+
CommandHandler,
|
|
5
|
+
InlineQueryHandler,
|
|
6
|
+
MessageHandler,
|
|
7
|
+
)
|
|
8
|
+
from .telegram_models import Update
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Dispatcher:
|
|
12
|
+
"""
|
|
13
|
+
Dispatcher routes an incoming Telegram update to the user's handler
|
|
14
|
+
subclasses, based on the type of content in the update.
|
|
15
|
+
|
|
16
|
+
Attributes:
|
|
17
|
+
update (Update): The incoming update object from Telegram.
|
|
18
|
+
|
|
19
|
+
Methods:
|
|
20
|
+
dispatch():
|
|
21
|
+
- command (text starts with '/') -> CommandHandler subclasses
|
|
22
|
+
- regular message -> MessageHandler subclasses
|
|
23
|
+
- callback query -> CallBackQueryHandler subclasses
|
|
24
|
+
- inline query -> InlineQueryHandler subclasses
|
|
25
|
+
- chosen inline result -> ChosenInlineResultHandler subclasses
|
|
26
|
+
- anything else -> ignored
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
def __init__(self, update: Update | dict | str | bytes):
|
|
30
|
+
if isinstance(update, Update):
|
|
31
|
+
self.update = update
|
|
32
|
+
elif isinstance(update, (str, bytes)):
|
|
33
|
+
self.update = Update.model_validate_json(update)
|
|
34
|
+
else:
|
|
35
|
+
self.update = Update.model_validate(update)
|
|
36
|
+
|
|
37
|
+
def dispatch(self):
|
|
38
|
+
base = self._pick_base()
|
|
39
|
+
if base is None:
|
|
40
|
+
return
|
|
41
|
+
|
|
42
|
+
for cls in base.__subclasses__():
|
|
43
|
+
cls().handle(self.update)
|
|
44
|
+
|
|
45
|
+
def _pick_base(self):
|
|
46
|
+
message = self.update.message
|
|
47
|
+
|
|
48
|
+
if message:
|
|
49
|
+
if message.text and message.text.startswith("/"):
|
|
50
|
+
return CommandHandler
|
|
51
|
+
return MessageHandler
|
|
52
|
+
|
|
53
|
+
if self.update.callback_query:
|
|
54
|
+
return CallbackQueryHandler
|
|
55
|
+
|
|
56
|
+
if self.update.inline_query:
|
|
57
|
+
return InlineQueryHandler
|
|
58
|
+
|
|
59
|
+
if self.update.chosen_inline_result:
|
|
60
|
+
return ChosenInlineResultHandler
|
|
61
|
+
|
|
62
|
+
return None
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
class TelegramError(Exception):
|
|
2
|
+
"""Base exception for all Telegram API related errors."""
|
|
3
|
+
|
|
4
|
+
pass
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class TelegramAPIError(TelegramError):
|
|
8
|
+
"""Raised when Telegram API returns ok: false."""
|
|
9
|
+
|
|
10
|
+
def __init__(self, method: str, error_code: int | None, description: str):
|
|
11
|
+
self.method = method
|
|
12
|
+
self.error_code = error_code
|
|
13
|
+
self.description = description
|
|
14
|
+
super().__init__(f"[{method}] ({error_code}): {description}")
|
|
15
|
+
|
|
16
|
+
def __repr__(self) -> str:
|
|
17
|
+
return (
|
|
18
|
+
f"TelegramAPIError(method={self.method!r}, "
|
|
19
|
+
f"error_code={self.error_code!r}, description={self.description!r})"
|
|
20
|
+
)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
class BaseHandler:
|
|
2
|
+
def handle(self, update):
|
|
3
|
+
raise NotImplementedError
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class CommandHandler(BaseHandler): ...
|
|
7
|
+
class MessageHandler(BaseHandler): ...
|
|
8
|
+
class CallbackQueryHandler(BaseHandler): ...
|
|
9
|
+
class InlineQueryHandler(BaseHandler): ...
|
|
10
|
+
class ChosenInlineResultHandler(BaseHandler): ...
|
django_telegram/py.typed
ADDED
|
File without changes
|