Skip to content

Commit 1980f43

Browse files
committed
feat: send ptv message
1 parent d9b1385 commit 1980f43

File tree

5 files changed

+92
-3
lines changed

5 files changed

+92
-3
lines changed

src/api/controllers/sendMessage.controller.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
SendLocationDto,
88
SendMediaDto,
99
SendPollDto,
10+
SendPtvDto,
1011
SendReactionDto,
1112
SendStatusDto,
1213
SendStickerDto,
@@ -39,6 +40,13 @@ export class SendMessageController {
3940
throw new BadRequestException('Owned media must be a url or base64');
4041
}
4142

43+
public async sendPtv({ instanceName }: InstanceDto, data: SendPtvDto, file?: any) {
44+
if (file || isURL(data?.video) || isBase64(data?.video)) {
45+
return await this.waMonitor.waInstances[instanceName].ptvMessage(data, file);
46+
}
47+
throw new BadRequestException('Owned media must be a url or base64');
48+
}
49+
4250
public async sendSticker({ instanceName }: InstanceDto, data: SendStickerDto, file?: any) {
4351
if (file || isURL(data.sticker) || isBase64(data.sticker)) {
4452
return await this.waMonitor.waInstances[instanceName].mediaSticker(data, file);

src/api/dto/sendMessage.dto.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class SendPollDto extends Metadata {
7070
messageSecret?: Uint8Array;
7171
}
7272

73-
export type MediaType = 'image' | 'document' | 'video' | 'audio';
73+
export type MediaType = 'image' | 'document' | 'video' | 'audio' | 'ptv';
7474

7575
export class SendMediaDto extends Metadata {
7676
mediatype: MediaType;
@@ -82,6 +82,10 @@ export class SendMediaDto extends Metadata {
8282
media: string;
8383
}
8484

85+
export class SendPtvDto extends Metadata {
86+
video: string;
87+
}
88+
8589
export class SendStickerDto extends Metadata {
8690
sticker: string;
8791
}

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
SendLocationDto,
4343
SendMediaDto,
4444
SendPollDto,
45+
SendPtvDto,
4546
SendReactionDto,
4647
SendStatusDto,
4748
SendStickerDto,
@@ -271,7 +272,7 @@ export class BaileysStartupService extends ChannelStartupService {
271272
qrcodeTerminal.generate(qr, { small: true }, (qrcode) =>
272273
this.logger.log(
273274
`\n{ instance: ${this.instance.name} pairingCode: ${this.instance.qrcode.pairingCode}, qrcodeCount: ${this.instance.qrcode.count} }\n` +
274-
qrcode,
275+
qrcode,
275276
),
276277
);
277278

@@ -1983,9 +1984,11 @@ export class BaileysStartupService extends ChannelStartupService {
19831984

19841985
private async prepareMediaMessage(mediaMessage: MediaMessage) {
19851986
try {
1987+
const type = mediaMessage.mediatype === 'ptv' ? 'video' : mediaMessage.mediatype;
1988+
19861989
const prepareMedia = await prepareWAMessageMedia(
19871990
{
1988-
[mediaMessage.mediatype]: isURL(mediaMessage.media)
1991+
[type]: isURL(mediaMessage.media)
19891992
? { url: mediaMessage.media }
19901993
: Buffer.from(mediaMessage.media, 'base64'),
19911994
} as any,
@@ -2039,6 +2042,10 @@ export class BaileysStartupService extends ChannelStartupService {
20392042
}
20402043
}
20412044

2045+
if (mediaMessage.mediatype === 'ptv') {
2046+
prepareMedia[mediaType] = prepareMedia[type + 'Message'];
2047+
}
2048+
20422049
prepareMedia[mediaType].caption = mediaMessage?.caption;
20432050
prepareMedia[mediaType].mimetype = mimetype;
20442051
prepareMedia[mediaType].fileName = mediaMessage.fileName;
@@ -2147,6 +2154,36 @@ export class BaileysStartupService extends ChannelStartupService {
21472154
);
21482155
}
21492156

2157+
public async ptvMessage(data: SendPtvDto, file?: any, isIntegration = false) {
2158+
const mediaData: SendMediaDto = {
2159+
number: data.number,
2160+
media: data.video,
2161+
mediatype: 'ptv',
2162+
delay: data?.delay,
2163+
quoted: data?.quoted,
2164+
mentionsEveryOne: data?.mentionsEveryOne,
2165+
mentioned: data?.mentioned,
2166+
};
2167+
2168+
if (file) mediaData.media = file.buffer.toString('base64');
2169+
2170+
const generate = await this.prepareMediaMessage(mediaData);
2171+
2172+
const mediaSent = await this.sendMessageWithTyping(
2173+
data.number,
2174+
{ ...generate.message },
2175+
{
2176+
delay: data?.delay,
2177+
presence: 'composing',
2178+
quoted: data?.quoted,
2179+
mentionsEveryOne: data?.mentionsEveryOne,
2180+
mentioned: data?.mentioned,
2181+
},
2182+
);
2183+
2184+
return mediaSent;
2185+
}
2186+
21502187
public async audioWhatsapp(data: SendAudioDto, file?: any) {
21512188
const mediaData: SendAudioDto = { ...data };
21522189

src/api/routes/sendMessage.router.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
SendLocationDto,
88
SendMediaDto,
99
SendPollDto,
10+
SendPtvDto,
1011
SendReactionDto,
1112
SendStatusDto,
1213
SendStickerDto,
@@ -22,6 +23,7 @@ import {
2223
locationMessageSchema,
2324
mediaMessageSchema,
2425
pollMessageSchema,
26+
ptvMessageSchema,
2527
reactionMessageSchema,
2628
statusMessageSchema,
2729
stickerMessageSchema,
@@ -71,6 +73,18 @@ export class MessageRouter extends RouterBroker {
7173

7274
return res.status(HttpStatus.CREATED).json(response);
7375
})
76+
.post(this.routerPath('sendPtv'), ...guards, upload.single('file'), async (req, res) => {
77+
const bodyData = req.body;
78+
79+
const response = await this.dataValidate<SendPtvDto>({
80+
request: req,
81+
schema: ptvMessageSchema,
82+
ClassRef: SendPtvDto,
83+
execute: (instance) => sendMessageController.sendPtv(instance, bodyData, req.file as any),
84+
});
85+
86+
return res.status(HttpStatus.CREATED).json(response);
87+
})
7488
.post(this.routerPath('sendWhatsAppAudio'), ...guards, upload.single('file'), async (req, res) => {
7589
const bodyData = req.body;
7690

src/validate/message.schema.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,32 @@ export const mediaMessageSchema: JSONSchema7 = {
111111
required: ['number', 'mediatype'],
112112
};
113113

114+
export const ptvMessageSchema: JSONSchema7 = {
115+
$id: v4(),
116+
type: 'object',
117+
properties: {
118+
number: { ...numberDefinition },
119+
video: { type: 'string' },
120+
delay: {
121+
type: 'integer',
122+
description: 'Enter a value in milliseconds',
123+
},
124+
quoted: { ...quotedOptionsSchema },
125+
everyOne: { type: 'boolean', enum: [true, false] },
126+
mentioned: {
127+
type: 'array',
128+
minItems: 1,
129+
uniqueItems: true,
130+
items: {
131+
type: 'string',
132+
pattern: '^\\d+',
133+
description: '"mentioned" must be an array of numeric strings',
134+
},
135+
},
136+
},
137+
required: ['number'],
138+
};
139+
114140
export const audioMessageSchema: JSONSchema7 = {
115141
$id: v4(),
116142
type: 'object',

0 commit comments

Comments
 (0)