Skip to content

Backend API Endpoints

harishoke edited this page Jun 1, 2025 · 1 revision

PollMasters Backend API Endpoints

The Node.js backend (server.js) exposes several HTTP API endpoints that the Python frontend uses for various operations. The server typically runs on http://localhost:3000.

  • GET /status

    • Description: Returns the current connection status of the Baileys WhatsApp client.
    • Response: JSON object containing:
      • status: String - e.g., "ready", "qr_pending", "disconnected", "logged_out".
      • qrCode: String (Optional) - The QR code data string if status is "qr_pending".
      • user: Object (Optional) - Information about the logged-in WhatsApp user if status is "ready".
  • POST /send-poll

    • Description: Sends a poll to a specified WhatsApp chat.
    • Request Body (JSON):
      {
        "chatId": "string (whatsapp_jid)",
        "question": "string",
        "options": ["string", "string", ...],
        "allowMultipleAnswers": boolean 
      }
      (Note: allowMultipleAnswers: true translates to selectableCount: 0 in Baileys, false translates to selectableCount: 1).
    • Response (JSON):
      • Success: { "success": true, "message": "Poll sent successfully!", "pollMsgId": "string" }
      • Failure: { "success": false, "message": "Error description", "error": "Optional error details" }
  • GET /get-chats

    • Description: Fetches a list of the connected WhatsApp account's chats and groups.
    • Response (JSON):
      • Success: { "success": true, "chats": [{"id": "string", "name": "string", "isGroup": boolean}, ...] }
      • Failure: { "success": false, "message": "Error description", "error": "Optional error details" }
  • POST /logout

    • Description: Logs out the current WhatsApp session from Baileys and attempts to delete the local session authentication files.
    • Response (JSON):
      • Success: { "success": true, "message": "Logged out and local session cleared..." }
      • Failure: { "success": false, "message": "Error description" }
  • GET /get-all-poll-data

    • Description: Fetches data for all polls currently tracked as active by the server for the current session.
    • Response (JSON):
      • Success: { "success": true, "polls": { "pollMsgId_1": { poll_data_object_1 }, ... } }
      • The poll_data_object structure includes question, options, optionHashes, results, voters, chatId, timestamp, selectableCount.

Socket.IO Events (Backend to Frontend):

The backend also communicates with the frontend using these Socket.IO events:

  • qr_code (qrData): Sends new QR code data.
  • client_status (statusString): Sends updates on WhatsApp client connection status (e.g., "ready", "disconnected", "qr_pending", "logged_out").
  • whatsapp_user (userDataObject): Sends basic info about the logged-in WhatsApp user.
  • initial_poll_data (allPollsObject): Sends all currently tracked poll data when a GUI client connects.
  • new_poll_sent ({ pollMsgId, pollData }): Informs the GUI that a new poll has been successfully sent by the backend.
  • poll_update_to_gui ({ pollMsgId, results, question, options, voters, selectableCount }): Sends real-time updates for a specific poll when new votes are received.

Clone this wiki locally