zalobot-python 0.1.1__tar.gz → 0.1.2__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.
- {zalobot_python-0.1.1 → zalobot_python-0.1.2}/PKG-INFO +1 -1
- {zalobot_python-0.1.1 → zalobot_python-0.1.2}/pyproject.toml +1 -1
- {zalobot_python-0.1.1 → zalobot_python-0.1.2}/src/zalobot_python/__init__.py +3 -2
- {zalobot_python-0.1.1 → zalobot_python-0.1.2}/src/zalobot_python/zalobot.py +20 -4
- {zalobot_python-0.1.1 → zalobot_python-0.1.2}/README.md +0 -0
- {zalobot_python-0.1.1 → zalobot_python-0.1.2}/src/zalobot_python/config.py +0 -0
- {zalobot_python-0.1.1 → zalobot_python-0.1.2}/src/zalobot_python/models.py +0 -0
- {zalobot_python-0.1.1 → zalobot_python-0.1.2}/src/zalobot_python/py.typed +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from .zalobot import ZaloBot
|
|
1
|
+
from .zalobot import ZaloBot, WebhookHandler
|
|
2
2
|
from .models import (
|
|
3
3
|
SuccessfulResponse,
|
|
4
4
|
ErrorResponse,
|
|
@@ -11,7 +11,7 @@ from .models import (
|
|
|
11
11
|
Chat,
|
|
12
12
|
Message,
|
|
13
13
|
Event,
|
|
14
|
-
ZaloAPIError
|
|
14
|
+
ZaloAPIError,
|
|
15
15
|
)
|
|
16
16
|
|
|
17
17
|
__all__ = [
|
|
@@ -19,6 +19,7 @@ __all__ = [
|
|
|
19
19
|
"ErrorResponse",
|
|
20
20
|
"ZaloAPIResponse",
|
|
21
21
|
"ZaloBot",
|
|
22
|
+
"WebhookHandler",
|
|
22
23
|
"BotInfo",
|
|
23
24
|
"WebhookInfo",
|
|
24
25
|
"MessageInfo",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from typing import Any, Literal
|
|
1
|
+
from typing import Any, Literal, Protocol
|
|
2
2
|
import httpx
|
|
3
3
|
|
|
4
4
|
from .models import (
|
|
@@ -34,11 +34,15 @@ async def fetch[T](
|
|
|
34
34
|
|
|
35
35
|
return ErrorResponse(**response_json)
|
|
36
36
|
|
|
37
|
+
class WebhookHandler(Protocol):
|
|
38
|
+
async def __call__(self, update_event: Event, bot: ZaloBot) -> None: ...
|
|
39
|
+
|
|
37
40
|
class ZaloBot:
|
|
38
41
|
def __init__(self, BOT_TOKEN: str):
|
|
39
42
|
self._BOT_TOKEN: str = BOT_TOKEN
|
|
40
43
|
self._base_url: str = f"{ZaloAPIConfig.BASE_URL}/bot{BOT_TOKEN}"
|
|
41
|
-
|
|
44
|
+
self._webhook_handlers: list[WebhookHandler] = []
|
|
45
|
+
|
|
42
46
|
async def getMe(self) -> BotInfo:
|
|
43
47
|
url = f"{self._base_url}/getMe"
|
|
44
48
|
|
|
@@ -53,7 +57,6 @@ class ZaloBot:
|
|
|
53
57
|
|
|
54
58
|
return res.result
|
|
55
59
|
|
|
56
|
-
|
|
57
60
|
async def getUpdates(self, timeout: int = 30) -> Event:
|
|
58
61
|
url = f"{self._base_url}/getUpdates"
|
|
59
62
|
|
|
@@ -71,7 +74,11 @@ class ZaloBot:
|
|
|
71
74
|
|
|
72
75
|
return res.result
|
|
73
76
|
|
|
74
|
-
async def setWebhook(
|
|
77
|
+
async def setWebhook(
|
|
78
|
+
self,
|
|
79
|
+
url: str,
|
|
80
|
+
secret_token: str,
|
|
81
|
+
) -> WebhookInfo:
|
|
75
82
|
url = f"{self._base_url}/setWebhook"
|
|
76
83
|
|
|
77
84
|
payload = {
|
|
@@ -115,6 +122,15 @@ class ZaloBot:
|
|
|
115
122
|
|
|
116
123
|
return res.result
|
|
117
124
|
|
|
125
|
+
def on_webhook_update(self, handler: WebhookHandler) -> None:
|
|
126
|
+
"""Registers a handler to handle on webhook event update"""
|
|
127
|
+
self._webhook_handlers.append(handler)
|
|
128
|
+
|
|
129
|
+
async def dispatch_webhook_handlers(self, update_event: Event) -> None:
|
|
130
|
+
"""Run all handlers given a webhook event"""
|
|
131
|
+
for handler in self._webhook_handlers:
|
|
132
|
+
await handler(update_event, self)
|
|
133
|
+
|
|
118
134
|
async def sendMessage(self, chat_id: str, text: str) -> MessageInfo:
|
|
119
135
|
url = f"{self._base_url}/sendMessage"
|
|
120
136
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|