Automate sending polls via WhatsApp

Author

Andrea Zonca

Published

April 25, 2024

Polls in WhatsApp are so convenient for the users, but so painful for group admins. Cannot modify them, cannot re-use or replicate them.

Best workaround I found is to use WPPConnect/WA-JS

This creates a sort of API in the browser console that can be used to just copy-paste from an editor to create a poll.

Now after we login to WhatsApp Web and select the target group chat, we can open the “Developer tools”, paste and execute:

c = WPP.chat.getActiveChat()
WPP.chat.sendCreatePollMessage(
c.id,
'Title of the poll',
['9am', '9:30am', '10am', '2pm', '2:30pm', '3pm', 'No', 'Maybe']
);

This is very convenient especially if you send many polls that have mostly the same options.

You can also generate options dynamically in Javascript:

c = WPP.chat.getActiveChat()
am = ['9am', '10am', '11am']
pm = ['2pm', '2:30pm', '3pm']
day = ['Sat', 'Sun']
other = ['No', 'Maybe']

options = []
for (i = 0; i < day.length; i++) {
    if (day[i] == 'Sun') {
        for (j = 0; j < am.length; j++) {
            options.push(day[i] + ' ' + am[j])
        }
    }
    for (j = 0; j < pm.length; j++) {
        options.push(day[i] + ' ' + pm[j])
    }
}
options = options.concat(other)

if (options.length > 12) {
    throw new Error('Number of options exceeds the limit of 12.');
} else {
    WPP.chat.sendCreatePollMessage(
        c.id,
        'Event title',
        options
    );
}