diff --git a/src/controllers/messageController.js b/src/controllers/messageController.js index 61271a0..cd50c27 100644 --- a/src/controllers/messageController.js +++ b/src/controllers/messageController.js @@ -187,7 +187,6 @@ const downloadMediaAsData = async (req, res) => { * @param {string} req.body.messageId - The ID of the message to forward. * @param {string} req.body.chatId - The ID of the chat that contains the message to forward. * @param {string} req.body.destinationChatId - The ID of the chat to forward the message to. - * @param {string} req.params.sessionId - The ID of the session to use the Telegram API with. * @param {Object} res - The response object to be sent back to the client. * @returns {Object} - The response object with a JSON body containing the result of the forward operation. * @throws Will throw an error if the message is not found or if there is an error during the forward operation. @@ -239,7 +238,6 @@ const forward = async (req, res) => { * @param {Object} req.body - The body of the request object. * @param {string} req.body.messageId - The ID of the message to get information about. * @param {string} req.body.chatId - The ID of the chat that contains the message to get information about. - * @param {string} req.params.sessionId - The ID of the session to use the Telegram API with. * @param {Object} res - The response object to be sent back to the client. * @returns {Object} - The response object with a JSON body containing the information about the message. * @throws Will throw an error if the message is not found or if there is an error during the get info operation. @@ -716,7 +714,7 @@ const edit = async (req, res) => { * @param {string} req.params.sessionId - The session ID. * @param {string} req.body.messageId - The message ID. * @param {string} req.body.chatId - The chat ID. - * @returns {Promise} A Promise that resolves with the result of the message.getReactions() call. + * @returns {Promise} A Promise that resolves with the result of the message.getContact() call. * @throws {Error} If message is not found, it throws an error with the message "Message not found". */ const getContact = async (req, res) => { @@ -735,6 +733,36 @@ const getContact = async (req, res) => { } } +/** + * Gets vote results for a message with a poll. + * @async + * @function getPollVotes + * @param {Object} req - The request object received by the server. + * @param {Object} req.body - The body of the request object. + * @param {string} req.body.messageId - The ID of the message to get information about. + * @param {string} req.body.chatId - The ID of the chat that contains the message to get information about. + * @param {Object} res - The response object to be sent back to the client. + * @returns {Object} - The response object with a JSON body containing the poll results. + * @throws Will throw an error if the message is not found or if there is an error during the get poll votes operation. + */ +const getPollVotes = async (req, res) => { + /* + #swagger.summary = 'Get poll vote results for a message with a poll' + #swagger.description = 'May return null if the message does not exist or is not a poll.' + */ + try { + const { messageId, chatId } = req.body + const client = sessions.get(req.params.sessionId) + const message = await _getMessageById(client, messageId, chatId) + if (!message) { throw new Error('Message not found') } + const votes = await message.getPollVotes() + res.json({ success: true, votes }) + } catch (error) { + sendErrorResponse(res, 500, error.message) + } +} + + /** * Executes a method on the message associated with the given sessionId. * @@ -816,5 +844,6 @@ module.exports = { getGroupMentions, edit, getContact, + getPollVotes, runMethod } diff --git a/src/routes.js b/src/routes.js index a9d228d..a0953d1 100644 --- a/src/routes.js +++ b/src/routes.js @@ -201,6 +201,7 @@ messageRouter.post('/getReactions/:sessionId', [middleware.sessionNameValidation messageRouter.post('/getGroupMentions/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], messageController.getGroupMentions) messageRouter.post('/edit/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], messageController.edit) messageRouter.post('/getContact/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], messageController.getContact) +messageRouter.post('/getPollVotes/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], messageController.getPollVotes) messageRouter.post('/runMethod/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], messageController.runMethod) /** diff --git a/swagger.json b/swagger.json index e677c9a..f051151 100644 --- a/swagger.json +++ b/swagger.json @@ -12017,6 +12017,116 @@ } } }, + "/message/getPollVotes/{sessionId}": { + "post": { + "tags": [ + "Message" + ], + "summary": "Get poll vote results for a message with a poll", + "description": "May return null if the message does not exist or is not a poll.", + "parameters": [ + { + "name": "sessionId", + "in": "path", + "required": true, + "schema": { + "type": "string" + }, + "description": "Unique identifier for the session (alphanumeric and - allowed)", + "example": "f8377d8d-a589-4242-9ba6-9486a04ef80c" + } + ], + "responses": { + "200": { + "description": "OK" + }, + "403": { + "description": "Forbidden.", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ForbiddenResponse" + } + } + } + }, + "404": { + "description": "Not Found.", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/NotFoundResponse" + } + } + } + }, + "422": { + "description": "Unprocessable Entity.", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ErrorResponse" + } + } + } + }, + "500": { + "description": "Server failure.", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ErrorResponse" + } + } + } + } + }, + "security": [ + { + "apiKeyAuth": [] + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "chatId": { + "type": "string", + "description": "The chat id which contains the message", + "example": "6281288888888@c.us" + }, + "messageId": { + "type": "string", + "description": "Unique WhatsApp ID for the message", + "example": "ABCDEF999999999" + } + } + } + }, + "application/xml": { + "schema": { + "type": "object", + "properties": { + "chatId": { + "type": "string", + "description": "The chat id which contains the message", + "example": "6281288888888@c.us" + }, + "messageId": { + "type": "string", + "description": "Unique WhatsApp ID for the message", + "example": "ABCDEF999999999" + } + } + } + } + } + } + } + }, "/message/runMethod/{sessionId}": { "post": { "tags": [