[core] API - mentions for send image, file, video
This commit is contained in:
1 parent
fd2b22b339
commit
869a343e43
4 files changed
+60
-22
No files matched your search
@@ -5,7 +5,6 @@ import {
|
||||
Post,
|
||||
Put,
|
||||
Query,
|
||||
UnprocessableEntityException,
|
||||
UsePipes,
|
||||
ValidationPipe,
|
||||
} from '@nestjs/common';
|
||||
@@ -45,20 +44,10 @@ import {
|
||||
WANumberExistResult,
|
||||
} from '../structures/chatting.dto';
|
||||
import { WAMessage } from '../structures/responses.dto';
|
||||
import { isJidGroup } from '@waha/core/utils/jids';
|
||||
|
||||
function validateRequestMentions(request: MessageTextRequest) {
|
||||
if (!isJidGroup(request.chatId)) {
|
||||
throw new UnprocessableEntityException(
|
||||
`"mentions":["all"] can be used only in group chats, not in '${request.chatId}'`,
|
||||
);
|
||||
}
|
||||
if (request.mentions.length > 1) {
|
||||
throw new UnprocessableEntityException(
|
||||
`"mentions":["all"] cannot be used with other mentions`,
|
||||
);
|
||||
}
|
||||
}
|
||||
import {
|
||||
mentionsAll,
|
||||
validateRequestMentions,
|
||||
} from '@waha/core/utils/mentions.all';
|
||||
|
||||
@ApiSecurity('api_key')
|
||||
@Controller('api')
|
||||
@@ -70,14 +59,9 @@ export class ChattingController {
|
||||
@ApiOperation({ summary: 'Send a text message' })
|
||||
async sendText(@Body() request: MessageTextRequest): Promise<WAMessage> {
|
||||
const whatsapp = await this.manager.getWorkingSession(request.session);
|
||||
if (request.mentions && request.mentions.includes('all')) {
|
||||
if (mentionsAll(request)) {
|
||||
validateRequestMentions(request);
|
||||
const participants = await whatsapp.getGroupParticipants(request.chatId);
|
||||
let mentions = participants.map((p) => p.id);
|
||||
// Exclude my ids
|
||||
const me = whatsapp.getSessionMeInfo();
|
||||
mentions = mentions.filter((id) => id !== me.id && id !== me.lid);
|
||||
request.mentions = mentions;
|
||||
request.mentions = await whatsapp.resolveMentionsAll(request.chatId);
|
||||
}
|
||||
return whatsapp.sendText(request);
|
||||
}
|
||||
@@ -90,6 +74,10 @@ export class ChattingController {
|
||||
})
|
||||
async sendImage(@Body() request: MessageImageRequest) {
|
||||
const whatsapp = await this.manager.getWorkingSession(request.session);
|
||||
if (mentionsAll(request)) {
|
||||
validateRequestMentions(request);
|
||||
request.mentions = await whatsapp.resolveMentionsAll(request.chatId);
|
||||
}
|
||||
return whatsapp.sendImage(request);
|
||||
}
|
||||
|
||||
@@ -101,6 +89,10 @@ export class ChattingController {
|
||||
})
|
||||
async sendFile(@Body() request: MessageFileRequest) {
|
||||
const whatsapp = await this.manager.getWorkingSession(request.session);
|
||||
if (mentionsAll(request)) {
|
||||
validateRequestMentions(request);
|
||||
request.mentions = await whatsapp.resolveMentionsAll(request.chatId);
|
||||
}
|
||||
return whatsapp.sendFile(request);
|
||||
}
|
||||
|
||||
@@ -123,6 +115,10 @@ export class ChattingController {
|
||||
})
|
||||
async sendVideo(@Body() request: MessageVideoRequest) {
|
||||
const whatsapp = await this.manager.getWorkingSession(request.session);
|
||||
if (mentionsAll(request)) {
|
||||
validateRequestMentions(request);
|
||||
request.mentions = await whatsapp.resolveMentionsAll(request.chatId);
|
||||
}
|
||||
return whatsapp.sendVideo(request);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import { MessagesForRead } from '@waha/core/utils/convertors';
|
||||
import {
|
||||
IgnoreJidConfig,
|
||||
isJidBroadcast,
|
||||
isJidGroup,
|
||||
isJidNewsletter,
|
||||
isNullJid,
|
||||
JidFilter,
|
||||
@@ -1198,6 +1199,14 @@ export abstract class WhatsappSession {
|
||||
public fetch(url: string): Promise<Buffer> {
|
||||
return fetchBuffer(url);
|
||||
}
|
||||
|
||||
public async resolveMentionsAll(chatId: string): Promise<string[]> {
|
||||
const participants = await this.getGroupParticipants(chatId);
|
||||
let mentions = participants.map((p) => p.id);
|
||||
// Exclude my ids
|
||||
const me = this.getSessionMeInfo();
|
||||
return mentions.filter((id) => id !== me.id && id !== me.lid);
|
||||
}
|
||||
}
|
||||
|
||||
export function getGroupInviteLink(code: string) {
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { isJidGroup } from '@waha/core/utils/jids';
|
||||
import { UnprocessableEntityException } from '@nestjs/common';
|
||||
|
||||
const ALL = 'all';
|
||||
|
||||
export function mentionsAll(request: { mentions?: string[] }) {
|
||||
return request.mentions && request.mentions.includes(ALL);
|
||||
}
|
||||
|
||||
export function validateRequestMentions(request: {
|
||||
chatId: string;
|
||||
mentions?: string[];
|
||||
}) {
|
||||
if (!isJidGroup(request.chatId)) {
|
||||
throw new UnprocessableEntityException(
|
||||
`"mentions":["all"] can be used only in group chats, not in '${request.chatId}'`,
|
||||
);
|
||||
}
|
||||
if (request.mentions.length > 1) {
|
||||
throw new UnprocessableEntityException(
|
||||
`"mentions":["all"] cannot be used with other mentions`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -303,6 +303,9 @@ class FileRequest extends ChatRequest {
|
||||
export class MessageImageRequest extends FileRequest {
|
||||
caption?: string;
|
||||
|
||||
@ApiHideProperty()
|
||||
mentions?: string[];
|
||||
|
||||
@ReplyToProperty()
|
||||
reply_to?: string;
|
||||
}
|
||||
@@ -310,6 +313,9 @@ export class MessageImageRequest extends FileRequest {
|
||||
export class MessageFileRequest extends FileRequest {
|
||||
caption?: string;
|
||||
|
||||
@ApiHideProperty()
|
||||
mentions?: string[];
|
||||
|
||||
@ReplyToProperty()
|
||||
reply_to?: string;
|
||||
}
|
||||
@@ -343,6 +349,9 @@ export class MessageVideoRequest extends ChatRequest {
|
||||
|
||||
caption?: string = 'Just watch at this!';
|
||||
|
||||
@ApiHideProperty()
|
||||
mentions?: string[];
|
||||
|
||||
@ApiProperty({
|
||||
description:
|
||||
'The ID of the message to reply to - false_11111111111@c.us_AAAAAAAAAAAAAAAAAAAA',
|
||||
|
||||
Reference in new issue
Block a user