Skip to content

Commit 4bf4b4a

Browse files
committed
feat: Route to send status broadcast
1 parent 9604f5c commit 4bf4b4a

3 files changed

Lines changed: 99 additions & 16 deletions

File tree

src/validate/validate.schema.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ export const statusMessageSchema: JSONSchema7 = {
196196
statusMessage: {
197197
type: 'object',
198198
properties: {
199-
text: { type: 'string' },
199+
type: { type: 'string', enum: ['text', 'image', 'audio', 'video'] },
200+
content: { type: 'string' },
201+
caption: { type: 'string' },
200202
backgroundColor: { type: 'string' },
201203
font: { type: 'integer', minimum: 0, maximum: 5 },
202204
statusJidList: {
@@ -210,8 +212,8 @@ export const statusMessageSchema: JSONSchema7 = {
210212
},
211213
},
212214
},
213-
required: ['text', 'backgroundColor', 'font', 'statusJidList'],
214-
...isNotEmpty('text', 'backgroundColor', 'font', 'statusJidList'),
215+
required: ['type', 'content', 'statusJidList'],
216+
...isNotEmpty('type', 'content', 'statusJidList'),
215217
},
216218
},
217219
required: ['statusMessage'],

src/whatsapp/dto/sendMessage.dto.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ class linkPreviewMessage {
3232
text: string;
3333
}
3434

35-
class StatusMessage {
36-
text: string;
37-
backgroundColor: string;
38-
font: number;
35+
export class StatusMessage {
36+
type: string;
37+
content: string;
3938
statusJidList: string[];
39+
caption?: string;
40+
backgroundColor?: string;
41+
font?: number;
4042
}
4143

4244
class PollMessage {

src/whatsapp/services/whatsapp.service.ts

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import {
7878
SendLinkPreviewDto,
7979
SendStickerDto,
8080
SendStatusDto,
81+
StatusMessage,
8182
} from '../dto/sendMessage.dto';
8283
import { arrayUnique, isBase64, isURL } from 'class-validator';
8384
import {
@@ -1236,7 +1237,7 @@ export class WAStartupService {
12361237
!message['poll'] &&
12371238
!message['linkPreview'] &&
12381239
!message['sticker'] &&
1239-
!message['status']
1240+
!sender.includes('@broadcast')
12401241
) {
12411242
if (!message['audio']) {
12421243
this.logger.verbose('Sending message');
@@ -1265,17 +1266,16 @@ export class WAStartupService {
12651266
);
12661267
}
12671268

1268-
if (message['status']) {
1269+
if (sender.includes('@broadcast')) {
12691270
this.logger.verbose('Sending message');
1271+
console.log(message['status']);
12701272
return await this.client.sendMessage(
12711273
sender,
1274+
message['status'].content as unknown as AnyMessageContent,
12721275
{
1273-
text: message['status'].text,
1274-
} as unknown as AnyMessageContent,
1275-
{
1276-
backgroundColor: message['status'].backgroundColor,
1277-
font: message['status'].font,
1278-
statusJidList: message['status'].statusJidList,
1276+
backgroundColor: message['status'].option.backgroundColor,
1277+
font: message['status'].option.font,
1278+
statusJidList: message['status'].option.statusJidList,
12791279
} as unknown as MiscMessageGenerationOptions,
12801280
);
12811281
}
@@ -1357,10 +1357,89 @@ export class WAStartupService {
13571357
);
13581358
}
13591359

1360+
private async formatStatusMessage(status: StatusMessage) {
1361+
if (!status.type) {
1362+
throw new BadRequestException('Type is required');
1363+
}
1364+
1365+
if (!status.content) {
1366+
throw new BadRequestException('Content is required');
1367+
}
1368+
1369+
if (
1370+
!status.statusJidList ||
1371+
!Array.isArray(status.statusJidList) ||
1372+
!status.statusJidList.length
1373+
) {
1374+
throw new BadRequestException('Status jid list is required');
1375+
}
1376+
1377+
if (status.type === 'text') {
1378+
if (!status.backgroundColor) {
1379+
throw new BadRequestException('Background color is required');
1380+
}
1381+
1382+
if (!status.font) {
1383+
throw new BadRequestException('Font is required');
1384+
}
1385+
1386+
return {
1387+
content: {
1388+
text: status.content,
1389+
},
1390+
option: {
1391+
backgroundColor: status.backgroundColor,
1392+
font: status.font,
1393+
statusJidList: status.statusJidList,
1394+
},
1395+
};
1396+
}
1397+
if (status.type === 'image') {
1398+
return {
1399+
content: {
1400+
image: {
1401+
url: status.content,
1402+
},
1403+
caption: status.caption,
1404+
},
1405+
option: {
1406+
statusJidList: status.statusJidList,
1407+
},
1408+
};
1409+
}
1410+
if (status.type === 'video') {
1411+
return {
1412+
content: {
1413+
video: {
1414+
url: status.content,
1415+
},
1416+
caption: status.caption,
1417+
},
1418+
option: {
1419+
statusJidList: status.statusJidList,
1420+
},
1421+
};
1422+
}
1423+
if (status.type === 'audio') {
1424+
return {
1425+
content: {
1426+
audio: {
1427+
url: status.content,
1428+
},
1429+
},
1430+
option: {
1431+
statusJidList: status.statusJidList,
1432+
},
1433+
};
1434+
}
1435+
1436+
throw new BadRequestException('Type not found');
1437+
}
1438+
13601439
public async statusMessage(data: SendStatusDto) {
13611440
this.logger.verbose('Sending status message');
13621441
return await this.sendMessageWithTyping('status@broadcast', {
1363-
status: data.statusMessage,
1442+
status: await this.formatStatusMessage(data.statusMessage),
13641443
});
13651444
}
13661445

0 commit comments

Comments
 (0)