How could I get voters from a poll?

Hi team,

I am having some issue with GET to /polls/voters.json (I need all userNames or IDs that voted each option)
Discourse is always returning me this error:

{
“errors”: [
“You supplied invalid parameters to the request: poll_name”
],
“error_type”: “invalid_parameters”
}

post_id and poll_name are OK because vote and unvote endpoints works fine. It’s just this one :frowning:

Dumb question, but don’t you see an ‘Export’ button on the poll itself?

Running the export ought to give you the API call (via your browser’s developer tools)…

6 Likes

Hi @ganncamp, I am not seeing that export button. Maybe should I configure this from server? I didn’t find any config about this.

I found an endpoint that should be returning this info but It’s not working - discourse/polls_controller.rb at main · discourse/discourse · GitHub

What do you get from “Show breakdown”?

1 Like

The Export button shows if you have the Discourse Data Explorer plugin installed and working. There are plans to unbundle this eventually, but at the moment there is a dependency.

8 Likes

Thanks everyone for the help! I dont want to install that plugin. Maybe, I will find a workaround with vote and un-vote or something like that :frowning:

Trying to reverse engineer the Export button to get poll results, but always get a 422 response.

url
'https://forum.example.com/admin/plugins/explorer/queries/-16/run.csv'
params
{'post_id': 82022, 'poll_name': 'poll2'}
headers
{'Api-Key': 'redacted', 'Api-Username': 'discobot'}
requests.post(url, params=params, headers=headers)
422

Query 16 is the built-in poll report. I’ve allowed admins to access this query. @discobot is an admin.
image

This username and API key work for all other queries. This is my only Data Explorer-based query (which seems like it would be a really powerful tool, BTW).

Here’s a working solution to get what Export poll gets.

import requests
from io import StringIO
import csv
from urllib.parse import urlencode

def get_discourse_poll_results(post_id, poll_name):
    """
    Retrieves poll results from Discourse using Data Explorer's query 16,
    which returns the poll results in CSV format.
    """

    url = f"{DISCOURSE_URL}/admin/plugins/explorer/queries/-16/run.csv"

    payload = {
        "params": f'{{"poll_name":"{poll_name}","post_id":"{post_id}"}}',
        "explain": "false",
        "limit": "1000000",
        "download": "1",
    }
    headers = {
        "Api-Key": DISCOURSE_API_KEY,
        "Api-Username": DISCOURSE_API_USERNAME,
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "X-Requested-With": "XMLHttpRequest"
    }

    try:
        response = requests.post(url, data=urlencode(payload), headers=headers)
        response.raise_for_status()

        csv_data = StringIO(response.text)
        reader = csv.reader(csv_data)
        results = list(reader)

        processed_results = process_poll_results(results)
        reformatted_results = reformat_rating_poll(processed_results)
        return reformatted_results

    except requests.exceptions.RequestException as e:
        print(f"Error retrieving poll results: {e}")
        print(f"Response text: {response.text}")
        return None
2 Likes