Spaces:
Runtime error
Runtime error
File size: 13,416 Bytes
4327358 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
import { exposeFunctionIfAbsent } from '@waha/core/engines/webjs/Puppeteer';
import { WebJSPresence } from '@waha/core/engines/webjs/types';
import { GetChatMessagesFilter } from '@waha/structures/chats.dto';
import { Label } from '@waha/structures/labels.dto';
import { LidToPhoneNumber } from '@waha/structures/lids.dto';
import { PaginationParams } from '@waha/structures/pagination.dto';
import { TextStatus } from '@waha/structures/status.dto';
import { sleep } from '@waha/utils/promiseTimeout';
import { EventEmitter } from 'events';
import * as lodash from 'lodash';
import { Page } from 'puppeteer';
import { Client, Events } from 'whatsapp-web.js';
import { Message } from 'whatsapp-web.js/src/structures';
import { CallErrorEvent, PAGE_CALL_ERROR_EVENT, WPage } from './WPage';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { LoadWAHA } = require('./_WAHA.js');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { LoadLodash } = require('./_lodash.js');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { LoadPaginator } = require('./_Paginator.js');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const ChatFactory = require('whatsapp-web.js/src/factories/ChatFactory');
export class WebjsClientCore extends Client {
public events = new EventEmitter();
private wpage: WPage = null;
constructor(
options,
protected tags: boolean,
) {
super(options);
// Wait until it's READY and inject more utils
this.on(Events.READY, async () => {
await this.attachCustomEventListeners();
await this.injectWaha();
});
}
async initialize() {
const result = await super.initialize();
if (this.pupPage && !(this.pupPage instanceof WPage)) {
this.wpage = new WPage(this.pupPage);
this.wpage.on(PAGE_CALL_ERROR_EVENT as any, (event: CallErrorEvent) => {
this.events.emit(PAGE_CALL_ERROR_EVENT as any, event);
});
this.pupPage = this.wpage as any as Page;
}
return result;
}
async injectWaha() {
await this.pupPage.evaluate(LoadLodash);
await this.pupPage.evaluate(LoadPaginator);
await this.pupPage.evaluate(LoadWAHA);
}
/**
* @result indicating whether the UX fresh look was successfully hidden.
*/
hideUXFreshLook(): Promise<boolean> {
return this.pupPage.evaluate(() => {
const WAWebUserPrefsUiRefresh = window.require('WAWebUserPrefsUiRefresh');
if (!WAWebUserPrefsUiRefresh) {
return false;
}
if (WAWebUserPrefsUiRefresh.getUiRefreshNuxAcked()) {
return false;
}
WAWebUserPrefsUiRefresh.incrementNuxViewCount();
WAWebUserPrefsUiRefresh.setUiRefreshNuxAcked(true);
const WAWebModalManager = window.require('WAWebModalManager');
WAWebModalManager.ModalManager.close();
return true;
});
}
async attachCustomEventListeners() {
await exposeFunctionIfAbsent(
this.pupPage,
'onNewMessageId',
(messageId: string) => {
this.events.emit('message.id', { id: messageId });
return;
},
);
if (this.tags) {
await this.attachTagsEvents();
}
}
async attachTagsEvents() {
await this.pupPage.evaluate(() => {
// @ts-ignore
if (window.decodeStanzaBack) {
return;
}
const tags = ['receipt', 'presence', 'chatstate'];
// @ts-ignore
window.decodeStanzaBack = window.Store.SocketWap.decodeStanza;
// @ts-ignore
window.Store.SocketWap.decodeStanza = async (...args) => {
// @ts-ignore
const result = await window.decodeStanzaBack(...args);
if (tags.includes(result?.tag)) {
// @ts-ignore
setTimeout(() => window.onTag(result), 0);
}
return result;
};
});
}
async destroy() {
this.events.removeAllListeners();
this.wpage?.removeAllListeners();
await super.destroy();
}
async setPushName(name: string) {
await this.pupPage.evaluate(async (pushName) => {
return await window['WAHA'].WAWebSetPushnameConnAction.setPushname(
pushName,
);
}, name);
if (this.info) {
this.info.pushname = name;
}
}
async unpair() {
await this.pupPage.evaluate(async () => {
if (
// @ts-ignore
window.Store &&
// @ts-ignore
window.Store.AppState &&
// @ts-ignore
typeof window.Store.AppState.logout === 'function'
) {
// @ts-ignore
await window.Store.AppState.logout();
}
});
}
async createLabel(name: string, color: number): Promise<number> {
const labelId: number = (await this.pupPage.evaluate(
async (name, color) => {
// @ts-ignore
return await window.WAHA.WAWebBizLabelEditingAction.labelAddAction(
name,
color,
);
},
name,
color,
)) as any;
return labelId;
}
async deleteLabel(label: Label) {
return await this.pupPage.evaluate(async (label) => {
// @ts-ignore
return await window.WAHA.WAWebBizLabelEditingAction.labelDeleteAction(
label.id,
label.name,
label.color,
);
}, label);
}
async updateLabel(label: Label) {
return await this.pupPage.evaluate(async (label) => {
// @ts-ignore
return await window.WAHA.WAWebBizLabelEditingAction.labelEditAction(
label.id,
label.name,
undefined, // predefinedId
label.color,
);
}, label);
}
async getChats(pagination?: PaginationParams, filter?: { ids?: string[] }) {
if (lodash.isEmpty(pagination)) {
return await super.getChats();
}
// Get paginated chats
pagination.limit ||= Infinity;
pagination.offset ||= 0;
const chats = await this.pupPage.evaluate(
async (pagination, filter) => {
// @ts-ignore
return await window.WAHA.getChats(pagination, filter);
},
pagination,
filter,
);
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,
pagination: PaginationParams,
) {
const messages = await this.pupPage.evaluate(
async (chatId, filter, pagination) => {
pagination.limit ||= Infinity;
pagination.offset ||= 0;
const msgFilter = (m) => {
if (m.isNotification) {
return false;
}
if (
filter['filter.fromMe'] != null &&
m.id.fromMe !== filter['filter.fromMe']
) {
return false;
}
if (
filter['filter.timestamp.gte'] != null &&
m.t < filter['filter.timestamp.gte']
) {
return false;
}
if (
filter['filter.timestamp.lte'] != null &&
m.t > filter['filter.timestamp.lte']
) {
return false;
}
if (filter['filter.ack'] != null && m.ack !== filter['filter.ack']) {
return false;
}
return true;
};
// @ts-ignore
const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
let msgs = chat.msgs.getModelsArray().filter(msgFilter);
while (msgs.length < pagination.limit + pagination.offset) {
const loadedMessages =
// @ts-ignore
await window.Store.ConversationMsgs.loadEarlierMsgs(chat);
if (!loadedMessages || loadedMessages.length == 0) break;
msgs = [...loadedMessages.filter(msgFilter), ...msgs];
msgs = msgs.sort((a, b) => b.t - a.t);
// Check if the earliest message is already outside the timerange filter
const earliest = msgs[msgs.length - 1];
if (earliest.t < (filter['filter.timestamp.gte'] || Infinity)) {
// Add only messages that pass the filter and stop loading more
break;
}
}
if (msgs.length > pagination.limit + pagination.offset) {
// sort by t - new first
msgs = msgs.sort((a, b) => b.t - a.t);
msgs = msgs.slice(
pagination.offset,
pagination.limit + pagination.offset,
);
}
// @ts-ignore
return msgs.map((m) => window.WWebJS.getMessageModel(m));
},
chatId,
filter,
pagination,
);
return messages.map((m) => new Message(this, m));
}
public async getAllLids(
pagination: PaginationParams,
): Promise<Array<LidToPhoneNumber>> {
const lids: Array<LidToPhoneNumber> = (await this.pupPage.evaluate(
async (pagination) => {
pagination.limit ||= Infinity;
pagination.offset ||= 0;
pagination.sortBy ||= 'lid';
// @ts-ignore
const WAWebApiContact = window.Store.LidUtils;
await WAWebApiContact.warmUpAllLidPnMappings();
const lidMap = WAWebApiContact.lidPnCache['$1'];
const values = Array.from(lidMap.values());
const result = values.map((map) => {
return {
// @ts-ignore
lid: map.lid._serialized,
// @ts-ignore
pn: map.phoneNumber._serialized,
};
});
// @ts-ignore
const paginator = new window.Paginator(pagination);
const page = paginator.apply(result);
return page;
},
pagination,
)) as any;
return lids;
}
public async getLidsCount(): Promise<number> {
const count: number = (await this.pupPage.evaluate(async () => {
// @ts-ignore
const WAWebApiContact = window.Store.LidUtils;
await WAWebApiContact.warmUpAllLidPnMappings();
const lidMap = WAWebApiContact.lidPnCache['$1'];
return lidMap.size;
})) as any;
return count;
}
public async findPNByLid(lid: string): Promise<string> {
const pn = await this.pupPage.evaluate(async (lid) => {
// @ts-ignore
const WAWebApiContact = window.Store.LidUtils;
// @ts-ignore
const WAWebWidFactory = window.Store.WidFactory;
const wid = WAWebWidFactory.createWid(lid);
const result = WAWebApiContact.getPhoneNumber(wid);
return result ? result._serialized : null;
}, lid);
return pn;
}
public async findLIDByPhoneNumber(phoneNumber: string): Promise<string> {
const lid: string = (await this.pupPage.evaluate(async (pn) => {
// @ts-ignore
const WAWebApiContact = window.Store.LidUtils;
// @ts-ignore
const WAWebWidFactory = window.Store.WidFactory;
const wid = WAWebWidFactory.createWid(pn);
const result = WAWebApiContact.getCurrentLid(wid);
return result ? result._serialized : null;
}, phoneNumber)) as any;
return lid;
}
/**
* Presences methods
*/
public async subscribePresence(chatId: string): Promise<void> {
await this.pupPage.evaluate(async (chatId) => {
const d = require;
const WidFactory = d('WAWebWidFactory');
const wid = WidFactory.createWidFromWidLike(chatId);
const chat = d('WAWebChatCollection').ChatCollection.get(wid);
const tc = chat == null ? void 0 : chat.getTcToken();
await d('WAWebContactPresenceBridge').subscribePresence(wid, tc);
}, chatId);
}
private async getCurrentPresence(chatId: string): Promise<WebJSPresence[]> {
const result = await this.pupPage.evaluate(async (chatId) => {
const d = require;
const WidFactory = d('WAWebWidFactory');
const PresenceCollection = d(
'WAWebPresenceCollection',
).PresenceCollection;
const wid = WidFactory.createWidFromWidLike(chatId);
const presence = PresenceCollection.get(wid);
if (!presence) {
return [];
}
let chatstates = [];
if (chatId.endsWith('@c.us')) {
chatstates = [presence.chatstate];
} else {
chatstates = presence.chatstates.getModelsArray();
}
return chatstates.map((chatstate) => {
return {
participant: chatstate.id._serialized,
lastSeen: chatstate.t,
state: chatstate.type,
};
});
}, chatId);
return result;
}
public async getPresence(chatId: string): Promise<WebJSPresence[]> {
await this.sendPresenceAvailable();
await this.subscribePresence(chatId);
await sleep(3_000);
return await this.getCurrentPresence(chatId);
}
}
|