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();

Ranjith kumar
5 4 votes
Article Rating
Subscribe
Notify of
guest

35 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
mahmoudi noureddine
mahmoudi noureddine
2 years ago

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

Daulat
Daulat
2 years ago

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

Anonymous
Anonymous
2 years ago

what if each list row has a different answer?

Firman
Firman
2 years ago

halo

muraly
2 years ago

its good tutorial.

ashley samuel
2 years 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 years ago
Reply to  Ranjith kumar

thanks so much. i will be waiitng

emi
emi
2 years ago
Hi, I can't get the example to work.
Neither with lists nor with buttons.
Any ideas?
Thank you
Nagaraja
Nagaraja
1 year ago
Reply to  Ranjith kumar

Hi,
I am also getting same issue without any error. What could be the reason?

Nagaraja
Nagaraja
1 year ago
Reply to  emi

Hi,
Did you got any solution for this?

eliya eldar
eliya eldar
1 year ago

it does not work for me. I work with buttons using whatsapp-web.js and if I try to send more than three buttons only the first three are sent. So I tried to work with the code you presented here, but it just doesn’t do anything. Not even an error.

raj
raj
1 year ago
Reply to  eliya eldar

hey did you find any fix for this?

Dor
Dor
1 year ago

Hi, first thank you for the detailed explanation,
I have a problem that the message was sent but not delivered.
any idea why?

Oliver Choi
Oliver Choi
1 year ago

Hello, I used your code and it works, but I modified it so that when someone sends me a message, I will send back an option list to them. This works unilaterally, and the sender cannot see the option list, but the receiver receives the option list. Am I missing some steps?

Oliver Choi
Oliver Choi
1 year ago
Reply to  Ranjith kumar

Thank you for your reply. So this is impossible to achieve like a bot. When the sender gives some instructions, can I send back the options list for the sender to choose from according to the instructions?

Oliver Choi
Oliver Choi
1 year ago
Reply to  Ranjith kumar

I want to create a WhatsApp chatbot. It will receive messages from the sender. If it is a special command, it will send an options list to the sender. The sender can select from the options and send their selection back to the chatbot. However, I do not want to use a WhatsApp business account.

azam sajid
azam sajid
1 year ago

can we get security code on mobile number rather than scanning form mobile qr code?

Teboho Mosuhli
Teboho Mosuhli
1 year ago

Hey!
Can you please help me on connecting the bot with really number?

Teboho Mosuhli
Teboho Mosuhli
1 year ago

Hey!
I tried to use the same code but on my site it seems not working. What can be the issue?

marcelo
marcelo
1 year ago

hi, can u help me?
let suppose im talking with one of my clients and i wanna send information about my product.
It is possible to send information with my command?
So when i type !info send information about my product to this client.

Nagaraja
Nagaraja
1 year ago

Hey!
I tried to use the same code but on my site it seems not working. What can be the issue?
I am using “whatsapp-web.js”: “^1.22.1”


faishol
faishol
1 year ago

hi man, thankyou for the tutorial, but i cant get the list

e04d99a0-77b6-408a-be8b-64cc1bc15e0f.jpg
adam smith
adam smith
1 year ago

i need to automate files[pdf,word,etc.] with the help of whatsapp-web-js,i want i put path of the file in excel with respective phone number and script should sent the media accordingly. kindly help me if you can achieving this . thanks:)

Rudraksh
Rudraksh
10 months ago

Hi,
I have the exact code, however, sending the list is not doing anything and there’s no error generated as well. So, it’s not working.