diff --git a/src/api/labels.controller.ts b/src/api/labels.controller.ts index 8f9cd75f..308096e2 100644 --- a/src/api/labels.controller.ts +++ b/src/api/labels.controller.ts @@ -1,5 +1,6 @@ import { Body, Controller, Get, Post, Query } from '@nestjs/common'; import { ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger'; +import { Label } from '@waha/structures/labels.dto'; import { SessionManager } from '../core/abc/manager.abc'; import { SessionQuery } from '../structures/base.dto'; @@ -12,7 +13,7 @@ export class LabelsController { @Get('/') @ApiOperation({ summary: 'Get all labels' }) - getAll(@Query() query: SessionQuery) { + getAll(@Query() query: SessionQuery): Promise { const whatsapp = this.manager.getSession(query.session); return whatsapp.getLabels(); } diff --git a/src/core/SwaggerConfiguratorCore.ts b/src/core/SwaggerConfiguratorCore.ts index d73edb07..bd120d06 100644 --- a/src/core/SwaggerConfiguratorCore.ts +++ b/src/core/SwaggerConfiguratorCore.ts @@ -74,6 +74,10 @@ export class SwaggerConfiguratorCore { ) .addTag('groups', `Groups methods.
`) .addTag('presence', `Presence information`) + .addTag( + 'labels', + 'Labels - available only for WhatsApp Business accounts', + ) .addTag('other', 'Other methods') .addApiKey({ type: 'apiKey', diff --git a/src/core/abc/session.abc.ts b/src/core/abc/session.abc.ts index 10883bbe..cfc7c0fc 100644 --- a/src/core/abc/session.abc.ts +++ b/src/core/abc/session.abc.ts @@ -4,6 +4,7 @@ import { ListChannelsQuery, } from '@waha/structures/channels.dto'; import { GetChatsQuery } from '@waha/structures/chats.dto'; +import { Label } from '@waha/structures/labels.dto'; import { LoggerBuilder } from '@waha/utils/logging'; import { EventEmitter } from 'events'; import * as fs from 'fs'; @@ -336,7 +337,7 @@ export abstract class WhatsappSession { * Labels methods */ - public getLabels() { + public getLabels(): Promise { throw new NotImplementedByEngineError(); } diff --git a/src/core/app.module.core.ts b/src/core/app.module.core.ts index b8201e25..baa7d7b9 100644 --- a/src/core/app.module.core.ts +++ b/src/core/app.module.core.ts @@ -15,10 +15,10 @@ import { Logger } from 'pino'; import { AuthController } from '../api/auth.controller'; import { ChatsController } from '../api/chats.controller'; import { ChattingController } from '../api/chatting.controller'; -import { LabelsController } from '../api/labels.controller'; import { ContactsController } from '../api/contacts.controller'; import { GroupsController } from '../api/groups.controller'; import { HealthController } from '../api/health.controller'; +import { LabelsController } from '../api/labels.controller'; import { PingController } from '../api/ping.controller'; import { PresenceController } from '../api/presence.controller'; import { ScreenshotController } from '../api/screenshot.controller'; @@ -37,7 +37,6 @@ import { SwaggerConfigServiceCore } from './config/SwaggerConfigServiceCore'; import { WAHAHealthCheckServiceCore } from './health/WAHAHealthCheckServiceCore'; import { SessionManagerCore } from './manager.core'; - export const IMPORTS = [ LoggerModule.forRoot({ renameContext: 'name', diff --git a/src/core/engines/noweb/session.noweb.core.ts b/src/core/engines/noweb/session.noweb.core.ts index cf7559f5..9bb18a5d 100644 --- a/src/core/engines/noweb/session.noweb.core.ts +++ b/src/core/engines/noweb/session.noweb.core.ts @@ -35,6 +35,7 @@ import { } from '@waha/structures/channels.dto'; import { GetChatsQuery } from '@waha/structures/chats.dto'; import { ContactQuery, ContactRequest } from '@waha/structures/contacts.dto'; +import { Label } from '@waha/structures/labels.dto'; import { PollVote, PollVotePayload, @@ -727,7 +728,7 @@ export class WhatsappSessionNoWebCore extends WhatsappSession { * Labels methods */ - getLabels() { + public getLabels(): Promise { throw new NotImplementedByEngineError(); } diff --git a/src/core/engines/webjs/session.webjs.core.ts b/src/core/engines/webjs/session.webjs.core.ts index 1a964e6d..30bafad4 100644 --- a/src/core/engines/webjs/session.webjs.core.ts +++ b/src/core/engines/webjs/session.webjs.core.ts @@ -51,6 +51,7 @@ import { ParticipantsRequest, SettingsSecurityChangeInfo, } from '@waha/structures/groups.dto'; +import { Label } from '@waha/structures/labels.dto'; import { WAMessage, WAMessageReaction } from '@waha/structures/responses.dto'; import { MeInfo } from '@waha/structures/sessions.dto'; import { WAMessageRevokedBody } from '@waha/structures/webhooks.dto'; @@ -62,6 +63,7 @@ import { Contact, Events, GroupChat, + Label as WEBJSLabel, Location, Message, Reaction, @@ -337,7 +339,7 @@ export class WhatsappSessionWebJSCore extends WhatsappSession { sendText(request: MessageTextRequest) { const options = { // It's fine to sent just ids instead of Contact object - mentions: (request.mentions as unknown) as string[], + mentions: request.mentions as unknown as string[], }; return this.whatsapp.sendMessage( this.ensureSuffix(request.chatId), @@ -359,7 +361,7 @@ export class WhatsappSessionWebJSCore extends WhatsappSession { const message = this.recreateMessage(messageId); const options = { // It's fine to sent just ids instead of Contact object - mentions: (request.mentions as unknown) as string[], + mentions: request.mentions as unknown as string[], }; return message.edit(request.text, options); } @@ -368,7 +370,7 @@ export class WhatsappSessionWebJSCore extends WhatsappSession { const options = { quotedMessageId: request.reply_to, // It's fine to sent just ids instead of Contact object - mentions: (request.mentions as unknown) as string[], + mentions: request.mentions as unknown as string[], }; return this.whatsapp.sendMessage(request.chatId, request.text, options); } @@ -485,8 +487,17 @@ export class WhatsappSessionWebJSCore extends WhatsappSession { * Label methods */ - getLabels() { - return this.whatsapp.getLabels(); + public async getLabels(): Promise { + const labels = await this.whatsapp.getLabels(); + return labels.map(this.toLabel); + } + + protected toLabel(label: WEBJSLabel): Label { + return { + id: label.id, + name: label.name, + color: 0, + }; } /** diff --git a/src/structures/labels.dto.ts b/src/structures/labels.dto.ts new file mode 100644 index 00000000..2e935ead --- /dev/null +++ b/src/structures/labels.dto.ts @@ -0,0 +1,21 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class Label { + @ApiProperty({ + example: '1', + description: 'Label ID', + }) + id: string; + + @ApiProperty({ + example: 'Lead', + description: 'Label name', + }) + name: string; + + @ApiProperty({ + example: 0, + description: 'Internal color number, not hex', + }) + color: number; +}