Skip to content

Commit

Permalink
added <send_contacts()> method
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalebu committed Aug 30, 2022
1 parent dc9d1e8 commit 26df1b0
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions heyoo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import requests
import logging
from requests_toolbelt.multipart.encoder import MultipartEncoder
from typing import Optional, Dict, Any, List, Union, Tuple, Callable


# Setup logging
Expand Down Expand Up @@ -351,6 +352,51 @@ def send_document(self, document, recipient_id, caption=None, link=True):
logging.error(f"Response: {r.json()}")
return r.json()

def send_contacts(self, contacts: List[Dict[Any, Any]], recipient_id: str):
"""send_contacts
Send a list of contacts to a user
Args:
contacts(List[Dict[Any, Any]]): List of contacts to send
recipient_id(str): Phone number of the user with country code wihout +
Example:
>>> from whatsapp import WhatsApp
>>> whatsapp = WhatsApp(token, phone_number_id)
>>> contacts = [{
"addresses": [{
"street": "STREET",
"city": "CITY",
"state": "STATE",
"zip": "ZIP",
"country": "COUNTRY",
"country_code": "COUNTRY_CODE",
"type": "HOME"
},
....
}
]
REFERENCE: https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#contacts-object
"""

data = {
"messaging_product": "whatsapp",
"to": recipient_id,
"type": "contacts",
"contacts": contacts,
}
logging.info(f"Sending contacts to {recipient_id}")
r = requests.post(self.url, headers=self.headers, json=data)
if r.status_code == 200:
logging.info(f"Contacts sent to {recipient_id}")
return r.json()
logging.info(f"Contacts not sent to {recipient_id}")
logging.info(f"Status code: {r.status_code}")
logging.error(f"Response: {r.json()}")
return r.json()

def upload_media(self, media: str):
"""
Uploads a media to the cloud api and returns the id of the media
Expand Down

0 comments on commit 26df1b0

Please sign in to comment.