Skip to content

Instantly share code, notes, and snippets.

@whatsmate
Last active April 15, 2024 09:29
Show Gist options
  • Save whatsmate/dd8a28d584bc9e8155f42b85e837702a to your computer and use it in GitHub Desktop.
Save whatsmate/dd8a28d584bc9e8155f42b85e837702a to your computer and use it in GitHub Desktop.
Sending a document to a registered WhatsApp user in Python 3
#!/usr/bin/env python3
import base64
import requests
# TODO: When you have your own Client ID and secret, put down their values here:
instanceId = "YOUR_GATEWAY_INSTANCE_ID_HERE"
clientId = "YOUR_OWN_ID_HERE"
clientSecret = "YOUR_OWN_SECRET_HERE"
# TODO: Customize the following 4 lines
number = '12025550108' # FIXME
fullpath_to_document = "../assets/subwaymap.pdf"
fn = "anyname.pdf"
caption = "You will find it useful." # caption is optional; can be None
# Encode the document in base64 format
doc_base64 = None
with open(fullpath_to_document, 'rb') as doc:
doc_base64 = base64.b64encode(doc.read())
headers = {
'X-WM-CLIENT-ID': clientId,
'X-WM-CLIENT-SECRET': clientSecret
}
jsonBody = {
'number': number,
'document': doc_base64.decode("utf-8"),
'filename': fn,
'caption': caption
}
r = requests.post("http://api.whatsmate.net/v3/whatsapp/single/document/message/%s" % instanceId,
headers=headers,
json=jsonBody)
print("Status code: " + str(r.status_code))
print("RESPONSE : " + str(r.content))
@whatsmate
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment