refactor: document endpoints
This commit is contained in:
parent
61491019cf
commit
8263ffb942
9 changed files with 1696 additions and 1022 deletions
|
|
@ -1,3 +1,4 @@
|
|||
const { MessageMedia, Location, Buttons, List, Poll } = require('whatsapp-web.js')
|
||||
const { sessions } = require('../sessions')
|
||||
const { sendErrorResponse } = require('../utils')
|
||||
|
||||
|
|
@ -14,8 +15,7 @@ const { sendErrorResponse } = require('../utils')
|
|||
const _getMessageById = async (client, messageId, chatId) => {
|
||||
const chat = await client.getChatById(chatId)
|
||||
const messages = await chat.fetchMessages({ limit: 100 })
|
||||
const message = messages.find((message) => { return message.id.id === messageId })
|
||||
return message
|
||||
return messages.find((message) => { return message.id.id === messageId })
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -30,11 +30,14 @@ const _getMessageById = async (client, messageId, chatId) => {
|
|||
* @returns {Promise<void>} - A Promise that resolves with no value when the function completes.
|
||||
*/
|
||||
const getClassInfo = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Get message'
|
||||
*/
|
||||
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') }
|
||||
if (!message) { throw new Error('Message not found') }
|
||||
res.json({ success: true, message })
|
||||
} catch (error) {
|
||||
sendErrorResponse(res, 500, error.message)
|
||||
|
|
@ -54,11 +57,37 @@ const getClassInfo = async (req, res) => {
|
|||
* @returns {Promise<void>} - A Promise that resolves with no value when the function completes.
|
||||
*/
|
||||
const deleteMessage = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Delete a message from the chat'
|
||||
#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'
|
||||
},
|
||||
everyone: {
|
||||
type: 'boolean',
|
||||
description: 'If true and the message is sent by the current user or the user is an admin, will delete it for everyone in the chat.',
|
||||
example: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
try {
|
||||
const { messageId, chatId, everyone } = 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 (!message) { throw new Error('Message not found') }
|
||||
const result = await message.delete(everyone)
|
||||
res.json({ success: true, result })
|
||||
} catch (error) {
|
||||
|
|
@ -79,12 +108,15 @@ const deleteMessage = async (req, res) => {
|
|||
* @returns {Promise<void>} - A Promise that resolves with no value when the function completes.
|
||||
*/
|
||||
const downloadMedia = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Download attached message media'
|
||||
*/
|
||||
try {
|
||||
const { messageId, chatId, everyone } = req.body
|
||||
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 messageMedia = await message.downloadMedia(everyone)
|
||||
if (!message) { throw new Error('Message not found') }
|
||||
const messageMedia = await message.downloadMedia()
|
||||
res.json({ success: true, messageMedia })
|
||||
} catch (error) {
|
||||
sendErrorResponse(res, 500, error.message)
|
||||
|
|
@ -106,11 +138,37 @@ const downloadMedia = async (req, res) => {
|
|||
* @throws Will throw an error if the message is not found or if there is an error during the forward operation.
|
||||
*/
|
||||
const forward = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Delete a message from the chat'
|
||||
#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'
|
||||
},
|
||||
destinationChatId: {
|
||||
type: 'string',
|
||||
description: 'The chat id to forward the message to',
|
||||
example: '6281288888889@c.us'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
try {
|
||||
const { messageId, chatId, destinationChatId } = 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 (!message) { throw new Error('Message not found') }
|
||||
const result = await message.forward(destinationChatId)
|
||||
res.json({ success: true, result })
|
||||
} catch (error) {
|
||||
|
|
@ -132,11 +190,15 @@ const forward = async (req, res) => {
|
|||
* @throws Will throw an error if the message is not found or if there is an error during the get info operation.
|
||||
*/
|
||||
const getInfo = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Get information about message delivery status'
|
||||
#swagger.description = 'May return null if the message does not exist or is not sent by you.'
|
||||
*/
|
||||
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') }
|
||||
if (!message) { throw new Error('Message not found') }
|
||||
const info = await message.getInfo()
|
||||
res.json({ success: true, info })
|
||||
} catch (error) {
|
||||
|
|
@ -159,11 +221,14 @@ const getInfo = async (req, res) => {
|
|||
* @throws {Error} - If there's an error retrieving the message or mentions
|
||||
*/
|
||||
const getMentions = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Get the contacts mentioned'
|
||||
*/
|
||||
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') }
|
||||
if (!message) { throw new Error('Message not found') }
|
||||
const contacts = await message.getMentions()
|
||||
res.json({ success: true, contacts })
|
||||
} catch (error) {
|
||||
|
|
@ -186,11 +251,14 @@ const getMentions = async (req, res) => {
|
|||
* @throws {Error} - If there's an error retrieving the message or order information
|
||||
*/
|
||||
const getOrder = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Get the order details'
|
||||
*/
|
||||
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') }
|
||||
if (!message) { throw new Error('Message not found') }
|
||||
const order = await message.getOrder()
|
||||
res.json({ success: true, order })
|
||||
} catch (error) {
|
||||
|
|
@ -213,11 +281,14 @@ const getOrder = async (req, res) => {
|
|||
* @throws {Object} If the specified message is not found or if an error occurs during the retrieval process.
|
||||
*/
|
||||
const getPayment = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Get the payment details'
|
||||
*/
|
||||
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') }
|
||||
if (!message) { throw new Error('Message not found') }
|
||||
const payment = await message.getPayment()
|
||||
res.json({ success: true, payment })
|
||||
} catch (error) {
|
||||
|
|
@ -240,11 +311,14 @@ const getPayment = async (req, res) => {
|
|||
* @throws {Object} If the specified message is not found or if an error occurs during the retrieval process.
|
||||
*/
|
||||
const getQuotedMessage = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Get the quoted message'
|
||||
*/
|
||||
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') }
|
||||
if (!message) { throw new Error('Message not found') }
|
||||
const quotedMessage = await message.getQuotedMessage()
|
||||
res.json({ success: true, quotedMessage })
|
||||
} catch (error) {
|
||||
|
|
@ -268,11 +342,37 @@ const getQuotedMessage = async (req, res) => {
|
|||
* @throws {Error} If there was an error during the operation.
|
||||
*/
|
||||
const react = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'React with an emoji'
|
||||
#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'
|
||||
},
|
||||
reaction: {
|
||||
type: 'string',
|
||||
description: 'Emoji to react with. Send an empty string to remove the reaction.',
|
||||
example: '👍'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
try {
|
||||
const { messageId, chatId, reaction } = req.body
|
||||
const { messageId, chatId, reaction = '' } = 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 (!message) { throw new Error('Message not found') }
|
||||
const result = await message.react(reaction)
|
||||
res.json({ success: true, result })
|
||||
} catch (error) {
|
||||
|
|
@ -297,12 +397,95 @@ const react = async (req, res) => {
|
|||
* @throws {Error} If there was an error during the operation.
|
||||
*/
|
||||
const reply = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Send a message as a reply'
|
||||
#swagger.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 identifier for the message',
|
||||
example: 'ABCDEF999999999'
|
||||
},
|
||||
contentType: {
|
||||
type: 'string',
|
||||
description: 'The type of message content, must be one of the following: string, MessageMedia, MessageMediaFromURL, Location, Contact, Poll, Buttons, or List',
|
||||
},
|
||||
content: {
|
||||
type: 'object',
|
||||
description: 'The content of the message, can be a string or an object',
|
||||
},
|
||||
options: {
|
||||
type: 'object',
|
||||
description: 'The message send options',
|
||||
}
|
||||
}
|
||||
},
|
||||
examples: {
|
||||
string: { value: { messageId: '3A80E857F9B44AF60C2C', chatId: '6281288888888@c.us', contentType: 'string', content: 'Reply text!' } }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
try {
|
||||
const { messageId, chatId, content, destinationChatId, options } = req.body
|
||||
const { messageId, chatId, content, contentType, 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') }
|
||||
const repliedMessage = await message.reply(content, destinationChatId, options)
|
||||
if (!message) { throw new Error('Message not found') }
|
||||
let contentMessage
|
||||
switch (contentType) {
|
||||
case 'string':
|
||||
if (options?.media) {
|
||||
const media = options.media
|
||||
media.filename = null
|
||||
media.filesize = null
|
||||
options.media = new MessageMedia(media.mimetype, media.data, media.filename, media.filesize)
|
||||
}
|
||||
contentMessage = content
|
||||
break
|
||||
case 'MessageMediaFromURL': {
|
||||
contentMessage = await MessageMedia.fromUrl(content, { unsafeMime: true })
|
||||
break
|
||||
}
|
||||
case 'MessageMedia': {
|
||||
contentMessage = new MessageMedia(content.mimetype, content.data, content.filename, content.filesize)
|
||||
break
|
||||
}
|
||||
case 'Location': {
|
||||
contentMessage = new Location(content.latitude, content.longitude, content.description)
|
||||
break
|
||||
}
|
||||
case 'Buttons': {
|
||||
contentMessage = new Buttons(content.body, content.buttons, content.title, content.footer)
|
||||
break
|
||||
}
|
||||
case 'List': {
|
||||
contentMessage = new List(content.body, content.buttonText, content.sections, content.title, content.footer)
|
||||
break
|
||||
}
|
||||
case 'Contact': {
|
||||
const contactId = content.contactId.endsWith('@c.us') ? content.contactId : `${content.contactId}@c.us`
|
||||
contentMessage = await client.getContactById(contactId)
|
||||
break
|
||||
}
|
||||
case 'Poll': {
|
||||
contentMessage = new Poll(content.pollName, content.pollOptions, content.options)
|
||||
break
|
||||
}
|
||||
default:
|
||||
return sendErrorResponse(res, 400, 'contentType invalid, must be string, MessageMedia, MessageMediaFromURL, Location, Buttons, List, Contact or Poll')
|
||||
}
|
||||
const repliedMessage = await message.reply(contentMessage, chatId, options)
|
||||
res.json({ success: true, repliedMessage })
|
||||
} catch (error) {
|
||||
sendErrorResponse(res, 500, error.message)
|
||||
|
|
@ -319,14 +502,17 @@ const reply = async (req, res) => {
|
|||
* @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.star() call.
|
||||
* @throws {Error} If message is not found, it throws an error with the message "Message not Found".
|
||||
* @throws {Error} If message is not found, it throws an error with the message "Message not found".
|
||||
*/
|
||||
const star = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Star the message'
|
||||
*/
|
||||
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') }
|
||||
if (!message) { throw new Error('Message not found') }
|
||||
const result = await message.star()
|
||||
res.json({ success: true, result })
|
||||
} catch (error) {
|
||||
|
|
@ -344,14 +530,17 @@ const star = async (req, res) => {
|
|||
* @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.unstar() call.
|
||||
* @throws {Error} If message is not found, it throws an error with the message "Message not Found".
|
||||
* @throws {Error} If message is not found, it throws an error with the message "Message not found".
|
||||
*/
|
||||
const unstar = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Unstar the message'
|
||||
*/
|
||||
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') }
|
||||
if (!message) { throw new Error('Message not found') }
|
||||
const result = await message.unstar()
|
||||
res.json({ success: true, result })
|
||||
} catch (error) {
|
||||
|
|
@ -359,6 +548,144 @@ const unstar = async (req, res) => {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @function getReactions
|
||||
* @async
|
||||
* @description Gets the reactions associated with the given message.
|
||||
* @param {Object} req - The request object.
|
||||
* @param {Object} res - The response object.
|
||||
* @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.
|
||||
* @throws {Error} If message is not found, it throws an error with the message "Message not found".
|
||||
*/
|
||||
const getReactions = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Get the reactions associated'
|
||||
*/
|
||||
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 result = await message.getReactions()
|
||||
res.json({ success: true, result })
|
||||
} catch (error) {
|
||||
sendErrorResponse(res, 500, error.message)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @function getReactions
|
||||
* @async
|
||||
* @description Gets groups mentioned in this message
|
||||
* @param {Object} req - The request object.
|
||||
* @param {Object} res - The response object.
|
||||
* @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.
|
||||
* @throws {Error} If message is not found, it throws an error with the message "Message not found".
|
||||
*/
|
||||
const getGroupMentions = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Get groups mentioned in this message'
|
||||
*/
|
||||
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 result = await message.getGroupMentions()
|
||||
res.json({ success: true, result })
|
||||
} catch (error) {
|
||||
sendErrorResponse(res, 500, error.message)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @function edit
|
||||
* @async
|
||||
* @description Edits the current message
|
||||
* @param {Object} req - The request object.
|
||||
* @param {Object} res - The response object.
|
||||
* @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.edit() call.
|
||||
* @throws {Error} If message is not found, it throws an error with the message "Message not found".
|
||||
*/
|
||||
const edit = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Edit the message'
|
||||
#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'
|
||||
},
|
||||
content: {
|
||||
type: 'string',
|
||||
description: 'The content of the message',
|
||||
},
|
||||
options: {
|
||||
type: 'object',
|
||||
description: 'Options used when editing the message',
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
try {
|
||||
const { messageId, chatId, content, 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') }
|
||||
const editedMessage = await message.edit(content, options)
|
||||
res.json({ success: true, message: editedMessage })
|
||||
} catch (error) {
|
||||
sendErrorResponse(res, 500, error.message)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @function getContact
|
||||
* @async
|
||||
* @description Gets groups mentioned in this message
|
||||
* @param {Object} req - The request object.
|
||||
* @param {Object} res - The response object.
|
||||
* @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.
|
||||
* @throws {Error} If message is not found, it throws an error with the message "Message not found".
|
||||
*/
|
||||
const getContact = async (req, res) => {
|
||||
/*
|
||||
#swagger.summary = 'Get the contact'
|
||||
*/
|
||||
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 contact = await message.getContact()
|
||||
res.json({ success: true, contact })
|
||||
} catch (error) {
|
||||
sendErrorResponse(res, 500, error.message)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getClassInfo,
|
||||
deleteMessage,
|
||||
|
|
@ -372,5 +699,9 @@ module.exports = {
|
|||
react,
|
||||
reply,
|
||||
star,
|
||||
unstar
|
||||
unstar,
|
||||
getReactions,
|
||||
getGroupMentions,
|
||||
edit,
|
||||
getContact
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue