Skip to content

Commit

Permalink
add route: link message
Browse files Browse the repository at this point in the history
  • Loading branch information
jrCleber committed Oct 20, 2024
1 parent f06a6b2 commit a057b12
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
"@adiwajshing/keyed-db": "^0.2.4",
"@hapi/boom": "^10.0.1",
"@prisma/client": "^5.19.1",
"@whiskeysockets/baileys": "^6.7.8",
"axios": "^1.7.7",
"baileys": "github:/jrCleber/Baileys",
"class-validator": "^0.14.0",
"cross-env": "^7.0.3",
"dayjs": "^1.11.13",
Expand Down
2 changes: 1 addition & 1 deletion src/utils/use-multi-file-auth-state-provider-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {
initAuthCreds,
proto,
SignalDataTypeMap,
} from 'baileys';
} from '@whiskeysockets/baileys';
import { Logger } from '../config/logger.config';
import { ConfigService } from '../config/env.config';
import { ProviderFiles } from '../provider/sessions';
Expand Down
23 changes: 23 additions & 0 deletions src/validate/validate.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
*/

import { JSONSchema7, JSONSchema7Definition } from 'json-schema';
import { title } from 'process';
import { ulid } from 'ulid';

const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => {
Expand Down Expand Up @@ -408,6 +409,28 @@ export const reactionMessageSchema: JSONSchema7 = {
required: ['reactionMessage'],
};

export const sendLinkSchema: JSONSchema7 = {
$id: ulid(),
type: 'object',
properties: {
number: { ...numberDefinition },
options: { ...optionsSchema },
linkMessage: {
type: 'object',
properties: {
text: { type: 'string' },
link: { type: 'string' },
description: { type: 'string' },
title: { type: 'string' },
thumbnailUrl: { type: 'string' },
},
required: ['link'],
...isNotEmpty('text', 'link', 'description', 'title', 'thumbnailUrl'),
},
},
required: ['linkMessage', 'number'],
};

// Chat Schema
export const whatsappNumberSchema: JSONSchema7 = {
$id: ulid(),
Expand Down
2 changes: 1 addition & 1 deletion src/whatsapp/controllers/instance.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* └──────────────────────────────────────────────────────────────────────────────┘
*/

import { delay } from 'baileys';
import { delay } from '@whiskeysockets/baileys';
import EventEmitter2 from 'eventemitter2';
import { ConfigService } from '../../config/env.config';
import {
Expand Down
5 changes: 5 additions & 0 deletions src/whatsapp/controllers/sendMessage.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
SendAudioDto,
SendButtonsDto,
SendContactDto,
SendLinkDto,
SendListDto,
SendListLegacyDto,
SendLocationDto,
Expand Down Expand Up @@ -138,4 +139,8 @@ export class SendMessageController {
public async sendListLegacy({ instanceName }: InstanceDto, data: SendListLegacyDto) {
return await this.waMonitor.waInstances.get(instanceName).listLegacy(data);
}

public async sendLinkPreview({ instanceName }: InstanceDto, data: SendLinkDto) {
return await this.waMonitor.waInstances.get(instanceName).linkMessage(data);
}
}
2 changes: 1 addition & 1 deletion src/whatsapp/dto/chat.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* └──────────────────────────────────────────────────────────────────────────────┘
*/

import { WAPresence } from 'baileys';
import { WAPresence } from '@whiskeysockets/baileys';

export class OnWhatsAppDto {
constructor(
Expand Down
14 changes: 13 additions & 1 deletion src/whatsapp/dto/sendMessage.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* └──────────────────────────────────────────────────────────────────────────────┘
*/

import { proto, WAPresence } from 'baileys';
import { proto, WAPresence } from '@whiskeysockets/baileys';
import { ulid } from 'ulid';

export class Options {
Expand Down Expand Up @@ -318,3 +318,15 @@ export class SendListLegacyDto extends Metadata {
this.listMessage = new ListLegacy(props.listMessage);
}
}

export class LinkMessage {
link: string;
thumbnailUrl?: string;
text?: string;
title?: string;
description?: string;
}

export class SendLinkDto extends Metadata {
linkMessage: LinkMessage;
}
13 changes: 12 additions & 1 deletion src/whatsapp/routers/sendMessage.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ import {
mediaFileMessageSchema,
mediaMessageSchema,
reactionMessageSchema,
sendLinkSchema,
textMessageSchema,
} from '../../validate/validate.schema';
import {
AudioMessageFileDto,
Button,
MediaFileDto,
SendAudioDto,
SendButtonsDto,
SendContactDto,
SendLinkDto,
SendListDto,
SendListLegacyDto,
SendLocationDto,
Expand Down Expand Up @@ -226,6 +227,16 @@ export function MessageRouter(
},
});

return res.status(HttpStatus.CREATED).json(response);
})
.post(routerPath('sendLink'), ...guards, async (req, res) => {
const response = await dataValidate<SendLinkDto>({
request: req,
schema: sendLinkSchema,
execute: (instance, data) =>
sendMessageController.sendLinkPreview(instance, data),
});

return res.status(HttpStatus.CREATED).json(response);
});

Expand Down
8 changes: 8 additions & 0 deletions src/whatsapp/services/instance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ export class InstanceService {
profilePicUrl: true,
createdAt: true,
updatedAt: true,
Auth: {
select: {
id: true,
token: true,
createdAt: true,
updatedAt: true,
},
},
Webhook: {
select: {
id: true,
Expand Down
33 changes: 32 additions & 1 deletion src/whatsapp/services/whatsapp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import makeWASocket, {
WAMessageUpdate,
WASocket,
WAVersion,
} from 'baileys';
} from '@whiskeysockets/baileys';
import {
ConfigService,
ConfigSessionPhone,
Expand Down Expand Up @@ -96,6 +96,7 @@ import {
SendAudioDto,
SendButtonsDto,
SendContactDto,
SendLinkDto,
SendListDto,
SendListLegacyDto,
SendLocationDto,
Expand Down Expand Up @@ -1630,6 +1631,36 @@ export class WAStartupService {
});
}

public async linkMessage(data: SendLinkDto) {
return await this.sendMessageWithTyping(data.number, {
extendedTextMessage: {
text: (() => {
if (data.linkMessage?.text) {
let t = data.linkMessage.link;
t += '\n\n';
t += data.linkMessage.text;
return t;
}
})(),
canonicalUrl: data.linkMessage.link,
matchedText: data.linkMessage?.text,
previewType: 0,
title: data.linkMessage?.title,
description: data.linkMessage?.description,
jpegThumbnail: await (async () => {
if (data.linkMessage?.thumbnailUrl) {
try {
const response = await axios.get(data.linkMessage.thumbnailUrl, {
responseType: 'arraybuffer',
});
return new Uint8Array(response.data);
} catch {}
}
})(),
},
});
}

// Chat Controller
public async whatsappNumber(data: WhatsAppNumberDto) {
const onWhatsapp: OnWhatsAppDto[] = [];
Expand Down

0 comments on commit a057b12

Please sign in to comment.