Table of Contents

How to send a WhatsApp template (HSM) from Google Sheets

Abby Updated by Abby

This guide shows how to send a WhatsApp template message (HSM) to a contact by ticking a checkbox in a Google Sheet. A small Google Apps Script watches the sheet; when someone ticks the box on a row, it calls your bot's WhatsApp opt-in URL with that row's data, and Landbot sends the template. When the contact replies, your bot takes over the conversation.

It's a good fit when your team already keeps contacts in a spreadsheet (leads, candidates, appointments) and wants to start WhatsApp conversations from there without exporting lists or using the API directly. The script also writes a Status on each row, so the team can see what was sent and nothing goes out twice by accident.

Before you start

You'll need:

  • A Landbot WhatsApp bot with an approved message template.
  • That bot's WhatsApp opt-in URL. It looks like https://messages.landbot.io/wa/W-XXXX-XXXXXXXXXXXXXXXX/opt_in and appears in the opt-in snippet for your WhatsApp channel.
  • A Google Sheet you can edit, and a Google account to run the script (the script sends messages on behalf of that account's trigger).

Step 1: Prepare the sheet

Create a tab (this guide calls it Contacts) with a header row and these columns:

Column

Header

Content

A

Name

Contact's name

B

Phone

Number with country code

C

Send?

A checkbox (Insert → Checkbox)

D

Status

Left empty; the script fills it

E

Chat

Left empty; the script adds an "Open chat" link to the conversation in Landbot

You can add more columns for other data you want in the message, such as a company or an appointment date. You'll map each one in the script.

Step 2: Map the template variables in the opt-in

When the script calls the opt-in URL, every query parameter is stored in the Landbot variable with the same name. For example, ?name=Ana&phone=525512345678 sets @name and @phone. Your opt-in configuration then decides which variables fill the template's placeholders.

Step 3: Add the script

  1. Go to script.google.com and create a New project. You only need edit access to the sheet; the script doesn't have to live inside it.
  2. Replace the default code with the script below.
  3. Fill in the settings at the top: the sheet ID (the long string between /d/ and /edit in the sheet's URL), the tab name, your opt-in URL, and the column numbers if your layout differs.
  4. In FIELDS, add one entry per value you want to send. The key is the parameter name Landbot receives; the number is the column it's read from. For example, company: 5 sends column E as @company.
// ── Settings ───────────────────────────────────────────────
const SPREADSHEET_ID = 'YOUR_SPREADSHEET_ID'; // the long ID in the sheet URL: /d/<ID>/edit
const SHEET_NAME = 'Contacts';
const OPTIN_URL = 'https://messages.landbot.io/wa/W-XXXX-XXXXXXXXXXXXXXXX/opt_in';
const CHECKBOX_COL = 3; // C Send?
const STATUS_COL = 4; // D Status
const CHAT_COL = 5; // E Chat (0 = don't write the link)
const CHAT_URL = 'https://app.landbot.io/gui/chats_v4/';
// Parameter name sent to Landbot → column number (A = 1, B = 2...)
const FIELDS = { name: 1, phone: 2 };

// Run once from the editor to install the trigger.
function setupTrigger() {
ScriptApp.getProjectTriggers()
.filter(t => t.getHandlerFunction() === 'onCheckboxEdit')
.forEach(t => ScriptApp.deleteTrigger(t));
ScriptApp.newTrigger('onCheckboxEdit').forSpreadsheet(SPREADSHEET_ID).onEdit().create();
}

// Runs on every edit; only acts when the Send? checkbox is ticked.
function onCheckboxEdit(e) {
const sheet = e.range.getSheet();
if (sheet.getName() !== SHEET_NAME) return;
const r = e.range;
if (CHECKBOX_COL < r.getColumn() || CHECKBOX_COL > r.getLastColumn()) return;

const lastCol = Math.max(sheet.getLastColumn(), CHECKBOX_COL, STATUS_COL, CHAT_COL);
for (let row = Math.max(r.getRow(), 2); row <= r.getLastRow(); row++) {
const vals = sheet.getRange(row, 1, 1, lastCol).getValues()[0];
if (vals[CHECKBOX_COL - 1] !== true) continue;
const status = String(vals[STATUS_COL - 1] || '');
if (status && !status.startsWith('Error')) continue; // already sent: don't send twice
sendRow(sheet, row, vals);
}
}

function sendRow(sheet, row, vals) {
const data = {};
for (const [key, col] of Object.entries(FIELDS)) data[key] = String(vals[col - 1] ?? '').trim();
data.phone = data.phone.replace(/\D/g, ''); // digits only, with country code

if (!data.phone) {
sheet.getRange(row, STATUS_COL).setValue('Error: missing phone');
return;
}

const query = Object.entries(data)
.filter(([, v]) => v)
.map(([k, v]) => `${k}=${encodeURIComponent(v)}`)
.join('&');

let status;
try {
const res = UrlFetchApp.fetch(`${OPTIN_URL}?${query}`, { method: 'post', muteHttpExceptions: true });
const code = res.getResponseCode();
const text = res.getContentText();
const ok = code >= 200 && code < 300;
status = ok ? `Sent ${new Date().toLocaleString()}` : `Error ${code}`;
Logger.log(`Row ${row}: ${code} ${text}`);

// The opt-in response includes chat_uuid: write a link to the conversation
if (ok && CHAT_COL) {
try {
const body = JSON.parse(text);
if (body.chat_uuid) {
sheet.getRange(row, CHAT_COL).setFormula(`=HYPERLINK("${CHAT_URL}${body.chat_uuid}","Open chat")`);
}
} catch (err) {
Logger.log(`Row ${row}: could not read chat_uuid (${err})`);
}
}
} catch (err) {
status = `Error ${err}`;
}
sheet.getRange(row, STATUS_COL).setValue(status);
}

Step 4: Install the trigger

  1. Save the project.
  2. In the function menu at the top of the editor, choose setupTrigger and click Run.
  3. Google asks you to authorize the script. If it warns that the app isn't verified, choose Advanced → Go to (project name); that's expected for your own scripts.
  4. Open Triggers (the clock icon on the left) and check that there is exactly one onCheckboxEdit trigger, from spreadsheet, on edit.

You only do this once. Later code changes take effect as soon as you save.

Step 5: Test it

  1. Add a row with your own name and phone number.
  2. Tick Send?. Within a few seconds Status should read "Sent" with the date and time, the Chat column should show Open chat, and the template should arrive on your phone.
  3. Click Open chat: it opens the conversation in Landbot.
  4. Reply to the template and check that your bot continues the conversation.
  5. Untick and tick the box again: nothing should be sent, because Status already shows "Sent".

How it works and tips

  • The trigger fires on every edit in the spreadsheet, but the script exits immediately unless the edit touches the Send? column on the right tab. You'll see these short runs in the project's Executions log; that's normal.
  • Only people's edits fire it. Changes made by another script or by Landbot's Google Sheets block don't, so a bot writing to the sheet never sends a template by accident.
  • No duplicates. A row is only sent when its Status is empty or starts with "Error". To resend on purpose, clear the Status cell and tick the box again.
  • Ticking several rows at once (for example, pasting ticked checkboxes) sends one template per row.
  • Use your own column layout. If you move or insert columns, update CHECKBOX_COL, STATUS_COL and FIELDS to match.
  • Quotas. Apps Script is free and its daily limits are far above typical volumes. Current figures are on Google's Apps Script quotas page.
  • After 24 hours without a reply, WhatsApp only lets you reach the contact again with an approved template, so plan a follow-up template if you need one.

Troubleshooting

Problem

What to check

Nothing happens when I tick the box

The trigger exists (Triggers page); SHEET_NAME matches the tab name exactly; CHECKBOX_COL points to the checkbox column. Look for a run in Executions.

Status says "Error: missing phone"

The phone cell is empty or FIELDS maps phone to the wrong column.

Status says "Error 4xx"

Check the opt-in URL and the phone format (country code, digits only). The full response is in the run's log in Executions.

Status says "Sent" but no message arrives

The number may not have WhatsApp, or the template variables are empty. Check that the names in the opt-in's body params match the keys in FIELDS.

The template arrives twice

There is more than one onCheckboxEdit trigger. Delete the extras in Triggers, or run setupTrigger again.

Was this article helpful?

Recognise the users input when sending a Message Template with buttons

Trigger Event if User Abandons Chat