Skip to content

Commit

Permalink
[core] Add status for WEBJS
Browse files Browse the repository at this point in the history
  • Loading branch information
devlikepro committed Nov 4, 2024
1 parent 9969966 commit cf886de
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
32 changes: 32 additions & 0 deletions src/core/engines/webjs/WebjsClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GetChatMessagesFilter } from '@waha/structures/chats.dto';
import { Label } from '@waha/structures/labels.dto';
import { PaginationParams } from '@waha/structures/pagination.dto';
import { TextStatus } from '@waha/structures/status.dto';
import * as lodash from 'lodash';
import { Client } from 'whatsapp-web.js';
import { Message } from 'whatsapp-web.js/src/structures';
Expand Down Expand Up @@ -101,6 +102,37 @@ export class WebjsClient extends Client {
return chats.map((chat) => ChatFactory.create(this, chat));
}

async sendTextStatus(status: TextStatus) {
// Convert from hex to number
const waColor = 'FF' + status.backgroundColor.replace('#', '');
const color = parseInt(waColor, 16);

const textStatus = {
text: status.text,
color: color,
font: status.font,
};
const sentMsg = await this.pupPage.evaluate(async (status) => {
// @ts-ignore
await window.Store.SendStatus.sendStatusTextMsgAction(status);
// @ts-ignore
const meUser = window.Store.User.getMeUser();
// @ts-ignore
const myStatus = window.Store.Status.getModelsArray().findLast(
(x) => x.id == meUser,
);
if (!myStatus) {
return undefined;
}
// @ts-ignore
const msg = myStatus.msgs.last();
// @ts-ignore
return msg ? window.WWebJS.getMessageModel(msg) : undefined;
}, textStatus);

return sentMsg ? new Message(this, sentMsg) : undefined;
}

async getMessages(
chatId: string,
filter: GetChatMessagesFilter,
Expand Down
23 changes: 23 additions & 0 deletions src/core/engines/webjs/session.webjs.core.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { UnprocessableEntityException } from '@nestjs/common';
import {
getChannelInviteLink,
WAHAInternalEvent,
WhatsappSession,
} from '@waha/core/abc/session.abc';
import { toJID } from '@waha/core/engines/noweb/session.noweb.core';
import { LocalAuth } from '@waha/core/engines/webjs/LocalAuth';
import { WebjsClient } from '@waha/core/engines/webjs/WebjsClient';
import {
Expand Down Expand Up @@ -63,6 +65,11 @@ import { ReplyToMessage } from '@waha/structures/message.dto';
import { PaginationParams } from '@waha/structures/pagination.dto';
import { WAMessage, WAMessageReaction } from '@waha/structures/responses.dto';
import { MeInfo } from '@waha/structures/sessions.dto';
import {
BROADCAST_ID,
StatusRequest,
TextStatus,
} from '@waha/structures/status.dto';
import { WAMessageRevokedBody } from '@waha/structures/webhooks.dto';
import { PaginatorInMemory } from '@waha/utils/Paginator';
import { sleep, waitUntil } from '@waha/utils/promiseTimeout';
Expand Down Expand Up @@ -968,6 +975,22 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
}
}

/**
* Status methods
*/
protected checkStatusRequest(request: StatusRequest) {
if (request.contacts?.length > 0) {
const msg =
"WEBJS doesn't accept 'contacts'. Remove the field to send status to all contacts.";
throw new UnprocessableEntityException(msg);
}
}

public sendTextStatus(status: TextStatus) {
this.checkStatusRequest(status);
return this.whatsapp.sendTextStatus(status);
}

/**
* END - Methods for API
*/
Expand Down
5 changes: 2 additions & 3 deletions src/structures/status.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import {

export const BROADCAST_ID = 'status@broadcast';

class StatusRequest {
export class StatusRequest {
@ApiProperty({
description:
'it is always necessary to inform the list of contacts that will have access to the posted status',
description: 'Contact list to send the status to.',
example: ['[email protected]'],
})
contacts = ['[email protected]'];
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12384,7 +12384,7 @@ __metadata:

"whatsapp-web.js@github:devlikeapro/whatsapp-web.js#fork-main-channels":
version: 1.26.0
resolution: "whatsapp-web.js@https://github.com/devlikeapro/whatsapp-web.js.git#commit=76329eedc27b07c11e38bf1b60e6c2476c11dd73"
resolution: "whatsapp-web.js@https://github.com/devlikeapro/whatsapp-web.js.git#commit=aa90f7378659a237f30cda6d368359a1f81904dc"
dependencies:
"@pedroslopez/moduleraid": ^5.0.2
archiver: ^5.3.1
Expand All @@ -12402,7 +12402,7 @@ __metadata:
optional: true
unzipper:
optional: true
checksum: b8d4fcb1fd1365a38f07f95dd2eb1fd743c6c406fc7ac679662ddeded80c441720bc8400222a37d61765ebcbb5c6799f169d5ac45f12f58b5421c55bb9f2ab7e
checksum: 7c914e9cdd4a25c967e057d0cb5019973f0556f6220320e4bcbc0666669801b92abf97c2cc783762ef462d0006123953e233136f457689ebac395260fe69a5fc
languageName: node
linkType: hard

Expand Down

0 comments on commit cf886de

Please sign in to comment.