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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: zalobot-python
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: Python SDK for the Zalo Bot API
5
5
  Author: NovaH00
6
6
  Author-email: NovaH00 <trantay2006super@gmail.com>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "zalobot-python"
3
- version = "0.1.1"
3
+ version = "0.1.2"
4
4
  description = "Python SDK for the Zalo Bot API"
5
5
  readme = "README.md"
6
6
  authors = [
@@ -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(self, url: str, secret_token: str) -> WebhookInfo:
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