Click here to Skip to main content
16,022,205 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how do i send message from google sheet to whatsapp directly? i am using this script but message not sent no error message display

function sendWhatsAppMessage() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const data = sheet.getDataRange().getValues();
  const today = new Date();

  for (let i = 1; i < data.length; i++) {
    const workerName = data[i][0];
    const mobileNumber = data[i][1];
    const task = data[i][2];
    const taskDate = new Date(data[i][3]);

    // Calculate the date one day before the task date
    const reminderDate = new Date(taskDate);
    reminderDate.setDate(reminderDate.getDate() - 1);

    if (today.getTime() === reminderDate.getTime()) {
      // Send WhatsApp message using Maytapi

      var MAYTAPI_PRODUCT_ID = '3xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
      var MAYTAPI_TOKEN = 'f1xxxx-xxxx-xxxx-xxxxxxxxxxxx';
      var MAYTAPI_PHONE_ID = '5xxxx';



      const maytapiUrl = 'https://api.maytapi.com/api/sendMessage'+ MAYTAPI_PRODUCT_ID + "/" + String(MAYTAPI_PHONE_ID) + "/sendMessage?token=" + MAYTAPI_TOKEN;
      const payload = {
        product_id: '3xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
        token: 'f1xxxx-xxxx-xxxx-xxxxxxxxxxxx',
        phone: mobileNumber,
        text: `Reminder for ${workerName}: ${task}`,
      };

      // Make an HTTP POST request to Maytapi
      const options = {
        method: 'post',
        contentType: 'application/json',
        payload: JSON.stringify(payload),
      };

      const response = UrlFetchApp.fetch(maytapiUrl, options);
      Logger.log(`Message sent to ${workerName}: ${response.getContentText()}`);
    }
  }
}


What I have tried:

how do i send message from google sheet to whatsapp directly? i am using this script but message not sent no error message display

function sendWhatsAppMessage() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const data = sheet.getDataRange().getValues();
  const today = new Date();

  for (let i = 1; i < data.length; i++) {
    const workerName = data[i][0];
    const mobileNumber = data[i][1];
    const task = data[i][2];
    const taskDate = new Date(data[i][3]);

    // Calculate the date one day before the task date
    const reminderDate = new Date(taskDate);
    reminderDate.setDate(reminderDate.getDate() - 1);

    if (today.getTime() === reminderDate.getTime()) {
      // Send WhatsApp message using Maytapi

      var MAYTAPI_PRODUCT_ID = '3xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
      var MAYTAPI_TOKEN = 'f1xxxx-xxxx-xxxx-xxxxxxxxxxxx';
      var MAYTAPI_PHONE_ID = '5xxxx';



      const maytapiUrl = 'https://api.maytapi.com/api/sendMessage'+ MAYTAPI_PRODUCT_ID + "/" + String(MAYTAPI_PHONE_ID) + "/sendMessage?token=" + MAYTAPI_TOKEN;
      const payload = {
        product_id: '3xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
        token: 'f1xxxx-xxxx-xxxx-xxxxxxxxxxxx',
        phone: mobileNumber,
        text: `Reminder for ${workerName}: ${task}`,
      };

      // Make an HTTP POST request to Maytapi
      const options = {
        method: 'post',
        contentType: 'application/json',
        payload: JSON.stringify(payload),
      };

      const response = UrlFetchApp.fetch(maytapiUrl, options);
      Logger.log(`Message sent to ${workerName}: ${response.getContentText()}`);
    }
  }
}
Posted
Updated 18-Jul-24 6:41am
v2
Comments
Richard Deeming 16-Jul-24 6:39am    
You'll need to contact whoever runs the maytapi [DOT] com service to find out what the problem is.

NB: It's a commercial product, so they should provide you with support. If they don't, then take your money elsewhere.
[no name] 19-Jul-24 5:11am    
My friends and I need a lot of time to study the inside heardle unlimited.

1 solution

You are using a function, getting, which returns the number of milliseconds since the epoch (1970-01-01). The chances of you getting the exact same millisecond in both calls is extremely low. Change the approach you use to check the time to something more rational, and allow for the timings to be a little bit inexact.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900