forked from green-api/whatsapp-api-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhooks.py
More file actions
101 lines (72 loc) · 2.82 KB
/
webhooks.py
File metadata and controls
101 lines (72 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import logging
from typing import Any, Callable, Optional, TYPE_CHECKING
if TYPE_CHECKING:
from ..API import GreenApi
class Webhooks:
_running: Optional[bool] = None
def __init__(self, api: "GreenApi"):
self.api = api
@property
def started(self) -> Optional[bool]:
"""Deprecated"""
self.api.logger.log(logging.WARNING, "This property is deprecated.")
return self._running
@started.setter
def started(self, value: bool) -> None:
"""Deprecated"""
self.api.logger.log(logging.WARNING, "This property is deprecated.")
self._running = value
def startReceivingNotifications(
self, onEvent: Callable[[str, dict], Any]
) -> None:
self._running = True
self._start_polling(onEvent)
def stopReceivingNotifications(self) -> None:
self._running = False
def job(self, onEvent: Callable[[str, dict], Any]) -> None:
"""Deprecated"""
self.api.logger.log(logging.WARNING, "This function is deprecated.")
print((
"Started receiving incoming notifications."
" To stop the process, press Ctrl + C."
))
while self.started:
try:
response = self.api.receiving.receiveNotification()
if response.code == 200:
if not response.data:
continue
response = response.data
body = response["body"]
type_webhook = body["typeWebhook"]
onEvent(type_webhook, body)
self.api.receiving.deleteNotification(
response["receiptId"]
)
except KeyboardInterrupt:
break
print("Stopped receiving incoming notifications.")
def _start_polling(self, handler: Callable[[str, dict], Any]) -> None:
self.api.session.headers["Connection"] = "keep-alive"
self.api.logger.log(
logging.INFO, "Started receiving incoming notifications."
)
while self._running:
try:
response = self.api.receiving.receiveNotification()
if response.code == 200:
if not response.data:
continue
response = response.data
body = response["body"]
type_webhook = body["typeWebhook"]
handler(type_webhook, body)
self.api.receiving.deleteNotification(
response["receiptId"]
)
except KeyboardInterrupt:
break
self.api.session.headers["Connection"] = "close"
self.api.logger.log(
logging.INFO, "Stopped receiving incoming notifications."
)