This article shows you how to send a document (e.g. a PDF file, an MP4 file, a WAV file, etc.) to a registered WhatsApp user in Node.js (JavaScript).
Before the recipient can receive your WhatsApp message, she MUST register with the WhatsMate WA Gateway. Instructions are available on the official site. Unregistered users will never receive messages from the Gateway.
To send a WhatsApp message containing a PDF file in Node.js
, 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
nodejs/send-pdf-individual.js
.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 node var http = require('http'); var fs = require('fs'); // Put down your own client ID and secret here: var instanceId = "YOUR_OWN_GATEWAY_INSTANCE_ID"; var clientId = "YOUR_OWN_CLIENT_ID"; var clientSecret = "YOUR_OWN_SECRET_ID"; var jsonPayload = JSON.stringify({ number: "12025550108", // FIXME document: fs.readFileSync("../assets/subwaymap.pdf").toString('base64'), // FIXME filename: "anyname.pdf", // FIXME caption: "Hope you like it" // FIXME. caption is optional. Can be null. }); var options = { hostname: "api.whatsmate.net", port: 80, path: "/v3/whatsapp/single/document/message/" + instanceId, method: "POST", headers: { "Content-Type": "application/json", "X-WM-CLIENT-ID": clientId, "X-WM-CLIENT-SECRET": clientSecret, "Content-Length": Buffer.byteLength(jsonPayload) } }; var request = new http.ClientRequest(options); request.end(jsonPayload); request.on('response', function (response) { console.log('Heard back from WhatsMate WhatsApp Gateway:\n'); console.log('Status code: ' + response.statusCode); response.setEncoding('utf8'); response.on('data', function (chunk) { console.log(chunk); }); }); - Study the
Node.js
source code and customize the TODO/FIXME lines.- Among other things, put down YOUR OWN number as the recipient. You can’t send messages to strangers because we are against SPAMMERS.
- Change to the directory containing the script:
cd nodejs
- Run the script to send yourself the first image:
./send-pdf-individual.js
Happy coding :)