Spaces:
Runtime error
Runtime error
| import { | |
| Body, | |
| Controller, | |
| Get, | |
| Post, | |
| Put, | |
| Query, | |
| UsePipes, | |
| ValidationPipe, | |
| } from '@nestjs/common'; | |
| import { ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger'; | |
| import { WAHAValidationPipe } from '@waha/nestjs/pipes/WAHAValidationPipe'; | |
| import { | |
| GetChatMessagesFilter, | |
| ReadChatMessagesQuery, | |
| transformAck, | |
| } from '@waha/structures/chats.dto'; | |
| import { SendButtonsRequest } from '@waha/structures/chatting.buttons.dto'; | |
| import { SendListRequest } from '@waha/structures/chatting.list.dto'; | |
| import { SessionManager } from '../core/abc/manager.abc'; | |
| import { | |
| ChatRequest, | |
| CheckNumberStatusQuery, | |
| GetMessageQuery, | |
| MessageButtonReply, | |
| MessageContactVcardRequest, | |
| MessageFileRequest, | |
| MessageForwardRequest, | |
| MessageImageRequest, | |
| MessageLinkCustomPreviewRequest, | |
| MessageLinkPreviewRequest, | |
| MessageLocationRequest, | |
| MessagePollRequest, | |
| MessagePollVoteRequest, | |
| MessageReactionRequest, | |
| MessageReplyRequest, | |
| MessageStarRequest, | |
| MessageTextQuery, | |
| MessageTextRequest, | |
| MessageVideoRequest, | |
| MessageVoiceRequest, | |
| SendSeenRequest, | |
| WANumberExistResult, | |
| } from '../structures/chatting.dto'; | |
| import { WAMessage } from '../structures/responses.dto'; | |
| ('api_key') | |
| ('api') | |
| ('📤 Chatting') | |
| export class ChattingController { | |
| constructor(private manager: SessionManager) {} | |
| ('/sendText') | |
| ({ summary: 'Send a text message' }) | |
| async sendText(() request: MessageTextRequest): Promise<WAMessage> { | |
| const whatsapp = await this.manager.getWorkingSession(request.session); | |
| return whatsapp.sendText(request); | |
| } | |
| ('/sendImage') | |
| ({ | |
| summary: 'Send an image', | |
| description: | |
| 'Either from an URL or base64 data - look at the request schemas for details.', | |
| }) | |
| async sendImage(() request: MessageImageRequest) { | |
| const whatsapp = await this.manager.getWorkingSession(request.session); | |
| return whatsapp.sendImage(request); | |
| } | |
| ('/sendFile') | |
| ({ | |
| summary: 'Send a file', | |
| description: | |
| 'Either from an URL or base64 data - look at the request schemas for details.', | |
| }) | |
| async sendFile(() request: MessageFileRequest) { | |
| const whatsapp = await this.manager.getWorkingSession(request.session); | |
| return whatsapp.sendFile(request); | |
| } | |
| ('/sendVoice') | |
| ({ | |
| summary: 'Send an voice message', | |
| description: | |
| 'Either from an URL or base64 data - look at the request schemas for details.', | |
| }) | |
| async sendVoice(() request: MessageVoiceRequest) { | |
| const whatsapp = await this.manager.getWorkingSession(request.session); | |
| return whatsapp.sendVoice(request); | |
| } | |
| ('/sendVideo') | |
| ({ | |
| summary: 'Send a video', | |
| description: | |
| 'Either from an URL or base64 data - look at the request schemas for details.', | |
| }) | |
| async sendVideo(() request: MessageVideoRequest) { | |
| const whatsapp = await this.manager.getWorkingSession(request.session); | |
| return whatsapp.sendVideo(request); | |
| } | |
| ('/send/link-custom-preview') | |
| ({ | |
| summary: 'Send a text message with a CUSTOM link preview.', | |
| description: | |
| 'You can use regular /api/sendText if you wanna send auto-generated link preview.', | |
| }) | |
| (new WAHAValidationPipe()) | |
| async sendLinkCustomPreview( | |
| () request: MessageLinkCustomPreviewRequest, | |
| ): Promise<any> { | |
| const whatsapp = await this.manager.getWorkingSession(request.session); | |
| if (!request.text.includes(request.preview.url)) { | |
| throw new Error( | |
| '"text" must include the URL provided in the "preview.url"', | |
| ); | |
| } | |
| return whatsapp.sendLinkCustomPreview(request); | |
| } | |
| ('/sendButtons') | |
| ({ | |
| summary: 'Send buttons message (interactive)', | |
| description: 'Send Buttons', | |
| deprecated: true, | |
| }) | |
| (new WAHAValidationPipe()) | |
| async sendButtons(() request: SendButtonsRequest) { | |
| const whatsapp = await this.manager.getWorkingSession(request.session); | |
| return whatsapp.sendButtons(request); | |
| } | |
| ('/sendList') | |
| ({ | |
| summary: 'Send a list message (interactive)', | |
| description: 'Send a List message with sections and rows', | |
| }) | |
| (new WAHAValidationPipe()) | |
| async sendList(() request: SendListRequest) { | |
| const whatsapp = await this.manager.getWorkingSession(request.session); | |
| return whatsapp.sendList(request); | |
| } | |
| ('/forwardMessage') | |
| async forwardMessage( | |
| () request: MessageForwardRequest, | |
| ): Promise<WAMessage> { | |
| const whatsapp = await this.manager.getWorkingSession(request.session); | |
| return await whatsapp.forwardMessage(request); | |
| } | |
| ('/sendSeen') | |
| async sendSeen(() chat: SendSeenRequest) { | |
| const hasMessageId = chat.messageIds?.length > 0 || Boolean(chat.messageId); | |
| if (!hasMessageId) { | |
| const whatsapp = await this.manager.getWorkingSession(chat.session); | |
| const query: ReadChatMessagesQuery = { | |
| messages: null, | |
| days: 7, | |
| }; | |
| return whatsapp.readChatMessages(chat.chatId, query); | |
| } | |
| const whatsapp = await this.manager.getWorkingSession(chat.session); | |
| return whatsapp.sendSeen(chat); | |
| } | |
| ('/startTyping') | |
| async startTyping(() chat: ChatRequest) { | |
| // It's infinitive action | |
| const whatsapp = await this.manager.getWorkingSession(chat.session); | |
| await whatsapp.startTyping(chat); | |
| return { result: true }; | |
| } | |
| ('/stopTyping') | |
| async stopTyping(() chat: ChatRequest) { | |
| const whatsapp = await this.manager.getWorkingSession(chat.session); | |
| await whatsapp.stopTyping(chat); | |
| return { result: true }; | |
| } | |
| ('/reaction') | |
| ({ summary: 'React to a message with an emoji' }) | |
| async setReaction(() request: MessageReactionRequest) { | |
| const whatsapp = await this.manager.getWorkingSession(request.session); | |
| return whatsapp.setReaction(request); | |
| } | |
| ('/star') | |
| ({ summary: 'Star or unstar a message' }) | |
| async setStar(() request: MessageStarRequest) { | |
| const whatsapp = await this.manager.getWorkingSession(request.session); | |
| await whatsapp.setStar(request); | |
| return; | |
| } | |
| ('/sendPoll') | |
| ({ | |
| summary: 'Send a poll with options', | |
| description: 'You can use it as buttons or list replacement', | |
| }) | |
| async sendPoll(() request: MessagePollRequest) { | |
| const whatsapp = await this.manager.getWorkingSession(request.session); | |
| return whatsapp.sendPoll(request); | |
| } | |
| ('/sendPollVote') | |
| ({ | |
| summary: 'Vote on a poll', | |
| description: 'Cast vote(s) on an existing poll message', | |
| }) | |
| (new WAHAValidationPipe()) | |
| async sendPollVote(() request: MessagePollVoteRequest) { | |
| const whatsapp = await this.manager.getWorkingSession(request.session); | |
| return whatsapp.sendPollVote(request); | |
| } | |
| ('/sendLocation') | |
| async sendLocation(() request: MessageLocationRequest) { | |
| const whatsapp = await this.manager.getWorkingSession(request.session); | |
| return whatsapp.sendLocation(request); | |
| } | |
| ('/sendContactVcard') | |
| async sendContactVcard(() request: MessageContactVcardRequest) { | |
| const whatsapp = await this.manager.getWorkingSession(request.session); | |
| return whatsapp.sendContactVCard(request); | |
| } | |
| ('/send/buttons/reply') | |
| ({ | |
| summary: 'Reply on a button message', | |
| }) | |
| (new ValidationPipe({ transform: true, whitelist: true })) | |
| async sendButtonsReply(() request: MessageButtonReply) { | |
| const whatsapp = await this.manager.getWorkingSession(request.session); | |
| return whatsapp.sendButtonsReply(request); | |
| } | |
| ('/sendText') | |
| ({ summary: 'Send a text message', deprecated: true }) | |
| async sendTextGet(() query: MessageTextQuery) { | |
| const whatsapp = await this.manager.getWorkingSession(query.session); | |
| const msg = new MessageTextRequest(); | |
| msg.chatId = query.phone; | |
| msg.text = query.text; | |
| return whatsapp.sendText(msg); | |
| } | |
| ('/messages') | |
| ({ | |
| summary: 'Get messages in a chat', | |
| description: 'DEPRECATED. Use "GET /api/chats/{id}/messages" instead', | |
| deprecated: true, | |
| }) | |
| (new ValidationPipe({ transform: true, whitelist: true })) | |
| async getMessages( | |
| () query: GetMessageQuery, | |
| () filter: GetChatMessagesFilter, | |
| ) { | |
| filter = transformAck(filter); | |
| const whatsapp = await this.manager.getWorkingSession(query.session); | |
| return whatsapp.getChatMessages(query.chatId, query, filter); | |
| } | |
| ('/checkNumberStatus') | |
| ({ | |
| summary: 'Check number status', | |
| description: 'DEPRECATED. Use "POST /contacts/check-exists" instead', | |
| deprecated: true, | |
| }) | |
| async DEPRECATED_checkNumberStatus( | |
| () request: CheckNumberStatusQuery, | |
| ): Promise<WANumberExistResult> { | |
| const whatsapp = await this.manager.getWorkingSession(request.session); | |
| return whatsapp.checkNumberStatus(request); | |
| } | |
| ('/reply') | |
| ({ | |
| summary: | |
| 'DEPRECATED - you can set "reply_to" field when sending text, image, etc', | |
| deprecated: true, | |
| }) | |
| async reply(() request: MessageReplyRequest) { | |
| const whatsapp = await this.manager.getWorkingSession(request.session); | |
| return whatsapp.reply(request); | |
| } | |
| ('/sendLinkPreview') | |
| ({ deprecated: true }) | |
| async sendLinkPreview_DEPRECATED(() request: MessageLinkPreviewRequest) { | |
| const whatsapp = await this.manager.getWorkingSession(request.session); | |
| return whatsapp.sendLinkPreview(request); | |
| } | |
| } | |