whatsapp-python 3.2.5__py3-none-any.whl → 3.3.5.dev0__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.
whatsapp/__init__.py CHANGED
@@ -5,6 +5,7 @@ from __future__ import annotations
5
5
 
6
6
  import requests
7
7
  import logging
8
+ from typing import Union
8
9
  from fastapi import FastAPI, HTTPException, Request
9
10
  from uvicorn import run as _run
10
11
  from .constants import VERSION
@@ -19,7 +20,7 @@ import json
19
20
 
20
21
 
21
22
  class WhatsApp(object):
22
- def __init__(self, token: str = "", phone_number_id: str = "", logger: bool = True, update_check: bool = True, verify_token: str = "", debug: bool = True):
23
+ def __init__(self, token: str = "", phone_number_id: Union[str, dict] = "", logger: bool = True, update_check: bool = True, verify_token: str = "", debug: bool = True):
23
24
  """
24
25
  Initialize the WhatsApp Object
25
26
 
@@ -32,6 +33,25 @@ class WhatsApp(object):
32
33
  # Check if the version is up to date
33
34
  logging.getLogger(__name__).addHandler(logging.NullHandler())
34
35
 
36
+ self.l = phone_number_id
37
+ print(self.l)
38
+
39
+ if isinstance(phone_number_id, dict):
40
+ # use first phone number id as default
41
+ phone_number_id = phone_number_id[list(phone_number_id.keys())[0]]
42
+ print(phone_number_id)
43
+
44
+ elif phone_number_id == "":
45
+ logging.error("Phone number ID not provided")
46
+ raise ValueError(
47
+ "Phone number ID not provided but required")
48
+ elif isinstance(phone_number_id, str):
49
+ logging.critical(
50
+ "The phone number ID should be a dictionary of phone numbers and their IDs. Using strings is deprecated and will be removed in the next version.")
51
+ print(phone_number_id)
52
+ else:
53
+ pass
54
+
35
55
  self.VERSION = VERSION
36
56
  if update_check is True:
37
57
  latest = str(requests.get(
@@ -206,10 +226,12 @@ class WhatsApp(object):
206
226
 
207
227
 
208
228
  class Message(object):
209
- def __init__(self, id: int = None, data: dict = {}, instance: WhatsApp = None, content: str = "", to: str = "", rec_type: str = "individual"): # type: ignore
229
+ # type: ignore
230
+ def __init__(self, id: int = None, data: dict = {}, instance: WhatsApp = None, content: str = "", to: str = "", rec_type: str = "individual"):
210
231
  self.instance = instance
211
232
  self.url = self.instance.url
212
233
  self.headers = self.instance.headers
234
+
213
235
 
214
236
  try:
215
237
  self.id = instance.get_message_id(data)
@@ -227,10 +249,6 @@ class Message(object):
227
249
  data)
228
250
  except:
229
251
  self.content = content
230
- try:
231
- self.sender = self.instance.get_mobile(data)
232
- except:
233
- self.sender = None
234
252
  try:
235
253
  self.name = self.instance.get_name(data)
236
254
  except:
@@ -272,4 +290,97 @@ class Message(object):
272
290
  except:
273
291
  pass
274
292
 
275
- from .ext._message import send, reply, mark_as_read, react
293
+ def reply(self, reply_text: str = "", preview_url: bool = True) -> dict:
294
+ if self.data == {}:
295
+ return {"error": "No data provided"}
296
+ author = self.instance.get_author(self.data)
297
+ payload = {
298
+ "messaging_product": "whatsapp",
299
+ "recipient_type": "individual",
300
+ "to": str(author),
301
+ "type": "text",
302
+ "context": {"message_id": self.id},
303
+ "text": {"preview_url": preview_url, "body": reply_text},
304
+ }
305
+ logging.info(f"Replying to {self.id}")
306
+ r = requests.post(self.url, headers=self.headers, json=payload)
307
+ if r.status_code == 200:
308
+ logging.info(f"Message sent to {
309
+ self.instance.get_author(self.data)}")
310
+ return r.json()
311
+ logging.info(f"Message not sent to {
312
+ self.instance.get_author(self.data)}")
313
+ logging.info(f"Status code: {r.status_code}")
314
+ logging.error(f"Response: {r.json()}")
315
+ return r.json()
316
+
317
+ def mark_as_read(self) -> dict:
318
+
319
+ payload = {
320
+ "messaging_product": "whatsapp",
321
+ "status": "read",
322
+ "message_id": self.id,
323
+ }
324
+
325
+ response = requests.post(
326
+ f"{self.instance.url}", headers=self.instance.headers, json=payload)
327
+ if response.status_code == 200:
328
+ logging.info(response.json())
329
+ return response.json()
330
+ else:
331
+ logging.error(response.json())
332
+ return response.json()
333
+
334
+ def send(self,sender = None, preview_url: bool = True) -> dict:
335
+
336
+ try:
337
+ sender = dict(self.instance.l)[sender]
338
+ print(self.instance.l)
339
+ print(sender)
340
+
341
+
342
+ print(12)
343
+
344
+ except:
345
+ print(22)
346
+ sender = self.instance.phone_number_id
347
+
348
+ if sender == None:
349
+ sender = self.instance.phone_number_id
350
+
351
+ url = f"https://graph.facebook.com/v18.0/{sender}/messages"
352
+ data = {
353
+ "messaging_product": "whatsapp",
354
+ "recipient_type": self.rec,
355
+ "to": self.to,
356
+ "type": "text",
357
+ "text": {"preview_url": preview_url, "body": self.content},
358
+ }
359
+ logging.info(f"Sending message to {self.to}")
360
+ print(url)
361
+ r = requests.post(url, headers=self.headers, json=data)
362
+ if r.status_code == 200:
363
+ logging.info(f"Message sent to {self.to}")
364
+ return r.json()
365
+ logging.info(f"Message not sent to {self.to}")
366
+ logging.info(f"Status code: {r.status_code}")
367
+ logging.error(f"Response: {r.json()}")
368
+ return r.json()
369
+
370
+ def react(self, emoji: str) -> dict:
371
+ data = {
372
+ "messaging_product": "whatsapp",
373
+ "recipient_type": "individual",
374
+ "to": self.to,
375
+ "type": "reaction",
376
+ "reaction": {"message_id": self.id, "emoji": emoji},
377
+ }
378
+ logging.info(f"Reacting to {self.id}")
379
+ r = requests.post(self.url, headers=self.headers, json=data)
380
+ if r.status_code == 200:
381
+ logging.info(f"Reaction sent to {self.to}")
382
+ return r.json()
383
+ logging.info(f"Reaction not sent to {self.to}")
384
+ logging.info(f"Status code: {r.status_code}")
385
+ logging.debug(f"Response: {r.json()}")
386
+ return r.json()
whatsapp/constants.py CHANGED
@@ -1,4 +1,4 @@
1
1
  # This file contains the constants used in the project.
2
2
  # The VERSION constant is used to store the version of the project - it's not only used in the __init__.py file, but also in the pyproject.toml file.
3
3
 
4
- VERSION = "3.2.5"
4
+ VERSION = "3.3.5.dev0"
whatsapp/ext/_message.py CHANGED
@@ -104,6 +104,8 @@ def mark_as_read(self) -> dict:
104
104
 
105
105
 
106
106
  def send(self, preview_url: bool = True) -> dict:
107
+ print(self.sender)
108
+ url = f"https://graph.facebook.com/v18.0/{self.sender}/messages"
107
109
  data = {
108
110
  "messaging_product": "whatsapp",
109
111
  "recipient_type": self.rec,
@@ -112,7 +114,7 @@ def send(self, preview_url: bool = True) -> dict:
112
114
  "text": {"preview_url": preview_url, "body": self.content},
113
115
  }
114
116
  logging.info(f"Sending message to {self.to}")
115
- r = requests.post(f"{self.url}", headers=self.headers, json=data)
117
+ r = requests.post(f"{url}", headers=self.headers, json=data)
116
118
  if r.status_code == 200:
117
119
  logging.info(f"Message sent to {self.to}")
118
120
  return r.json()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: whatsapp-python
3
- Version: 3.2.5
3
+ Version: 3.3.5.dev0
4
4
  Summary: Open source Python wrapper for the WhatsApp Cloud API
5
5
  Project-URL: Homepage, https://github.com/filipporomani/whatsapp-python
6
6
  Project-URL: Docs, https://github.com/filipporomani/whatsapp-python/wiki
@@ -81,6 +81,7 @@ I fixed some bugs and added many features, however the library still needs a lot
81
81
 
82
82
  - Listening to events (messages, media, etc.)
83
83
  - Sending messages
84
+ - Sending messages from different numbers individually
84
85
  - Marking messages as read
85
86
  - Reacting to messages
86
87
  - Sending Media (images, audio, video and documents)
@@ -0,0 +1,13 @@
1
+ whatsapp/__init__.py,sha256=7G9OXsmH3tIXlUPw5tRvvo9RymKaNdAa0mImS_iD3ys,14548
2
+ whatsapp/constants.py,sha256=LC4qzGNpp1zEedl6VVfWej-2mD6CDYshqSLtaIYL_VI,230
3
+ whatsapp/ext/_buttons.py,sha256=whj0v4Qgow9AWaWztZxUxaHB8o0MnqA8uRjHHsDht6c,2836
4
+ whatsapp/ext/_media.py,sha256=SAawDZ_5ctneC9kLmtxkjmQyI4sbnQkSuKWs9mcYQUM,4377
5
+ whatsapp/ext/_message.py,sha256=pCtE0DsEwd6mTku4wT89SH09mvMi331V_s8R4n4AS4k,4408
6
+ whatsapp/ext/_property.py,sha256=-btASb_1B4t69TRGAm21lgnjgb5yemZteW7Bmmouooc,123
7
+ whatsapp/ext/_send_media.py,sha256=CpvAdFUOh65G628NULqauy4lEM-xmks6-6irsdy31Yc,9769
8
+ whatsapp/ext/_send_others.py,sha256=A6554k98ftStmUu_Wo0wls-2zWuB8ZujaqQoEEgtbZs,2848
9
+ whatsapp/ext/_static.py,sha256=NghY748DarWcpHZ6Xa7DtuVpYnHv3udEgZL5E1YdL4M,9853
10
+ whatsapp_python-3.3.5.dev0.dist-info/METADATA,sha256=gGotqHv5UZYGWEOv_9CIplBhyQWaVbXGxWbo2Rj21-M,6358
11
+ whatsapp_python-3.3.5.dev0.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
12
+ whatsapp_python-3.3.5.dev0.dist-info/licenses/LICENSE,sha256=Lcl62br0APneFzEOsYHEVs0hThCIA8eEauvxuLGd1cs,1076
13
+ whatsapp_python-3.3.5.dev0.dist-info/RECORD,,
@@ -1,13 +0,0 @@
1
- whatsapp/__init__.py,sha256=GJlJBuBmtMN7R8THoRGGYAVgvwJHKHF2sxoF01GgNt8,10516
2
- whatsapp/constants.py,sha256=46gfu0Ww1ZG0lipwh3o8HTNCSze4Dmu87mJ8Gk0DdI8,225
3
- whatsapp/ext/_buttons.py,sha256=whj0v4Qgow9AWaWztZxUxaHB8o0MnqA8uRjHHsDht6c,2836
4
- whatsapp/ext/_media.py,sha256=SAawDZ_5ctneC9kLmtxkjmQyI4sbnQkSuKWs9mcYQUM,4377
5
- whatsapp/ext/_message.py,sha256=vvI7leCqZSX-RGuiJ3CFpifCPniPo_GK5eRgCwUSD1s,4321
6
- whatsapp/ext/_property.py,sha256=-btASb_1B4t69TRGAm21lgnjgb5yemZteW7Bmmouooc,123
7
- whatsapp/ext/_send_media.py,sha256=CpvAdFUOh65G628NULqauy4lEM-xmks6-6irsdy31Yc,9769
8
- whatsapp/ext/_send_others.py,sha256=A6554k98ftStmUu_Wo0wls-2zWuB8ZujaqQoEEgtbZs,2848
9
- whatsapp/ext/_static.py,sha256=NghY748DarWcpHZ6Xa7DtuVpYnHv3udEgZL5E1YdL4M,9853
10
- whatsapp_python-3.2.5.dist-info/METADATA,sha256=dpdwNvJ8WmCmwQrhqlGt4eq6cNJW5ozxBCCbnfAauCo,6298
11
- whatsapp_python-3.2.5.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
12
- whatsapp_python-3.2.5.dist-info/licenses/LICENSE,sha256=Lcl62br0APneFzEOsYHEVs0hThCIA8eEauvxuLGd1cs,1076
13
- whatsapp_python-3.2.5.dist-info/RECORD,,