Skip to content

Commit

Permalink
feat: implement multiple senders
Browse files Browse the repository at this point in the history
  • Loading branch information
filipporomani committed Sep 13, 2024
1 parent 60688f4 commit 32796e9
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 15 deletions.
2 changes: 1 addition & 1 deletion whatsapp/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file contains the constants used in the project.
# 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.

VERSION = "3.3.5.dev0"
VERSION = "3.5.9"
117 changes: 107 additions & 10 deletions whatsapp/ext/_send_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import requests


def send_location(self, lat: str, long: str, name: str, address: str, recipient_id: str) -> dict:
def send_location(self, lat: str, long: str, name: str, address: str, recipient_id: str, sender = None) -> dict:
"""
Sends a location message to a WhatsApp user
Expand All @@ -18,6 +18,22 @@ def send_location(self, lat: str, long: str, name: str, address: str, recipient_
>>> whatsapp = WhatsApp(token, phone_number_id)
>>> whatsapp.send_location("-23.564", "-46.654", "My Location", "Rua dois, 123", "5511999999999")
"""
try:
sender = dict(self.instance.l)[sender]
print(self.instance.l)
print(sender)


print(12)

except:
print(22)
sender = self.instance.phone_number_id

if sender == None:
sender = self.instance.phone_number_id

url = f"https://graph.facebook.com/v18.0/{sender}/messages"
data = {
"messaging_product": "whatsapp",
"to": recipient_id,
Expand All @@ -30,7 +46,7 @@ def send_location(self, lat: str, long: str, name: str, address: str, recipient_
},
}
logging.info(f"Sending location to {recipient_id}")
r = requests.post(self.url, headers=self.headers, json=data)
r = requests.post(url, headers=self.headers, json=data)
if r.status_code == 200:
logging.info(f"Location sent to {recipient_id}")
return r.json()
Expand All @@ -47,6 +63,7 @@ def send_image(
recipient_type: str = "individual",
caption: str = "",
link: bool = True,
sender = None
) -> dict:
"""
Sends an image message to a WhatsApp user
Expand All @@ -67,6 +84,22 @@ def send_image(
>>> whatsapp = WhatsApp(token, phone_number_id)
>>> whatsapp.send_image("https://i.imgur.com/Fh7XVYY.jpeg", "5511999999999")
"""
try:
sender = dict(self.l)[sender]
print(self.l)
print(sender)


print(12)

except:
print(22)
sender = self.phone_number_id

if sender == None:
sender = self.phone_number_id

url = f"https://graph.facebook.com/v18.0/{sender}/messages"
if link:
data = {
"messaging_product": "whatsapp",
Expand All @@ -84,7 +117,7 @@ def send_image(
"image": {"id": image, "caption": caption},
}
logging.info(f"Sending image to {recipient_id}")
r = requests.post(self.url, headers=self.headers, json=data)
r = requests.post(url, headers=self.headers, json=data)
if r.status_code == 200:
logging.info(f"Image sent to {recipient_id}")
return r.json()
Expand All @@ -94,7 +127,7 @@ def send_image(
return r.json()


def send_sticker(self, sticker: str, recipient_id: str, recipient_type: str = "individual", link: bool = True) -> dict:
def send_sticker(self, sticker: str, recipient_id: str, recipient_type: str = "individual", link: bool = True, sender = None) -> dict:
"""
Sends a sticker message to a WhatsApp user
Expand All @@ -113,6 +146,22 @@ def send_sticker(self, sticker: str, recipient_id: str, recipient_type: str = "i
>>> whatsapp = WhatsApp(token, phone_number_id)
>>> whatsapp.send_sticker("170511049062862", "5511999999999", link=False)
"""
try:
sender = dict(self.l)[sender]
print(self.l)
print(sender)


print(12)

except:
print(22)
sender = self.phone_number_id

if sender == None:
sender = self.phone_number_id

url = f"https://graph.facebook.com/v18.0/{sender}/messages"
if link:
data = {
"messaging_product": "whatsapp",
Expand All @@ -130,7 +179,7 @@ def send_sticker(self, sticker: str, recipient_id: str, recipient_type: str = "i
"sticker": {"id": sticker},
}
logging.info(f"Sending sticker to {recipient_id}")
r = requests.post(self.url, headers=self.headers, json=data)
r = requests.post(url, headers=self.headers, json=data)
if r.status_code == 200:
logging.info(f"Sticker sent to {recipient_id}")
return r.json()
Expand All @@ -140,7 +189,7 @@ def send_sticker(self, sticker: str, recipient_id: str, recipient_type: str = "i
return r.json()


def send_audio(self, audio: str, recipient_id: str, link: bool = True) -> dict:
def send_audio(self, audio: str, recipient_id: str, link: bool = True, sender = None) -> dict:
"""
Sends an audio message to a WhatsApp user
Audio messages can either be sent by passing the audio id or by passing the audio link.
Expand All @@ -155,6 +204,22 @@ def send_audio(self, audio: str, recipient_id: str, link: bool = True) -> dict:
>>> whatsapp = WhatsApp(token, phone_number_id)
>>> whatsapp.send_audio("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3", "5511999999999")
"""
try:
sender = dict(self.l)[sender]
print(self.l)
print(sender)


print(12)

except:
print(22)
sender = self.phone_number_id

if sender == None:
sender = self.phone_number_id

url = f"https://graph.facebook.com/v18.0/{sender}/messages"
if link:
data = {
"messaging_product": "whatsapp",
Expand All @@ -170,7 +235,7 @@ def send_audio(self, audio: str, recipient_id: str, link: bool = True) -> dict:
"audio": {"id": audio},
}
logging.info(f"Sending audio to {recipient_id}")
r = requests.post(self.url, headers=self.headers, json=data)
r = requests.post(url, headers=self.headers, json=data)
if r.status_code == 200:
logging.info(f"Audio sent to {recipient_id}")
return r.json()
Expand All @@ -181,7 +246,7 @@ def send_audio(self, audio: str, recipient_id: str, link: bool = True) -> dict:


def send_video(
self, video: str, recipient_id: str, caption: str = "", link: bool = True
self, video: str, recipient_id: str, caption: str = "", link: bool = True, sender = None
) -> dict:
""" "
Sends a video message to a WhatsApp user
Expand All @@ -198,6 +263,22 @@ def send_video(
>>> whatsapp = WhatsApp(token, phone_number_id)
>>> whatsapp.send_video("https://www.youtube.com/watch?v=dQw4w9WgXcQ", "5511999999999")
"""
try:
sender = dict(self.l)[sender]
print(self.l)
print(sender)


print(12)

except:
print(22)
sender = self.phone_number_id

if sender == None:
sender = self.phone_number_id

url = f"https://graph.facebook.com/v18.0/{sender}/messages"
if link:
data = {
"messaging_product": "whatsapp",
Expand All @@ -213,7 +294,7 @@ def send_video(
"video": {"id": video, "caption": caption},
}
logging.info(f"Sending video to {recipient_id}")
r = requests.post(self.url, headers=self.headers, json=data)
r = requests.post(url, headers=self.headers, json=data)
if r.status_code == 200:
logging.info(f"Video sent to {recipient_id}")
return r.json()
Expand Down Expand Up @@ -241,6 +322,22 @@ def send_document(
>>> whatsapp = WhatsApp(token, phone_number_id)
>>> whatsapp.send_document("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf", "5511999999999")
"""
try:
sender = dict(self.l)[sender]
print(self.l)
print(sender)


print(12)

except:
print(22)
sender = self.phone_number_id

if sender == None:
sender = self.phone_number_id

url = f"https://graph.facebook.com/v18.0/{sender}/messages"
if link:
data = {
"messaging_product": "whatsapp",
Expand All @@ -257,7 +354,7 @@ def send_document(
}

logging.info(f"Sending document to {recipient_id}")
r = requests.post(self.url, headers=self.headers, json=data)
r = requests.post(url, headers=self.headers, json=data)
if r.status_code == 200:
logging.info(f"Document sent to {recipient_id}")
return r.json()
Expand Down
40 changes: 36 additions & 4 deletions whatsapp/ext/_send_others.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging


def send_custom_json(self, data: dict, recipient_id: str = ""):
def send_custom_json(self, data: dict, recipient_id: str = "", sender= None):
"""
Sends a custom json to a WhatsApp user. This can be used to send custom objects to the message endpoint.
Expand All @@ -18,6 +18,22 @@ def send_custom_json(self, data: dict, recipient_id: str = ""):
"type": "audio",
"audio": {"id": audio}}, "5511999999999")
"""
try:
sender = dict(self.l)[sender]
print(self.l)
print(sender)


print(12)

except:
print(22)
sender = self.phone_number_id

if sender == None:
sender = self.phone_number_id

url = f"https://graph.facebook.com/v18.0/{sender}/messages"

if recipient_id:
if "to" in data.keys():
Expand All @@ -28,7 +44,7 @@ def send_custom_json(self, data: dict, recipient_id: str = ""):
data["to"] = recipient_id

logging.info(f"Sending custom json to {recipient_id}")
r = requests.post(self.url, headers=self.headers, json=data)
r = requests.post(url, headers=self.headers, json=data)
if r.status_code == 200:
logging.info(f"Custom json sent to {recipient_id}")
return r.json()
Expand All @@ -39,7 +55,7 @@ def send_custom_json(self, data: dict, recipient_id: str = ""):


def send_contacts(
self, contacts: List[Dict[Any, Any]], recipient_id: str
self, contacts: List[Dict[Any, Any]], recipient_id: str, sender=None
) -> Dict[Any, Any]:
"""send_contacts
Expand Down Expand Up @@ -68,6 +84,22 @@ def send_contacts(
REFERENCE: https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#contacts-object
"""
try:
sender = dict(self.l)[sender]
print(self.l)
print(sender)


print(12)

except:
print(22)
sender = self.phone_number_id

if sender == None:
sender = self.phone_number_id

url = f"https://graph.facebook.com/v18.0/{sender}/messages"

data = {
"messaging_product": "whatsapp",
Expand All @@ -76,7 +108,7 @@ def send_contacts(
"contacts": contacts,
}
logging.info(f"Sending contacts to {recipient_id}")
r = requests.post(self.url, headers=self.headers, json=data)
r = requests.post(url, headers=self.headers, json=data)
if r.status_code == 200:
logging.info(f"Contacts sent to {recipient_id}")
return r.json()
Expand Down

0 comments on commit 32796e9

Please sign in to comment.