This article shows you how to send a document (e.g. a PDF file, an MP4 file, a WAV file, etc.) to a WhatsApp group in Python.
To send a PDF document to a WhatsApp group in Python, do this:
- First, learn how to send a simple text message on the official site.
- With the above knowledge, you can now download our sample code.
- Locate the file
python/send-pdf-group.py
.This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters#!/usr/bin/env python 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 3 lines group = 'YOUR UNIQUE GROUP NAME HERE' # TODO Specify your unique group name here 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 = { 'group_name ': group, 'document': doc_base64, 'filename': fn, 'caption': caption } r = requests.post("http://api.whatsmate.net/v3/whatsapp/group/document/message/%s" % instanceId, headers=headers, json=jsonBody) print("Status code: " + str(r.status_code)) print("RESPONSE : " + str(r.content)) - Study the Python source code and customize the TODO/FIXME lines.
- Among other things, you must name your group creatively. Otherwise, the group may not receive the message.
- Change to the directory containing the script:
cd python
- Run the Python script to send yourself the first PDF file:
./send-pdf-group.py
Happy coding :)