feat: add ability to run any method
This commit is contained in:
parent
d609c0ca85
commit
b7d1e21739
6 changed files with 798 additions and 22 deletions
|
|
@ -448,6 +448,66 @@ const changeLabels = async (req, res) => {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a method on the chat associated with the given sessionId.
|
||||
*
|
||||
* @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.
|
||||
* @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 runMethod = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Execute a method on the chat'
|
||||
#swagger.description = 'Execute a method on the chat and return the result'
|
||||
#swagger.requestBody = {
|
||||
required: true,
|
||||
schema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
chatId: {
|
||||
type: 'string',
|
||||
description: 'The chat id which contains the message',
|
||||
example: '6281288888888@c.us'
|
||||
},
|
||||
method: {
|
||||
type: 'string',
|
||||
description: 'The name of the method to execute',
|
||||
example: 'getLabels'
|
||||
},
|
||||
options: {
|
||||
anyOf: [
|
||||
{ type: 'object' },
|
||||
{ type: 'string' }
|
||||
],
|
||||
description: 'The options to pass to the method',
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
*/
|
||||
try {
|
||||
const { chatId, method, options } = req.body
|
||||
const client = sessions.get(req.params.sessionId)
|
||||
const chat = await client.getChatById(chatId)
|
||||
if (!chat) {
|
||||
sendErrorResponse(res, 404, 'Chat not Found')
|
||||
return
|
||||
}
|
||||
if (typeof chat[method] !== 'function') {
|
||||
throw new Error('Method is not implemented')
|
||||
}
|
||||
const result = options ? await chat[method](options) : await chat[method]()
|
||||
res.json({ success: true, data: result })
|
||||
} catch (error) {
|
||||
sendErrorResponse(res, 500, error.message)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getClassInfo,
|
||||
clearMessages,
|
||||
|
|
@ -461,5 +521,6 @@ module.exports = {
|
|||
markUnread,
|
||||
syncHistory,
|
||||
getLabels,
|
||||
changeLabels
|
||||
changeLabels,
|
||||
runMethod
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1855,6 +1855,55 @@ const getContactLidAndPhone = async (req, res) => {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a method on the client associated with the given sessionId.
|
||||
*
|
||||
* @async
|
||||
* @function
|
||||
* @param {Object} req - The HTTP request object containing the chatId and sessionId.
|
||||
* @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 runMethod = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Execute a method on the client'
|
||||
#swagger.description = 'Execute a method on the client and return the result'
|
||||
#swagger.requestBody = {
|
||||
required: true,
|
||||
schema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
method: {
|
||||
type: 'string',
|
||||
description: 'The name of the method to execute',
|
||||
example: 'getLabels'
|
||||
},
|
||||
options: {
|
||||
anyOf: [
|
||||
{ type: 'object' },
|
||||
{ type: 'string' }
|
||||
],
|
||||
description: 'The options to pass to the method',
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
*/
|
||||
try {
|
||||
const { method, options } = req.body
|
||||
const client = sessions.get(req.params.sessionId)
|
||||
if (typeof client[method] !== 'function') {
|
||||
throw new Error('Method is not implemented')
|
||||
}
|
||||
const result = options ? await client[method](options) : await client[method]()
|
||||
res.json({ success: true, data: result })
|
||||
} catch (error) {
|
||||
sendErrorResponse(res, 500, error.message)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getClassInfo,
|
||||
acceptInvite,
|
||||
|
|
@ -1904,5 +1953,6 @@ module.exports = {
|
|||
openChatWindowAt,
|
||||
resetState,
|
||||
setBackgroundSync,
|
||||
getContactLidAndPhone
|
||||
getContactLidAndPhone,
|
||||
runMethod
|
||||
}
|
||||
|
|
|
|||
|
|
@ -680,6 +680,63 @@ const rejectGroupMembershipRequests = async (req, res) => {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a method on the group associated with the given sessionId.
|
||||
*
|
||||
* @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.
|
||||
* @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 runMethod = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Execute a method on the group'
|
||||
#swagger.description = 'Execute a method on the group and return the result'
|
||||
#swagger.requestBody = {
|
||||
required: true,
|
||||
schema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
chatId: {
|
||||
type: 'string',
|
||||
description: 'Unique WhatsApp id for the given chat group',
|
||||
example: 'XXXXXXXXXX@g.us'
|
||||
},
|
||||
method: {
|
||||
type: 'string',
|
||||
description: 'The name of the method to execute',
|
||||
example: 'getLabels'
|
||||
},
|
||||
options: {
|
||||
anyOf: [
|
||||
{ type: 'object' },
|
||||
{ type: 'string' }
|
||||
],
|
||||
description: 'The options to pass to the method',
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
*/
|
||||
try {
|
||||
const { chatId, method, options } = req.body
|
||||
const client = sessions.get(req.params.sessionId)
|
||||
const chat = await client.getChatById(chatId)
|
||||
if (!chat.isGroup) { throw new Error('The chat is not a group') }
|
||||
if (typeof chat[method] !== 'function') {
|
||||
throw new Error('Method is not implemented')
|
||||
}
|
||||
const result = options ? await chat[method](options) : await chat[method]()
|
||||
res.json({ success: true, data: result })
|
||||
} catch (error) {
|
||||
sendErrorResponse(res, 500, error.message)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getClassInfo,
|
||||
addParticipants,
|
||||
|
|
@ -697,5 +754,6 @@ module.exports = {
|
|||
deletePicture,
|
||||
getGroupMembershipRequests,
|
||||
approveGroupMembershipRequests,
|
||||
rejectGroupMembershipRequests
|
||||
rejectGroupMembershipRequests,
|
||||
runMethod
|
||||
}
|
||||
|
|
|
|||
|
|
@ -735,6 +735,68 @@ const getContact = async (req, res) => {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a method on the message associated with the given sessionId.
|
||||
*
|
||||
* @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.
|
||||
* @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 runMethod = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Execute a method on the message'
|
||||
#swagger.description = 'Execute a method on the message and return the result'
|
||||
#swagger.requestBody = {
|
||||
required: true,
|
||||
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 identifier for the message',
|
||||
example: 'ABCDEF999999999'
|
||||
},
|
||||
method: {
|
||||
type: 'string',
|
||||
description: 'The name of the method to execute',
|
||||
example: 'getInfo'
|
||||
},
|
||||
options: {
|
||||
anyOf: [
|
||||
{ type: 'object' },
|
||||
{ type: 'string' }
|
||||
],
|
||||
description: 'The options to pass to the method',
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
*/
|
||||
try {
|
||||
const { messageId, chatId, method, options } = req.body
|
||||
const client = sessions.get(req.params.sessionId)
|
||||
const message = await _getMessageById(client, messageId, chatId)
|
||||
if (!message) { throw new Error('Message not found') }
|
||||
if (typeof message[method] !== 'function') {
|
||||
throw new Error('Method is not implemented')
|
||||
}
|
||||
const result = options ? await message[method](options) : await message[method]()
|
||||
res.json({ success: true, data: result })
|
||||
} catch (error) {
|
||||
sendErrorResponse(res, 500, error.message)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getClassInfo,
|
||||
deleteMessage,
|
||||
|
|
@ -753,5 +815,6 @@ module.exports = {
|
|||
getReactions,
|
||||
getGroupMentions,
|
||||
edit,
|
||||
getContact
|
||||
getContact,
|
||||
runMethod
|
||||
}
|
||||
|
|
|
|||
|
|
@ -109,6 +109,7 @@ clientRouter.post('/openChatWindowAt/:sessionId', [middleware.sessionNameValidat
|
|||
clientRouter.post('/resetState/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], clientController.resetState)
|
||||
clientRouter.post('/setBackgroundSync/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], clientController.setBackgroundSync)
|
||||
clientRouter.post('/getContactLidAndPhone/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], clientController.getContactLidAndPhone)
|
||||
clientRouter.post('/runMethod/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], clientController.runMethod)
|
||||
|
||||
/**
|
||||
* ================
|
||||
|
|
@ -133,6 +134,7 @@ chatRouter.post('/markUnread/:sessionId', [middleware.sessionNameValidation, mid
|
|||
chatRouter.post('/syncHistory/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], chatController.syncHistory)
|
||||
chatRouter.post('/getLabels/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], chatController.getLabels)
|
||||
chatRouter.post('/changeLabels/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], chatController.changeLabels)
|
||||
chatRouter.post('/runMethod/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], chatController.runMethod)
|
||||
|
||||
/**
|
||||
* ================
|
||||
|
|
@ -161,6 +163,7 @@ groupChatRouter.post('/deletePicture/:sessionId', [middleware.sessionNameValidat
|
|||
groupChatRouter.post('/getGroupMembershipRequests/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], groupChatController.getGroupMembershipRequests)
|
||||
groupChatRouter.post('/approveGroupMembershipRequests/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], groupChatController.approveGroupMembershipRequests)
|
||||
groupChatRouter.post('/rejectGroupMembershipRequests/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], groupChatController.rejectGroupMembershipRequests)
|
||||
groupChatRouter.post('/runMethod/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], groupChatController.runMethod)
|
||||
|
||||
/**
|
||||
* ================
|
||||
|
|
@ -190,6 +193,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('/runMethod/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], messageController.runMethod)
|
||||
|
||||
/**
|
||||
* ================
|
||||
|
|
|
|||
Loading…
Reference in a new issue