Skip to content

Commit 5a72ad8

Browse files
authored
Merge pull request #31 from green-api/dev
Added logger
2 parents c99bfc0 + 74e99f4 commit 5a72ad8

File tree

9 files changed

+550
-442
lines changed

9 files changed

+550
-442
lines changed

.github/workflows/python-publish.yml

Lines changed: 0 additions & 39 deletions
This file was deleted.

.vscode/launch.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

.vscode/settings.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

LICENSE

Lines changed: 357 additions & 279 deletions
Large diffs are not rendered by default.

examples/createGroupAndSendMessage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
def main():
99
create_group_response = greenAPI.groups.createGroup(
10-
"Group Name", ["11001234567@c.us"]
10+
"Group Name", ["11001234567@c.us", "11001234567@c.us"]
1111
)
1212
if create_group_response.code == 200:
1313
print(create_group_response.data)

examples/receiveNotification.py

Lines changed: 107 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import json
21
from datetime import datetime
2+
from json import dumps
33

44
from whatsapp_api_client_python import API
55

@@ -9,89 +9,109 @@
99

1010

1111
def main():
12-
greenAPI.webhooks.startReceivingNotifications(onEvent)
13-
14-
def onEvent(typeWebhook, body):
15-
if typeWebhook == 'incomingMessageReceived':
16-
onIncomingMessageReceived(body)
17-
elif typeWebhook == 'deviceInfo':
18-
onDeviceInfo(body)
19-
elif typeWebhook == 'incomingCall':
20-
onIncomingCall(body)
21-
elif typeWebhook == 'outgoingAPIMessageReceived':
22-
onOutgoingAPIMessageReceived(body)
23-
elif typeWebhook == 'outgoingMessageReceived':
24-
onOutgoingMessageReceived(body)
25-
elif typeWebhook == 'outgoingMessageStatus':
26-
onOutgoingMessageStatus(body)
27-
elif typeWebhook == 'stateInstanceChanged':
28-
onStateInstanceChanged(body)
29-
elif typeWebhook == 'statusInstanceChanged':
30-
onStatusInstanceChanged(body)
31-
32-
def onIncomingMessageReceived(body):
33-
idMessage = body['idMessage']
34-
eventDate = datetime.fromtimestamp(body['timestamp'])
35-
senderData = body['senderData']
36-
messageData = body['messageData']
37-
print(idMessage + ': '
38-
+ 'At ' + str(eventDate) + ' Incoming from ' \
39-
+ json.dumps(senderData, ensure_ascii=False) \
40-
+ ' message = ' + json.dumps(messageData, ensure_ascii=False))
41-
42-
def onIncomingCall(body):
43-
idMessage = body['idMessage']
44-
eventDate = datetime.fromtimestamp(body['timestamp'])
45-
fromWho = body['from']
46-
print(idMessage + ': '
47-
+ 'Call from ' + fromWho
48-
+ ' at ' + str(eventDate))
49-
50-
def onDeviceInfo(body):
51-
eventDate = datetime.fromtimestamp(body['timestamp'])
52-
deviceData = body['deviceData']
53-
print('At ' + str(eventDate) + ': ' \
54-
+ json.dumps(deviceData, ensure_ascii=False))
55-
56-
def onOutgoingMessageReceived(body):
57-
idMessage = body['idMessage']
58-
eventDate = datetime.fromtimestamp(body['timestamp'])
59-
senderData = body['senderData']
60-
messageData = body['messageData']
61-
print(idMessage + ': '
62-
+ 'At ' + str(eventDate) + ' Outgoing from ' \
63-
+ json.dumps(senderData, ensure_ascii=False) \
64-
+ ' message = ' + json.dumps(messageData, ensure_ascii=False))
65-
66-
def onOutgoingAPIMessageReceived(body):
67-
idMessage = body['idMessage']
68-
eventDate = datetime.fromtimestamp(body['timestamp'])
69-
senderData = body['senderData']
70-
messageData = body['messageData']
71-
print(idMessage + ': '
72-
+ 'At ' + str(eventDate) + ' API outgoing from ' \
73-
+ json.dumps(senderData, ensure_ascii=False) + \
74-
' message = ' + json.dumps(messageData, ensure_ascii=False))
75-
76-
def onOutgoingMessageStatus(body):
77-
idMessage = body['idMessage']
78-
status = body['status']
79-
eventDate = datetime.fromtimestamp(body['timestamp'])
80-
print(idMessage + ': '
81-
+ 'At ' + str(eventDate) + ' status = ' + status)
82-
83-
def onStateInstanceChanged(body):
84-
eventDate = datetime.fromtimestamp(body['timestamp'])
85-
stateInstance = body['stateInstance']
86-
print('At ' + str(eventDate) + ' state instance = ' \
87-
+ json.dumps(stateInstance, ensure_ascii=False))
88-
89-
def onStatusInstanceChanged(body):
90-
eventDate = datetime.fromtimestamp(body['timestamp'])
91-
statusInstance = body['statusInstance']
92-
print('At ' + str(eventDate) + ' status instance = ' \
93-
+ json.dumps(statusInstance, ensure_ascii=False))
94-
95-
96-
if __name__ == "__main__":
97-
main()
12+
greenAPI.webhooks.startReceivingNotifications(handler)
13+
14+
15+
def handler(type_webhook: str, body: dict) -> None:
16+
if type_webhook == "incomingMessageReceived":
17+
incoming_message_received(body)
18+
elif type_webhook == "outgoingMessageReceived":
19+
outgoing_message_received(body)
20+
elif type_webhook == "outgoingAPIMessageReceived":
21+
outgoing_api_message_received(body)
22+
elif type_webhook == "outgoingMessageStatus":
23+
outgoing_message_status(body)
24+
elif type_webhook == "stateInstanceChanged":
25+
state_instance_changed(body)
26+
elif type_webhook == "deviceInfo":
27+
device_info(body)
28+
elif type_webhook == "incomingCall":
29+
incoming_call(body)
30+
elif type_webhook == "statusInstanceChanged":
31+
status_instance_changed(body)
32+
33+
34+
def get_notification_time(timestamp: int) -> str:
35+
return str(datetime.fromtimestamp(timestamp))
36+
37+
38+
def incoming_message_received(body: dict) -> None:
39+
timestamp = body["timestamp"]
40+
time = get_notification_time(timestamp)
41+
42+
data = dumps(body, ensure_ascii=False, indent=4)
43+
44+
print(f"New incoming message at {time} with data: {data}", end="\n\n")
45+
46+
47+
def outgoing_message_received(body: dict) -> None:
48+
timestamp = body["timestamp"]
49+
time = get_notification_time(timestamp)
50+
51+
data = dumps(body, ensure_ascii=False, indent=4)
52+
53+
print(f"New outgoing message at {time} with data: {data}", end="\n\n")
54+
55+
56+
def outgoing_api_message_received(body: dict) -> None:
57+
timestamp = body["timestamp"]
58+
time = get_notification_time(timestamp)
59+
60+
data = dumps(body, ensure_ascii=False, indent=4)
61+
62+
print(f"New outgoing API message at {time} with data: {data}", end="\n\n")
63+
64+
65+
def outgoing_message_status(body: dict) -> None:
66+
timestamp = body["timestamp"]
67+
time = get_notification_time(timestamp)
68+
69+
data = dumps(body, ensure_ascii=False, indent=4)
70+
71+
response = (
72+
f"Status of sent message has been updated at {time} with data: {data}"
73+
)
74+
print(response, end="\n\n")
75+
76+
77+
def state_instance_changed(body: dict) -> None:
78+
timestamp = body["timestamp"]
79+
time = get_notification_time(timestamp)
80+
81+
data = dumps(body, ensure_ascii=False, indent=4)
82+
83+
print(f"Current instance state at {time} with data: {data}", end="\n\n")
84+
85+
86+
def device_info(body: dict) -> None:
87+
timestamp = body["timestamp"]
88+
time = get_notification_time(timestamp)
89+
90+
data = dumps(body, ensure_ascii=False, indent=4)
91+
92+
response = (
93+
f"Current device information at {time} with data: {data}"
94+
)
95+
print(response, end="\n\n")
96+
97+
98+
def incoming_call(body: dict) -> None:
99+
timestamp = body["timestamp"]
100+
time = get_notification_time(timestamp)
101+
102+
data = dumps(body, ensure_ascii=False, indent=4)
103+
104+
print(f"New incoming call at {time} with data: {data}", end="\n\n")
105+
106+
107+
def status_instance_changed(body: dict) -> None:
108+
timestamp = body["timestamp"]
109+
time = get_notification_time(timestamp)
110+
111+
data = dumps(body, ensure_ascii=False, indent=4)
112+
113+
print(f"Current instance status at {time} with data: {data}", end="\n\n")
114+
115+
116+
if __name__ == '__main__':
117+
main()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
"Operating System :: OS Independent",
2727
"Programming Language :: Python",
2828
"Programming Language :: Python :: 3",
29+
"Programming Language :: Python :: 3 :: Only",
2930
"Programming Language :: Python :: 3.7",
3031
"Programming Language :: Python :: 3.8",
3132
"Programming Language :: Python :: 3.9",
3233
"Programming Language :: Python :: 3.10",
3334
"Programming Language :: Python :: 3.11",
34-
"Programming Language :: Python :: 3 :: Only",
3535
"Topic :: Communications",
3636
"Topic :: Communications :: Chat",
3737
"Topic :: Software Development",

0 commit comments

Comments
 (0)