Send WhatsApp Messages from Google Sheets using the WhatsApp API

203 views
Skip to first unread message

Vivek Shahi

unread,
Mar 6, 2023, 3:24:38 AM3/6/23
to Google Apps Script Community
Dear All, I am facing some errors during this process; please help me.

 error.png

Brett Grear

unread,
Mar 6, 2023, 7:19:41 AM3/6/23
to Google Apps Script Community
Your screenshot tells you what line the error is on but you've not shown us that part of your code 

Vivek Shahi

unread,
Mar 6, 2023, 7:40:47 AM3/6/23
to google-apps-sc...@googlegroups.com
image.png
image.png

image.png

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/BCVsPWw4McY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/5b3e15d5-c374-4b84-ad14-342cb1fa8c1cn%40googlegroups.com.

Vivek Shahi

unread,
Mar 13, 2023, 5:45:48 AM3/13/23
to Google Apps Script Community
This is the code please help me

const WHATSAPP_ACCESS_TOKEN = "EAAHEuqo9pXUBANzrGgoXGaZAHAcCgI7ZAoKZAH19aVMBZBiX85iWqcxk5M2EGHK9Ooukb5jh8q4duEKaLJFIbx3fp84ZCD14J81ipb7ejwsAasLJ3ZB56zPBSadGt8hI9PA6vkPJwClXirS1Vhg1dae7Lr7XqseuQPdNyNwmLiUazvJxY5Tp67o1jPWgqZAJuW9z6y4XFEcOEbUiNZAZCvSeI9LDimcwUsrIZD";
const WHATSAPP_TEMPLATE_NAME = "mbautoworld";
const LANGUAGE_CODE = "en";

const sendMessage_ = ({
  recipient_number,
  customer_name,
  item_name,
  delivery_date,
}) => {
  const request = UrlFetchApp.fetch(apiUrl, {
    muteHttpExceptionstrue,
    method"POST",
    headers: {
      Authorization`Bearer ${WHATSAPP_ACCESS_TOKEN}`,
      "Content-Type""application/json",
    },
    "payload"JSON.stringify({
      "messaging_product""whatsapp",
      "type""template",
      "to""recipient_number",
      "template": {
        "name""mbautoworld",
        "language": { "code""en" },
        "components": [
          {
            "type""body",
            "parameters": [
              {
                "type"'text',
                "text""customer_name",
              },
              {
                "type"'text',
                "text""item_name",
              },
              {
                "type"'text',
                "text""delivery_date",
              },
            ],
          },
        ],
      },
    }),
  });

  const { error } = JSON.parse(request);
  const status = error ? `Error: ${JSON.stringify(error)}` : `Message sent to ${recipient_number}`;
  Logger.log(status);
};

const getSheetData_ = () => {
  const [header, ...rows] = SpreadsheetApp.getActiveSheet().getDataRange().getDisplayValues();
  const data = [];
  rows.forEach((row) => {
    const recipient = { };
    header.forEach((titlecolumn) => {
      recipient[title] = row[column];
    });
    data.push(recipient);
  });
  return data;
};

const main = () => {
  const data = getSheetData_();
  data.forEach((recipient) => {
      const status = sendMessage_({
        recipient_numberrecipient["Phone Number"].replace(/[^\d]/g""),
        customer_namerecipient["Customer Name"],
        item_namerecipient["Item Name"],
        delivery_daterecipient["Delivery Date"],
      });
  });
};

Reply all
Reply to author
Forward
0 new messages