AUTOMATE SCRIPT

Send WhatsApp Messages from Google Sheets using Google Apps Script

This post shows you How to Send WhatsApp Messages from Google Sheets using Google Apps Script.
I am using the WhatsMate WA Gateway service to send WhatsApp messages. we will need to register with the WhatsMate WA Gateway.

Send WhatsApp Messages from Google Sheets with Google Apps Script, do this:

Send WhatsApp Messages from Google Sheets

Send WhatsApp Messages from Google Sheets using Google Apps Script

In the sheet where we add manually contact details and Messages. column C is the sender’s Whatsapp mobile number

  • We can add multiple rows means multiple messages in the google sheet
  • We must have to add the mobile no (Column C) and message (Column E)

Apps Script: Send WhatsApp Messages from Google Sheets

In the sheet menu, tools > script editor. The script editor will open and Paste the code below in the script editor. After pasting the code please refresh the sheet.
You will see the custom menu, run that menu then authorize the code, and after authorization Whatsapp message will be sent

				
					function onOpen( ){
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Menu')
      .addItem('Send Whatsapp Message', 'myFunction')
      .addSeparator()
      .addToUi();
}
function myFunction() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process - 2 exempts my header row
  var numRows = sheet.getLastRow();   // Number of rows to process
  var numColumns = sheet.getLastColumn();
  var dataRange = sheet.getRange(startRow, 1, numRows-1, numColumns); 
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var fName =     row[0]; 
    var lName =     row[1];  
    var destNumber =  row[2]; 
    var message =      row[4];
    sendWhatsapp(destNumber, message); 
  }
}

function sendWhatsapp(destNumber, message) {
  var instanceId = "YOUR_INSTANCE_ID_HERE";  // TODO: Replace it with your gateway instance ID here
  var clientId = "YOUR_CLIENT_ID_HERE";  // TODO: Replace it with your Forever Green client ID here
  var clientSecret = "YOUR_CLIENT_SECRET_HERE";   // TODO: Replace it with your Forever Green client secret here
  
  var jsonPayload = JSON.stringify({
    number: destNumber,
    message: message
  });
  
  var options = {
    "method" : "post",
    "contentType": "application/json",
    "headers": {
      "X-WM-CLIENT-ID": clientId,
      "X-WM-CLIENT-SECRET": clientSecret
    },
    "payload" : jsonPayload,
    "Content-Length": jsonPayload.length
  };
    
  UrlFetchApp.fetch("http://api.whatsmate.net/v3/whatsapp/single/text/message/" + instanceId, options);
}