fix: open chat with active poll and add new endpoint

This commit is contained in:
Anton Voylenko 2025-03-15 19:26:59 +02:00
commit 588642e904
4 changed files with 171 additions and 26 deletions

View file

@ -102,6 +102,8 @@ const sendMessage = async (req, res) => {
case 'Poll': { case 'Poll': {
const poll = new Poll(content.pollName, content.pollOptions, content.options) const poll = new Poll(content.pollName, content.pollOptions, content.options)
messageOut = await client.sendMessage(chatId, poll, options) messageOut = await client.sendMessage(chatId, poll, options)
// fix for poll events not being triggered (open the chat that you sent the poll)
await client.interface.openChatWindow(chatId)
break break
} }
default: default:
@ -1000,7 +1002,7 @@ const pinChat = async (req, res) => {
chatId: { chatId: {
type: 'string', type: 'string',
description: 'ID of the chat', description: 'ID of the chat',
example: '' example: '6281288888888@c.us'
}, },
} }
}, },
@ -1133,7 +1135,7 @@ const sendSeen = async (req, res) => {
chatId: { chatId: {
type: 'string', type: 'string',
description: 'ID of the chat', description: 'ID of the chat',
example: '' example: '6281288888888@c.us'
}, },
} }
}, },
@ -1210,7 +1212,7 @@ const unarchiveChat = async (req, res) => {
chatId: { chatId: {
type: 'string', type: 'string',
description: 'ID of the chat', description: 'ID of the chat',
example: '' example: '6281288888888@c.us'
}, },
} }
}, },
@ -1249,7 +1251,7 @@ const unmuteChat = async (req, res) => {
chatId: { chatId: {
type: 'string', type: 'string',
description: 'ID of the chat', description: 'ID of the chat',
example: '' example: '6281288888888@c.us'
}, },
} }
}, },
@ -1288,7 +1290,7 @@ const unpinChat = async (req, res) => {
chatId: { chatId: {
type: 'string', type: 'string',
description: 'ID of the chat', description: 'ID of the chat',
example: '' example: '6281288888888@c.us'
}, },
} }
}, },
@ -1532,7 +1534,7 @@ const syncHistory = async (req, res) => {
chatId: { chatId: {
type: 'string', type: 'string',
description: 'ID of the chat', description: 'ID of the chat',
example: '' example: '6281288888888@c.us'
}, },
} }
}, },
@ -1666,6 +1668,45 @@ const getFormattedNumber = async (req, res) => {
} }
} }
/**
* Open the chat window.
*
* @async
* @function
* @param {Object} req - The HTTP request object containing the chatId and sessionId.
* @param {string} req.body.chatId - The unique identifier of the chat to unmute.
* @param {string} req.params.sessionId - The unique identifier of the session associated with the client to use.
* @param {Object} res - The HTTP response object.
* @returns {Promise<Object>} - A Promise that resolves with a JSON object containing a success flag and the result of the operation.
* @throws {Error} - If an error occurs during the operation, it is thrown and handled by the catch block.
*/
const openChatWindow = async (req, res) => {
/*
#swagger.summary = 'Open the chat window'
#swagger.requestBody = {
required: true,
schema: {
type: 'object',
properties: {
chatId: {
type: 'string',
description: 'ID of the chat',
example: '6281288888888@c.us'
},
}
},
}
*/
try {
const { chatId } = req.body
const client = sessions.get(req.params.sessionId)
await client.interface.openChatWindow(chatId)
res.json({ success: true })
} catch (error) {
sendErrorResponse(res, 500, error.message)
}
}
module.exports = { module.exports = {
getClassInfo, getClassInfo,
acceptInvite, acceptInvite,
@ -1710,5 +1751,6 @@ module.exports = {
syncHistory, syncHistory,
getContactDeviceCount, getContactDeviceCount,
getCountryCode, getCountryCode,
getFormattedNumber getFormattedNumber,
openChatWindow
} }

View file

@ -472,10 +472,12 @@ const reply = async (req, res) => {
} }
case 'Poll': { case 'Poll': {
contentMessage = new Poll(content.pollName, content.pollOptions, content.options) contentMessage = new Poll(content.pollName, content.pollOptions, content.options)
// fix for poll events not being triggered (open the chat that you sent the poll)
await client.interface.openChatWindow(chatId)
break break
} }
default: default:
return sendErrorResponse(res, 400, 'contentType invalid, must be string, MessageMedia, MessageMediaFromURL, Location, Buttons, List, Contact or Poll') return sendErrorResponse(res, 400, 'contentType invalid, must be string, MessageMedia, MessageMediaFromURL, Location, Contact or Poll')
} }
const repliedMessage = await message.reply(contentMessage, chatId, options) const repliedMessage = await message.reply(contentMessage, chatId, options)
res.json({ success: true, repliedMessage }) res.json({ success: true, repliedMessage })

View file

@ -102,6 +102,7 @@ clientRouter.post('/syncHistory/:sessionId', [middleware.sessionNameValidation,
clientRouter.post('/getContactDeviceCount/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], clientController.getContactDeviceCount) clientRouter.post('/getContactDeviceCount/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], clientController.getContactDeviceCount)
clientRouter.post('/getCountryCode/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], clientController.getCountryCode) clientRouter.post('/getCountryCode/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], clientController.getCountryCode)
clientRouter.post('/getFormattedNumber/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], clientController.getFormattedNumber) clientRouter.post('/getFormattedNumber/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], clientController.getFormattedNumber)
clientRouter.post('/openChatWindow/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], clientController.openChatWindow)
/** /**
* ================ * ================

View file

@ -2876,7 +2876,7 @@
"chatId": { "chatId": {
"type": "string", "type": "string",
"description": "ID of the chat", "description": "ID of the chat",
"example": "" "example": "6281288888888@c.us"
} }
} }
} }
@ -2888,7 +2888,7 @@
"chatId": { "chatId": {
"type": "string", "type": "string",
"description": "ID of the chat", "description": "ID of the chat",
"example": "" "example": "6281288888888@c.us"
} }
} }
} }
@ -3396,7 +3396,7 @@
"chatId": { "chatId": {
"type": "string", "type": "string",
"description": "ID of the chat", "description": "ID of the chat",
"example": "" "example": "6281288888888@c.us"
} }
} }
} }
@ -3408,7 +3408,7 @@
"chatId": { "chatId": {
"type": "string", "type": "string",
"description": "ID of the chat", "description": "ID of the chat",
"example": "" "example": "6281288888888@c.us"
} }
} }
} }
@ -3600,10 +3600,10 @@
"schema": { "schema": {
"type": "object", "type": "object",
"properties": { "properties": {
"number": { "chatId": {
"type": "string", "type": "string",
"description": "Number or ID", "description": "ID of the chat",
"example": "" "example": "6281288888888@c.us"
} }
} }
} }
@ -3612,10 +3612,10 @@
"schema": { "schema": {
"type": "object", "type": "object",
"properties": { "properties": {
"number": { "chatId": {
"type": "string", "type": "string",
"description": "Number or ID", "description": "ID of the chat",
"example": "" "example": "6281288888888@c.us"
} }
} }
} }
@ -3803,7 +3803,7 @@
"chatId": { "chatId": {
"type": "string", "type": "string",
"description": "ID of the chat", "description": "ID of the chat",
"example": "" "example": "6281288888888@c.us"
} }
} }
} }
@ -3815,7 +3815,7 @@
"chatId": { "chatId": {
"type": "string", "type": "string",
"description": "ID of the chat", "description": "ID of the chat",
"example": "" "example": "6281288888888@c.us"
} }
} }
} }
@ -3903,7 +3903,7 @@
"chatId": { "chatId": {
"type": "string", "type": "string",
"description": "ID of the chat", "description": "ID of the chat",
"example": "" "example": "6281288888888@c.us"
} }
} }
} }
@ -3915,7 +3915,7 @@
"chatId": { "chatId": {
"type": "string", "type": "string",
"description": "ID of the chat", "description": "ID of the chat",
"example": "" "example": "6281288888888@c.us"
} }
} }
} }
@ -4003,7 +4003,7 @@
"chatId": { "chatId": {
"type": "string", "type": "string",
"description": "ID of the chat", "description": "ID of the chat",
"example": "" "example": "6281288888888@c.us"
} }
} }
} }
@ -4015,7 +4015,7 @@
"chatId": { "chatId": {
"type": "string", "type": "string",
"description": "ID of the chat", "description": "ID of the chat",
"example": "" "example": "6281288888888@c.us"
} }
} }
} }
@ -4645,7 +4645,7 @@
"chatId": { "chatId": {
"type": "string", "type": "string",
"description": "ID of the chat", "description": "ID of the chat",
"example": "" "example": "6281288888888@c.us"
} }
} }
} }
@ -4657,7 +4657,7 @@
"chatId": { "chatId": {
"type": "string", "type": "string",
"description": "ID of the chat", "description": "ID of the chat",
"example": "" "example": "6281288888888@c.us"
} }
} }
} }
@ -4966,6 +4966,106 @@
} }
} }
}, },
"/client/openChatWindow/{sessionId}": {
"post": {
"tags": [
"Client"
],
"summary": "Open the chat window",
"description": "",
"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": "ID of the chat",
"example": "6281288888888@c.us"
}
}
}
},
"application/xml": {
"schema": {
"type": "object",
"properties": {
"chatId": {
"type": "string",
"description": "ID of the chat",
"example": "6281288888888@c.us"
}
}
}
}
}
}
}
},
"/chat/getClassInfo/{sessionId}": { "/chat/getClassInfo/{sessionId}": {
"post": { "post": {
"tags": [ "tags": [