/** * Messages resource — sending and querying messages. * * Backed by `src/modules/message/message.controller.ts`. * NOTE: the real paths use the `/send-` prefix, e.g. `/messages/send-text`. * @packageDocumentation */ import { encodeSegment } from '../http.js'; import type { OpenWAClient } from '../client.js'; import type { BatchStatusResponse, BulkMessageResponse, ChatHistoryMessage, DeleteMessageRequest, EditMessageRequest, ForwardMessageRequest, ListMessagesQuery, MessageHistoryQuery, MessageListResponse, MessageResponse, ReactionRecord, ReactMessageRequest, ReplyMessageRequest, SendBulkRequest, SendContactRequest, SendLocationRequest, SendAudioRequest, SendMediaRequest, SendTemplateRequest, SendTextRequest, SuccessResult, } from '../types.js'; export class MessagesResource { constructor(private readonly client: OpenWAClient) {} /** List messages, optionally filtered by chat or sender. Returns a `{ messages, total }` page. */ list(sessionId: string, query?: ListMessagesQuery): Promise { return this.client.request({ method: 'GET', path: `/api/sessions/${encodeSegment(sessionId)}/messages`, query, }); } /** Send a text message. */ sendText(sessionId: string, body: SendTextRequest): Promise { return this.client.request({ method: 'POST', path: `/api/sessions/${encodeSegment(sessionId)}/messages/send-text`, body, }); } /** Send an image (url or base64). */ sendImage(sessionId: string, body: SendMediaRequest): Promise { return this.client.sendMedia(sessionId, 'send-image', body); } /** Send a video (url or base64). */ sendVideo(sessionId: string, body: SendMediaRequest): Promise { return this.client.sendMedia(sessionId, 'send-video', body); } /** Send an audio file (url or base64). */ sendAudio(sessionId: string, body: SendAudioRequest): Promise { return this.client.sendMedia(sessionId, 'send-audio', body); } /** Send a document (url or base64; `filename` required). */ sendDocument(sessionId: string, body: SendMediaRequest): Promise { return this.client.sendMedia(sessionId, 'send-document', body); } /** Send a sticker (url or base64). */ sendSticker(sessionId: string, body: SendMediaRequest): Promise { return this.client.sendMedia(sessionId, 'send-sticker', body); } /** Send a location. */ sendLocation(sessionId: string, body: SendLocationRequest): Promise { return this.client.request({ method: 'POST', path: `/api/sessions/${encodeSegment(sessionId)}/messages/send-location`, body, }); } /** Send a contact card. */ sendContact(sessionId: string, body: SendContactRequest): Promise { return this.client.request({ method: 'POST', path: `/api/sessions/${encodeSegment(sessionId)}/messages/send-contact`, body, }); } /** Render and send a stored message template. */ sendTemplate(sessionId: string, body: SendTemplateRequest): Promise { return this.client.request({ method: 'POST', path: `/api/sessions/${encodeSegment(sessionId)}/messages/send-template`, body, }); } /** Reply to a specific message. */ reply(sessionId: string, body: ReplyMessageRequest): Promise { return this.client.request({ method: 'POST', path: `/api/sessions/${encodeSegment(sessionId)}/messages/reply`, body, }); } /** Forward a message to another chat. */ forward(sessionId: string, body: ForwardMessageRequest): Promise { return this.client.request({ method: 'POST', path: `/api/sessions/${encodeSegment(sessionId)}/messages/forward`, body, }); } /** React to a message (empty `emoji` removes the reaction). */ react(sessionId: string, body: ReactMessageRequest): Promise { return this.client.request({ method: 'POST', path: `/api/sessions/${encodeSegment(sessionId)}/messages/react`, body, }); } /** Delete a message. */ delete(sessionId: string, body: DeleteMessageRequest): Promise { return this.client.request({ method: 'POST', path: `/api/sessions/${encodeSegment(sessionId)}/messages/delete`, body, }); } /** Edit the text of an own message (404 if the message is not found). */ editMessage(sessionId: string, body: EditMessageRequest): Promise { return this.client.request({ method: 'POST', path: `/api/sessions/${encodeSegment(sessionId)}/messages/edit`, body, }); } /** Get the message history for a chat (read live from WhatsApp). */ history(sessionId: string, chatId: string, query?: MessageHistoryQuery): Promise { return this.client.request({ method: 'GET', path: `/api/sessions/${encodeSegment(sessionId)}/messages/${encodeSegment(chatId)}/history`, query, }); } /** Get reactions for a specific message. */ reactions(sessionId: string, chatId: string, messageId: string): Promise { return this.client.request({ method: 'GET', path: `/api/sessions/${encodeSegment(sessionId)}/messages/${encodeSegment(chatId)}/${encodeSegment(messageId)}/reactions`, }); } /** * Send a batch of messages asynchronously. Returns HTTP 202 with a batch id; * poll the status via {@link batchStatus}. */ sendBulk(sessionId: string, body: SendBulkRequest): Promise { return this.client.request({ method: 'POST', path: `/api/sessions/${encodeSegment(sessionId)}/messages/send-bulk`, body, }); } /** Poll the status/progress of a bulk send batch. */ batchStatus(sessionId: string, batchId: string): Promise { return this.client.request({ method: 'GET', path: `/api/sessions/${encodeSegment(sessionId)}/messages/batch/${encodeSegment(batchId)}`, }); } /** Cancel a running batch. Requires an OPERATOR-level key. */ cancelBatch(sessionId: string, batchId: string): Promise { return this.client.request({ method: 'POST', path: `/api/sessions/${encodeSegment(sessionId)}/messages/batch/${encodeSegment(batchId)}/cancel`, }); } }