-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathpayload.py
More file actions
140 lines (119 loc) · 5.13 KB
/
payload.py
File metadata and controls
140 lines (119 loc) · 5.13 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import asyncio
from whatsapp_api_client_python import API
class GreenAPIDemo:
def __init__(self):
self.greenAPI = API.GreenAPI("1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345")
self.test_chat = "79876543210@c.us"
async def run_demo(self):
tasks = [
self.demo_account_management(),
self.demo_contacts(),
self.demo_sending_messages(),
self.demo_journals(),
self.demo_queues()
]
await asyncio.gather(*tasks, return_exceptions=True)
async def demo_account_management(self):
response = await self.greenAPI.account.getStateInstanceAsync()
if response.code == 200:
print(response.data)
response = await self.greenAPI.account.getSettingsAsync()
if response.code == 200:
settings = response.data
print("getSettings:")
print(f" - delaySendMessagesMilliseconds: {settings.get('delaySendMessagesMilliseconds', 'N/A')}ms")
print(f" - incomingWebhook: {settings.get('incomingWebhook', 'N/A')}")
print(f" - outgoingWebhook: {settings.get('outgoingWebhook', 'N/A')}")
new_settings = {"delaySendMessagesMilliseconds": 1000, "outgoingWebhook": "yes", "incomingWebhook": "yes"}
response = await self.greenAPI.account.setSettingsAsync(new_settings)
if response.code == 200:
print("setSettings: ", response.data)
response = await self.greenAPI.account.qrAsync()
if response.code == 200:
print(f"QR received (data size: {len(response.data)} bytes)")
async def demo_contacts(self):
response = await self.greenAPI.serviceMethods.getContactsAsync()
if response.code == 200:
contacts = response.data
print(f"getContacts: {len(contacts)} contacts were received")
for i, contact in enumerate(contacts[:3]):
print(f" {i+1}. {contact.get('name', 'No name')} - {contact.get('id')}")
test_numbers = [79876543210, 79001234568]
for number in test_numbers:
response = await self.greenAPI.serviceMethods.checkWhatsappAsync(number)
if response.code == 200:
exists = response.data.get('existsWhatsapp', False)
status = "Whatsapp exists" if exists else "WhatsApp don't exist"
print(f"Phone: {number}: {status}")
async def demo_sending_messages(self):
response = await self.greenAPI.sending.sendMessageAsync(
self.test_chat,
"This message was sent from Green-API SDK Python"
)
if response.code == 200:
print("Text message sent: ", response.data)
response = await self.greenAPI.sending.sendMessageAsync(
self.test_chat,
"Checking link preview: https://green-api.com",
linkPreview=True
)
if response.code == 200:
print("Message with preview sent: ", response.data)
response = await self.greenAPI.sending.sendPollAsync(
self.test_chat,
"Wake me up",
[
{"optionName": "Wake me up inside"},
{"optionName": "Before you go go"},
{"optionName": "When september ends"}
],
multipleAnswers=False
)
if response.code == 200:
print("Poll message sent: ", response.data)
contact = {
"phoneContact": 79876543210,
"firstName": "Jane",
"lastName": "Doe"
}
response = await self.greenAPI.sending.sendContactAsync(
self.test_chat,
contact
)
if response.code == 200:
print("Contact message sent: ", response.data)
response = await self.greenAPI.sending.sendLocationAsync(
"79876543210@c.us",
44.9370129,
89.8728409,
"Restaurant",
"123456, Best Place"
)
if response.code == 200:
print("Location message sent: ", response.data)
async def demo_journals(self):
response = await self.greenAPI.journals.lastIncomingMessagesAsync(minutes=1440)
if response.code == 200:
messages = response.data
print(f"lastIncomingMessages: {len(messages)}")
for msg in messages[:2]:
print(f" - From: {msg.get('senderId')}")
print(f" Text: {msg.get('textMessage', 'Media/File')}")
async def demo_queues(self):
response = await self.greenAPI.queues.showMessagesQueueAsync()
if response.code == 200:
queue = response.data
print(f"MessagesQueue: {len(queue)}")
print("Waiting 5 seconds... (for all messages to send)")
await asyncio.sleep(5)
response = await self.greenAPI.queues.clearMessagesQueueAsync()
if response.code == 200:
print("Queue cleared: ", response.data)
async def main():
demo = GreenAPIDemo()
try:
await demo.run_demo()
except Exception as e:
print(f"error: {e}")
if __name__ == '__main__':
asyncio.run(main())