How to send a list of options and get the user response in WhatsApp API

I made a video explaining how to do Whatsapp automation using Javascript. Someone has asked this question in the comments of that video. Let’s find out!

Here’s the example source code to send a list of products in which users can select one product. After selecting a product, an automatic message will be sent with the message “You’ve select X product”

Demo of sending a list of selectable options using Whatsapp API
Demo of sending a list of selectable options using Whatsapp API

I’m sending the list to a group named “Source Group” in this example. You can change the group name or modify the code to send it to a contact based on your requirement.

const { Client, LocalAuth, Buttons, List } = require("whatsapp-web.js");
const qrcode = require("qrcode-terminal");

const myGroupName = "Source Group";

const client = new Client({
  authStrategy: new LocalAuth(),
});

client.on("qr", (qr) => {
  qrcode.generate(qr, { small: true });
});

client.on("ready", () => {
  console.log("Client is ready!");
  client.getChats().then((chats) => {
    myGroup = chats.find((chat) => chat.name === myGroupName);

    const productsList = new List(
      "Here's our list of products at 50% off",
      "View all products",
      [
        {
          title: "Products list",
          rows: [
            { id: "apple", title: "Apple" },
            { id: "mango", title: "Mango" },
            { id: "banana", title: "Banana" },
          ],
        },
      ],
      "Please select a product"
    );
    client.sendMessage(myGroup.id._serialized, productsList);
  });
});

client.on("message", (message) => {
  if(message.type === 'list_response'){
    message.reply(`You've selected ${message.body}`);
  }
});

client.initialize();

5 2 votes
Article Rating
Subscribe
Notify of
guest
11 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
mahmoudi noureddine
mahmoudi noureddine
7 months ago

how to integrate With Google Sheet !! thank you for your Help

Daulat
Daulat
6 months ago

Hi,
Can you please, Help us to show products in whatsApp

Anonymous
Anonymous
4 months ago

what if each list row has a different answer?

Firman
Firman
2 months ago

halo

muraly
2 months ago

its good tutorial.

ashley samuel
2 months ago

i want to thank you for the detailed tutorial on how to use the whatsapp api. could you also guide us with an example on how to use products,add to cart and orders.? hope to hear from you soon

ashley samuel
2 months ago
Reply to  Ranjith kumar

thanks so much. i will be waiitng