whatsapp-python 3.3.5.dev0__py3-none-any.whl → 3.3.9__py3-none-any.whl

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,267 +1,364 @@
1
- import logging
2
- import requests
3
-
4
-
5
- def send_location(self, lat: str, long: str, name: str, address: str, recipient_id: str) -> dict:
6
- """
7
- Sends a location message to a WhatsApp user
8
-
9
- Args:
10
- lat[str]: Latitude of the location
11
- long[str]: Longitude of the location
12
- name[str]: Name of the location
13
- address[str]: Address of the location
14
- recipient_id[str]: Phone number of the user with country code wihout +
15
-
16
- Example:
17
- >>> from whatsapp import WhatsApp
18
- >>> whatsapp = WhatsApp(token, phone_number_id)
19
- >>> whatsapp.send_location("-23.564", "-46.654", "My Location", "Rua dois, 123", "5511999999999")
20
- """
21
- data = {
22
- "messaging_product": "whatsapp",
23
- "to": recipient_id,
24
- "type": "location",
25
- "location": {
26
- "latitude": lat,
27
- "longitude": long,
28
- "name": name,
29
- "address": address,
30
- },
31
- }
32
- logging.info(f"Sending location to {recipient_id}")
33
- r = requests.post(self.url, headers=self.headers, json=data)
34
- if r.status_code == 200:
35
- logging.info(f"Location sent to {recipient_id}")
36
- return r.json()
37
- logging.info(f"Location not sent to {recipient_id}")
38
- logging.info(f"Status code: {r.status_code}")
39
- logging.error(r.json())
40
- return r.json()
41
-
42
-
43
- def send_image(
44
- self,
45
- image: str,
46
- recipient_id: str,
47
- recipient_type: str = "individual",
48
- caption: str = "",
49
- link: bool = True,
50
- ) -> dict:
51
- """
52
- Sends an image message to a WhatsApp user
53
-
54
- There are two ways to send an image message to a user, either by passing the image id or by passing the image link.
55
- Image id is the id of the image uploaded to the cloud api.
56
-
57
- Args:
58
- image[str]: Image id or link of the image
59
- recipient_id[str]: Phone number of the user with country code wihout +
60
- recipient_type[str]: Type of the recipient, either individual or group
61
- caption[str]: Caption of the image
62
- link[bool]: Whether to send an image id or an image link, True means that the image is an id, False means that the image is a link
63
-
64
-
65
- Example:
66
- >>> from whatsapp import WhatsApp
67
- >>> whatsapp = WhatsApp(token, phone_number_id)
68
- >>> whatsapp.send_image("https://i.imgur.com/Fh7XVYY.jpeg", "5511999999999")
69
- """
70
- if link:
71
- data = {
72
- "messaging_product": "whatsapp",
73
- "recipient_type": recipient_type,
74
- "to": recipient_id,
75
- "type": "image",
76
- "image": {"link": image, "caption": caption},
77
- }
78
- else:
79
- data = {
80
- "messaging_product": "whatsapp",
81
- "recipient_type": recipient_type,
82
- "to": recipient_id,
83
- "type": "image",
84
- "image": {"id": image, "caption": caption},
85
- }
86
- logging.info(f"Sending image to {recipient_id}")
87
- r = requests.post(self.url, headers=self.headers, json=data)
88
- if r.status_code == 200:
89
- logging.info(f"Image sent to {recipient_id}")
90
- return r.json()
91
- logging.info(f"Image not sent to {recipient_id}")
92
- logging.info(f"Status code: {r.status_code}")
93
- logging.error(r.json())
94
- return r.json()
95
-
96
-
97
- def send_sticker(self, sticker: str, recipient_id: str, recipient_type: str = "individual", link: bool = True) -> dict:
98
- """
99
- Sends a sticker message to a WhatsApp user
100
-
101
- There are two ways to send a sticker message to a user, either by passing the image id or by passing the sticker link.
102
- Sticker id is the id of the sticker uploaded to the cloud api.
103
-
104
- Args:
105
- sticker[str]: Sticker id or link of the sticker
106
- recipient_id[str]: Phone number of the user with country code wihout +
107
- recipient_type[str]: Type of the recipient, either individual or group
108
- link[bool]: Whether to send an sticker id or an sticker link, True means that the sticker is an id, False means that the image is a link
109
-
110
-
111
- Example:
112
- >>> from whatsapp import WhatsApp
113
- >>> whatsapp = WhatsApp(token, phone_number_id)
114
- >>> whatsapp.send_sticker("170511049062862", "5511999999999", link=False)
115
- """
116
- if link:
117
- data = {
118
- "messaging_product": "whatsapp",
119
- "recipient_type": recipient_type,
120
- "to": recipient_id,
121
- "type": "sticker",
122
- "sticker": {"link": sticker},
123
- }
124
- else:
125
- data = {
126
- "messaging_product": "whatsapp",
127
- "recipient_type": recipient_type,
128
- "to": recipient_id,
129
- "type": "sticker",
130
- "sticker": {"id": sticker},
131
- }
132
- logging.info(f"Sending sticker to {recipient_id}")
133
- r = requests.post(self.url, headers=self.headers, json=data)
134
- if r.status_code == 200:
135
- logging.info(f"Sticker sent to {recipient_id}")
136
- return r.json()
137
- logging.info(f"Sticker not sent to {recipient_id}")
138
- logging.info(f"Status code: {r.status_code}")
139
- logging.error(r.json())
140
- return r.json()
141
-
142
-
143
- def send_audio(self, audio: str, recipient_id: str, link: bool = True) -> dict:
144
- """
145
- Sends an audio message to a WhatsApp user
146
- Audio messages can either be sent by passing the audio id or by passing the audio link.
147
-
148
- Args:
149
- audio[str]: Audio id or link of the audio
150
- recipient_id[str]: Phone number of the user with country code wihout +
151
- link[bool]: Whether to send an audio id or an audio link, True means that the audio is an id, False means that the audio is a link
152
-
153
- Example:
154
- >>> from whatsapp import WhatsApp
155
- >>> whatsapp = WhatsApp(token, phone_number_id)
156
- >>> whatsapp.send_audio("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3", "5511999999999")
157
- """
158
- if link:
159
- data = {
160
- "messaging_product": "whatsapp",
161
- "to": recipient_id,
162
- "type": "audio",
163
- "audio": {"link": audio},
164
- }
165
- else:
166
- data = {
167
- "messaging_product": "whatsapp",
168
- "to": recipient_id,
169
- "type": "audio",
170
- "audio": {"id": audio},
171
- }
172
- logging.info(f"Sending audio to {recipient_id}")
173
- r = requests.post(self.url, headers=self.headers, json=data)
174
- if r.status_code == 200:
175
- logging.info(f"Audio sent to {recipient_id}")
176
- return r.json()
177
- logging.info(f"Audio not sent to {recipient_id}")
178
- logging.info(f"Status code: {r.status_code}")
179
- logging.error(f"Response: {r.json()}")
180
- return r.json()
181
-
182
-
183
- def send_video(
184
- self, video: str, recipient_id: str, caption: str = "", link: bool = True
185
- ) -> dict:
186
- """ "
187
- Sends a video message to a WhatsApp user
188
- Video messages can either be sent by passing the video id or by passing the video link.
189
-
190
- Args:
191
- video[str]: Video id or link of the video
192
- recipient_id[str]: Phone number of the user with country code wihout +
193
- caption[str]: Caption of the video
194
- link[bool]: Whether to send a video id or a video link, True means that the video is an id, False means that the video is a link
195
-
196
- example:
197
- >>> from whatsapp import WhatsApp
198
- >>> whatsapp = WhatsApp(token, phone_number_id)
199
- >>> whatsapp.send_video("https://www.youtube.com/watch?v=dQw4w9WgXcQ", "5511999999999")
200
- """
201
- if link:
202
- data = {
203
- "messaging_product": "whatsapp",
204
- "to": recipient_id,
205
- "type": "video",
206
- "video": {"link": video, "caption": caption},
207
- }
208
- else:
209
- data = {
210
- "messaging_product": "whatsapp",
211
- "to": recipient_id,
212
- "type": "video",
213
- "video": {"id": video, "caption": caption},
214
- }
215
- logging.info(f"Sending video to {recipient_id}")
216
- r = requests.post(self.url, headers=self.headers, json=data)
217
- if r.status_code == 200:
218
- logging.info(f"Video sent to {recipient_id}")
219
- return r.json()
220
- logging.info(f"Video not sent to {recipient_id}")
221
- logging.info(f"Status code: {r.status_code}")
222
- logging.error(f"Response: {r.json()}")
223
- return r.json()
224
-
225
-
226
- def send_document(
227
- self, document: str, recipient_id: str, caption: str = "", link: bool = True
228
- ) -> dict:
229
- """ "
230
- Sends a document message to a WhatsApp user
231
- Document messages can either be sent by passing the document id or by passing the document link.
232
-
233
- Args:
234
- document[str]: Document id or link of the document
235
- recipient_id[str]: Phone number of the user with country code wihout +
236
- caption[str]: Caption of the document
237
- link[bool]: Whether to send a document id or a document link, True means that the document is an id, False means that the document is a link
238
-
239
- Example:
240
- >>> from whatsapp import WhatsApp
241
- >>> whatsapp = WhatsApp(token, phone_number_id)
242
- >>> whatsapp.send_document("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf", "5511999999999")
243
- """
244
- if link:
245
- data = {
246
- "messaging_product": "whatsapp",
247
- "to": recipient_id,
248
- "type": "document",
249
- "document": {"link": document, "caption": caption},
250
- }
251
- else:
252
- data = {
253
- "messaging_product": "whatsapp",
254
- "to": recipient_id,
255
- "type": "document",
256
- "document": {"id": document, "caption": caption},
257
- }
258
-
259
- logging.info(f"Sending document to {recipient_id}")
260
- r = requests.post(self.url, headers=self.headers, json=data)
261
- if r.status_code == 200:
262
- logging.info(f"Document sent to {recipient_id}")
263
- return r.json()
264
- logging.info(f"Document not sent to {recipient_id}")
265
- logging.info(f"Status code: {r.status_code}")
266
- logging.error(f"Response: {r.json()}")
267
- return r.json()
1
+ import logging
2
+ import requests
3
+
4
+
5
+ def send_location(self, lat: str, long: str, name: str, address: str, recipient_id: str, sender = None) -> dict:
6
+ """
7
+ Sends a location message to a WhatsApp user
8
+
9
+ Args:
10
+ lat[str]: Latitude of the location
11
+ long[str]: Longitude of the location
12
+ name[str]: Name of the location
13
+ address[str]: Address of the location
14
+ recipient_id[str]: Phone number of the user with country code wihout +
15
+
16
+ Example:
17
+ >>> from whatsapp import WhatsApp
18
+ >>> whatsapp = WhatsApp(token, phone_number_id)
19
+ >>> whatsapp.send_location("-23.564", "-46.654", "My Location", "Rua dois, 123", "5511999999999")
20
+ """
21
+ try:
22
+ sender = dict(self.instance.l)[sender]
23
+ print(self.instance.l)
24
+ print(sender)
25
+
26
+
27
+ print(12)
28
+
29
+ except:
30
+ print(22)
31
+ sender = self.instance.phone_number_id
32
+
33
+ if sender == None:
34
+ sender = self.instance.phone_number_id
35
+
36
+ url = f"https://graph.facebook.com/v18.0/{sender}/messages"
37
+ data = {
38
+ "messaging_product": "whatsapp",
39
+ "to": recipient_id,
40
+ "type": "location",
41
+ "location": {
42
+ "latitude": lat,
43
+ "longitude": long,
44
+ "name": name,
45
+ "address": address,
46
+ },
47
+ }
48
+ logging.info(f"Sending location to {recipient_id}")
49
+ r = requests.post(url, headers=self.headers, json=data)
50
+ if r.status_code == 200:
51
+ logging.info(f"Location sent to {recipient_id}")
52
+ return r.json()
53
+ logging.info(f"Location not sent to {recipient_id}")
54
+ logging.info(f"Status code: {r.status_code}")
55
+ logging.error(r.json())
56
+ return r.json()
57
+
58
+
59
+ def send_image(
60
+ self,
61
+ image: str,
62
+ recipient_id: str,
63
+ recipient_type: str = "individual",
64
+ caption: str = "",
65
+ link: bool = True,
66
+ sender = None
67
+ ) -> dict:
68
+ """
69
+ Sends an image message to a WhatsApp user
70
+
71
+ There are two ways to send an image message to a user, either by passing the image id or by passing the image link.
72
+ Image id is the id of the image uploaded to the cloud api.
73
+
74
+ Args:
75
+ image[str]: Image id or link of the image
76
+ recipient_id[str]: Phone number of the user with country code wihout +
77
+ recipient_type[str]: Type of the recipient, either individual or group
78
+ caption[str]: Caption of the image
79
+ link[bool]: Whether to send an image id or an image link, True means that the image is an id, False means that the image is a link
80
+
81
+
82
+ Example:
83
+ >>> from whatsapp import WhatsApp
84
+ >>> whatsapp = WhatsApp(token, phone_number_id)
85
+ >>> whatsapp.send_image("https://i.imgur.com/Fh7XVYY.jpeg", "5511999999999")
86
+ """
87
+ try:
88
+ sender = dict(self.l)[sender]
89
+ print(self.l)
90
+ print(sender)
91
+
92
+
93
+ print(12)
94
+
95
+ except:
96
+ print(22)
97
+ sender = self.phone_number_id
98
+
99
+ if sender == None:
100
+ sender = self.phone_number_id
101
+
102
+ url = f"https://graph.facebook.com/v18.0/{sender}/messages"
103
+ if link:
104
+ data = {
105
+ "messaging_product": "whatsapp",
106
+ "recipient_type": recipient_type,
107
+ "to": recipient_id,
108
+ "type": "image",
109
+ "image": {"link": image, "caption": caption},
110
+ }
111
+ else:
112
+ data = {
113
+ "messaging_product": "whatsapp",
114
+ "recipient_type": recipient_type,
115
+ "to": recipient_id,
116
+ "type": "image",
117
+ "image": {"id": image, "caption": caption},
118
+ }
119
+ logging.info(f"Sending image to {recipient_id}")
120
+ r = requests.post(url, headers=self.headers, json=data)
121
+ if r.status_code == 200:
122
+ logging.info(f"Image sent to {recipient_id}")
123
+ return r.json()
124
+ logging.info(f"Image not sent to {recipient_id}")
125
+ logging.info(f"Status code: {r.status_code}")
126
+ logging.error(r.json())
127
+ return r.json()
128
+
129
+
130
+ def send_sticker(self, sticker: str, recipient_id: str, recipient_type: str = "individual", link: bool = True, sender = None) -> dict:
131
+ """
132
+ Sends a sticker message to a WhatsApp user
133
+
134
+ There are two ways to send a sticker message to a user, either by passing the image id or by passing the sticker link.
135
+ Sticker id is the id of the sticker uploaded to the cloud api.
136
+
137
+ Args:
138
+ sticker[str]: Sticker id or link of the sticker
139
+ recipient_id[str]: Phone number of the user with country code wihout +
140
+ recipient_type[str]: Type of the recipient, either individual or group
141
+ link[bool]: Whether to send an sticker id or an sticker link, True means that the sticker is an id, False means that the image is a link
142
+
143
+
144
+ Example:
145
+ >>> from whatsapp import WhatsApp
146
+ >>> whatsapp = WhatsApp(token, phone_number_id)
147
+ >>> whatsapp.send_sticker("170511049062862", "5511999999999", link=False)
148
+ """
149
+ try:
150
+ sender = dict(self.l)[sender]
151
+ print(self.l)
152
+ print(sender)
153
+
154
+
155
+ print(12)
156
+
157
+ except:
158
+ print(22)
159
+ sender = self.phone_number_id
160
+
161
+ if sender == None:
162
+ sender = self.phone_number_id
163
+
164
+ url = f"https://graph.facebook.com/v18.0/{sender}/messages"
165
+ if link:
166
+ data = {
167
+ "messaging_product": "whatsapp",
168
+ "recipient_type": recipient_type,
169
+ "to": recipient_id,
170
+ "type": "sticker",
171
+ "sticker": {"link": sticker},
172
+ }
173
+ else:
174
+ data = {
175
+ "messaging_product": "whatsapp",
176
+ "recipient_type": recipient_type,
177
+ "to": recipient_id,
178
+ "type": "sticker",
179
+ "sticker": {"id": sticker},
180
+ }
181
+ logging.info(f"Sending sticker to {recipient_id}")
182
+ r = requests.post(url, headers=self.headers, json=data)
183
+ if r.status_code == 200:
184
+ logging.info(f"Sticker sent to {recipient_id}")
185
+ return r.json()
186
+ logging.info(f"Sticker not sent to {recipient_id}")
187
+ logging.info(f"Status code: {r.status_code}")
188
+ logging.error(r.json())
189
+ return r.json()
190
+
191
+
192
+ def send_audio(self, audio: str, recipient_id: str, link: bool = True, sender = None) -> dict:
193
+ """
194
+ Sends an audio message to a WhatsApp user
195
+ Audio messages can either be sent by passing the audio id or by passing the audio link.
196
+
197
+ Args:
198
+ audio[str]: Audio id or link of the audio
199
+ recipient_id[str]: Phone number of the user with country code wihout +
200
+ link[bool]: Whether to send an audio id or an audio link, True means that the audio is an id, False means that the audio is a link
201
+
202
+ Example:
203
+ >>> from whatsapp import WhatsApp
204
+ >>> whatsapp = WhatsApp(token, phone_number_id)
205
+ >>> whatsapp.send_audio("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3", "5511999999999")
206
+ """
207
+ try:
208
+ sender = dict(self.l)[sender]
209
+ print(self.l)
210
+ print(sender)
211
+
212
+
213
+ print(12)
214
+
215
+ except:
216
+ print(22)
217
+ sender = self.phone_number_id
218
+
219
+ if sender == None:
220
+ sender = self.phone_number_id
221
+
222
+ url = f"https://graph.facebook.com/v18.0/{sender}/messages"
223
+ if link:
224
+ data = {
225
+ "messaging_product": "whatsapp",
226
+ "to": recipient_id,
227
+ "type": "audio",
228
+ "audio": {"link": audio},
229
+ }
230
+ else:
231
+ data = {
232
+ "messaging_product": "whatsapp",
233
+ "to": recipient_id,
234
+ "type": "audio",
235
+ "audio": {"id": audio},
236
+ }
237
+ logging.info(f"Sending audio to {recipient_id}")
238
+ r = requests.post(url, headers=self.headers, json=data)
239
+ if r.status_code == 200:
240
+ logging.info(f"Audio sent to {recipient_id}")
241
+ return r.json()
242
+ logging.info(f"Audio not sent to {recipient_id}")
243
+ logging.info(f"Status code: {r.status_code}")
244
+ logging.error(f"Response: {r.json()}")
245
+ return r.json()
246
+
247
+
248
+ def send_video(
249
+ self, video: str, recipient_id: str, caption: str = "", link: bool = True, sender = None
250
+ ) -> dict:
251
+ """ "
252
+ Sends a video message to a WhatsApp user
253
+ Video messages can either be sent by passing the video id or by passing the video link.
254
+
255
+ Args:
256
+ video[str]: Video id or link of the video
257
+ recipient_id[str]: Phone number of the user with country code wihout +
258
+ caption[str]: Caption of the video
259
+ link[bool]: Whether to send a video id or a video link, True means that the video is an id, False means that the video is a link
260
+
261
+ example:
262
+ >>> from whatsapp import WhatsApp
263
+ >>> whatsapp = WhatsApp(token, phone_number_id)
264
+ >>> whatsapp.send_video("https://www.youtube.com/watch?v=dQw4w9WgXcQ", "5511999999999")
265
+ """
266
+ try:
267
+ sender = dict(self.l)[sender]
268
+ print(self.l)
269
+ print(sender)
270
+
271
+
272
+ print(12)
273
+
274
+ except:
275
+ print(22)
276
+ sender = self.phone_number_id
277
+
278
+ if sender == None:
279
+ sender = self.phone_number_id
280
+
281
+ url = f"https://graph.facebook.com/v18.0/{sender}/messages"
282
+ if link:
283
+ data = {
284
+ "messaging_product": "whatsapp",
285
+ "to": recipient_id,
286
+ "type": "video",
287
+ "video": {"link": video, "caption": caption},
288
+ }
289
+ else:
290
+ data = {
291
+ "messaging_product": "whatsapp",
292
+ "to": recipient_id,
293
+ "type": "video",
294
+ "video": {"id": video, "caption": caption},
295
+ }
296
+ logging.info(f"Sending video to {recipient_id}")
297
+ r = requests.post(url, headers=self.headers, json=data)
298
+ if r.status_code == 200:
299
+ logging.info(f"Video sent to {recipient_id}")
300
+ return r.json()
301
+ logging.info(f"Video not sent to {recipient_id}")
302
+ logging.info(f"Status code: {r.status_code}")
303
+ logging.error(f"Response: {r.json()}")
304
+ return r.json()
305
+
306
+
307
+ def send_document(
308
+ self, document: str, recipient_id: str, caption: str = "", link: bool = True
309
+ ) -> dict:
310
+ """ "
311
+ Sends a document message to a WhatsApp user
312
+ Document messages can either be sent by passing the document id or by passing the document link.
313
+
314
+ Args:
315
+ document[str]: Document id or link of the document
316
+ recipient_id[str]: Phone number of the user with country code wihout +
317
+ caption[str]: Caption of the document
318
+ link[bool]: Whether to send a document id or a document link, True means that the document is an id, False means that the document is a link
319
+
320
+ Example:
321
+ >>> from whatsapp import WhatsApp
322
+ >>> whatsapp = WhatsApp(token, phone_number_id)
323
+ >>> whatsapp.send_document("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf", "5511999999999")
324
+ """
325
+ try:
326
+ sender = dict(self.l)[sender]
327
+ print(self.l)
328
+ print(sender)
329
+
330
+
331
+ print(12)
332
+
333
+ except:
334
+ print(22)
335
+ sender = self.phone_number_id
336
+
337
+ if sender == None:
338
+ sender = self.phone_number_id
339
+
340
+ url = f"https://graph.facebook.com/v18.0/{sender}/messages"
341
+ if link:
342
+ data = {
343
+ "messaging_product": "whatsapp",
344
+ "to": recipient_id,
345
+ "type": "document",
346
+ "document": {"link": document, "caption": caption},
347
+ }
348
+ else:
349
+ data = {
350
+ "messaging_product": "whatsapp",
351
+ "to": recipient_id,
352
+ "type": "document",
353
+ "document": {"id": document, "caption": caption},
354
+ }
355
+
356
+ logging.info(f"Sending document to {recipient_id}")
357
+ r = requests.post(url, headers=self.headers, json=data)
358
+ if r.status_code == 200:
359
+ logging.info(f"Document sent to {recipient_id}")
360
+ return r.json()
361
+ logging.info(f"Document not sent to {recipient_id}")
362
+ logging.info(f"Status code: {r.status_code}")
363
+ logging.error(f"Response: {r.json()}")
364
+ return r.json()