diff --git a/docs/WhatsApp Groups Automation/Sending Polls To WhatsApp Groups.md b/docs/WhatsApp Groups Automation/Sending Polls To WhatsApp Groups.md new file mode 100644 index 000000000..962780ee2 --- /dev/null +++ b/docs/WhatsApp Groups Automation/Sending Polls To WhatsApp Groups.md @@ -0,0 +1,116 @@ +### **5 minute read                                                                                                                         `Beginner`** + +## Sending Polls to WhatsApp Group Collections. + +Glific platform can be used to create polls. Send or scheudule the polls in advance to multiple groups. + +### Pre-requisites +1. WhatsApp Groups Integration should be enabled. [Read here](https://glific.github.io/docs/docs/WhatsApp%20Groups%20Automation/Setting%20up%20WhatsApp%20Groups%20Automation%20for%20existing%20NGOs) + +### Create the poll in Glific platform + +1. Go to the `WhatsApp Polls` under the `WhatsApp Groups` section + +2. Click on `Create` and provide the title, the question of the poll in content and the options. Ensure you make the right choice for allowing multiple responses. +3. Poll questions are limited to 255 characters. +4. Under Options enter up to 12 poll options. +5. Poll options are limited to 100 characters. +6. To allow users voting for more than one option, turn on `Allow multiple answers` + Screenshot 2025-02-14 at 2 51 54 PM + +8. Click on `Save`. This creates the poll. +9. Copy the UUID +Screenshot 2025-02-14 at 2 51 07 PM + +### Create a flow to send the poll +1. Create a new flow +2. Use `Call a webhook` node. Select `Function` from the dropdown and type the following function name to send the poll `send_wa_group_poll` + +Screenshot 2025-02-14 at 2 49 58 PM + +Go to Function `FUNCTION body` and pass the following parameters +`{ + "wa_group": "@wa_group", + "poll_uuid": "" +}` +Screenshot 2025-02-14 at 2 50 43 PM + +- “poll_uuid”: "" from the polls list paging by selecting on `Copy UUID`. +- “wa_group”: “@wa_group” will remain as is. + +3. This completes the flow +Screenshot 2025-02-14 at 4 08 36 PM + +**4. Please note: No further `send message node` is needed to send the poll to WhatsApp group. The webhook success response sends the poll with the given poll uuid to the groups.** + +Here is a [SAMPLE FLOW](https://drive.google.com/file/d/13CyRD8Gmq0J1J_lICrwIq55TBbwHND90/view?usp=sharing). To use this flow, go to the link, download the csv, and upload this flow using `import a flow` in your Glific instance. Change the `poll_uuid` in the webhook function body to match the `poll_uuid` of the poll you have created in your Glific instace. + +### Sending the poll to required WhatsApp Group or Collection. + +1. This can be done by setting a [trigger to a WhatsApp Group Collection](https://glific.github.io/docs/docs/WhatsApp%20Groups%20Automation/WhatsApp%20Groups%20Automation%20Features#scheduling-messages-and-media) +2. Or the particular flow can be started for the WhatsApp Group by starting a flow for the group like shown below. +Screenshot 2025-02-14 at 4 15 51 PM + + +### Polls can also be used from Google sheet, to send different polls based on the date or any other incremental counter logic. + +1. [Link a readable Google Sheet](https://glific.github.io/docs/docs/WhatsApp%20Groups%20Automation/WhatsApp%20Groups%20Automation%20Features#scheduling-messages-and-media) in the Glific, which has the Key and poll uuids + Screenshot 2025-02-14 at 4 17 07 PM + +3. Use Link a google sheet node to read the poll uuid from the sheet based on your choice of variables (could be date wise etc). Please note that above is just one example of how it can be used. +4. Call the webhook function `send_wa_group_poll` and use the sheet result to populate the `poll_uuid` in the function body of the webhook call. +Screenshot 2025-02-14 at 4 17 46 PM +5. This flow picture is only to illustrate how google sheet can be used to send polls. + +### Getting the data of responses + +1. Responses to the polls are stored in the `wa_messages` tables +2. The `type` field stores the type of message being sent or received. Filtering for `poll` under this field gives all the poll type text messages sent from the number to the groups +3. `poll_content` field contains the json structure for the poll. This contains the entire poll body, options and the vote count. +4. At present it is possible to get only the no of responses to a particular option, and not which phone number has responded what. +5. Following query creates a view to get the aggregated output of all the polls and its responses. This can be further manipulated to get the responses for a particular poll across the various groups or drill down to responses from a particular group. (replace the xxxxxxxxxx with the table dataset from your chatbot. It is usually the phone number of your chatbot) + +``` + WITH distinct_data AS ( + SELECT + id, + wa_group_id, + wa_group_name, + MAX(body) AS body, + MAX(poll_content) AS poll_content, + MAX(inserted_at) as inserted_at + FROM + `tides-saas-309509.917834811114.wa_messages` + GROUP BY id, wa_group_id, wa_group_name + ), + parsed_data AS ( + SELECT + id, + body, + wa_group_id, + wa_group_name, + JSON_EXTRACT_ARRAY(poll_content, '$.options') AS options, + inserted_at + FROM + distinct_data + ) + + SELECT + wa_group_id, + wa_group_name, + body, + JSON_EXTRACT_SCALAR(option, '$.name') AS option_name, + SUM(CAST(JSON_EXTRACT_SCALAR(option, '$.votes') AS INT64)) AS votes, + MAX(inserted_at) as inserted_at + FROM + parsed_data, + UNNEST(options) AS option + GROUP BY body, option_name, wa_group_id, wa_group_name +``` + +**Points to note: ** +1. While creating the poll, please populate the options you want to them to appear in when being read on WhatsApp +2. Poll once created cannot be edited, please review and test thoroughly before using it in actual groups. + + +