Get Stories (#3294)

* Get all broadcast

* fixed return msgs

* feat: add usage example

* Update src/structures/Broadcast.js

Co-authored-by: alechkos <93551621+alechkos@users.noreply.github.com>

---------

Co-authored-by: alechkos <93551621+alechkos@users.noreply.github.com>
This commit is contained in:
tofers 2024-09-30 20:33:31 +03:00 • committed by GitHub
commit 5b0e139ad4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 125 additions and 2 deletions

View file

@ -465,6 +465,11 @@ client.on('message', async msg => {
// const isSynced = await chat.syncHistory();
await msg.reply(isSynced ? 'Historical chat is syncing..' : 'There is no historical chat to sync.');
} else if (msg.body === '!statuses') {
const statuses = await client.getBroadcasts();
console.log(statuses);
const chat = await statuses[0]?.getChat(); // Get user chat of a first status
console.log(chat);
}
});

25
index.d.ts vendored
View file

@ -75,6 +75,9 @@ declare namespace WAWebJS {
/** Get all current Labels */
getLabels(): Promise<Label[]>
/** Get all current Broadcasts */
getBroadcasts(): Promise<Broadcast[]>
/** Change labels in chats */
addOrRemoveLabels(labelIds: Array<number|string>, chatIds: Array<string>): Promise<void>
@ -1089,6 +1092,28 @@ declare namespace WAWebJS {
getChats: () => Promise<Chat[]>
}
export interface Broadcast {
/** Chat Object ID */
id: {
server: string,
user: string,
_serialized: string
},
/** Unix timestamp of last story */
timestamp: number,
/** Number of available statuses */
totalCount: number,
/** Number of not viewed */
unreadCount: number,
/** Unix timestamp of last story */
msgs: Message[],
/** Returns the Chat of the owner of the story */
getChat: () => Promise<Chat>,
/** Returns the Contact of the owner of the story */
getContact: () => Promise<Contact>,
}
/** Options for sending a message */
export interface MessageSendOptions {
/** Show links preview. Has no effect on multi-device accounts. */

View file

@ -22,6 +22,7 @@ module.exports = {
ProductMetadata: require('./src/structures/ProductMetadata'),
List: require('./src/structures/List'),
Buttons: require('./src/structures/Buttons'),
Broadcast: require('./src/structures/Broadcast'),
// Auth Strategies
NoAuth: require('./src/authStrategies/NoAuth'),

View file

@ -15,7 +15,7 @@ const { LoadUtils } = require('./util/Injected/Utils');
const ChatFactory = require('./factories/ChatFactory');
const ContactFactory = require('./factories/ContactFactory');
const WebCacheFactory = require('./webCache/WebCacheFactory');
const { ClientInfo, Message, MessageMedia, Contact, Location, Poll, PollVote, GroupNotification, Label, Call, Buttons, List, Reaction } = require('./structures');
const { ClientInfo, Message, MessageMedia, Contact, Location, Poll, PollVote, GroupNotification, Label, Call, Buttons, List, Reaction, Broadcast} = require('./structures');
const NoAuth = require('./authStrategies/NoAuth');
const {exposeFunctionIfAbsent} = require('./util/Puppeteer');
@ -1473,6 +1473,17 @@ class Client extends EventEmitter {
return labels.map(data => new Label(this, data));
}
/**
* Get all current Broadcast
* @returns {Promise<Array<Broadcast>>}
*/
async getBroadcasts() {
const broadcasts = await this.pupPage.evaluate(async () => {
return window.WWebJS.getAllStatuses();
});
return broadcasts.map(data => new Broadcast(this, data));
}
/**
* Get Label instance by ID
* @param {string} labelId

View file

@ -0,0 +1,69 @@
'use strict';
const Base = require('./Base');
const Message = require('./Message');
/**
* Represents a Status/Story on WhatsApp
* @extends {Base}
*/
class Broadcast extends Base {
constructor(client, data) {
super(client);
if (data) this._patch(data);
}
_patch(data) {
/**
* ID that represents the chat
* @type {object}
*/
this.id = data.id;
/**
* Unix timestamp of last status
* @type {number}
*/
this.timestamp = data.t;
/**
* Number of available statuses
* @type {number}
*/
this.totalCount = data.totalCount;
/**
* Number of not viewed
* @type {number}
*/
this.unreadCount = data.unreadCount;
/**
* Messages statuses
* @type {Message[]}
*/
this.msgs = data.msgs.map(msg => new Message(this.client, msg));
return super._patch(data);
}
/**
* Returns the Chat this message was sent in
* @returns {Promise<Chat>}
*/
getChat() {
return this.client.getChatById(this.id._serialized);
}
/**
* Returns the Contact this message was sent from
* @returns {Promise<Contact>}
*/
getContact() {
return this.client.getContactById(this.id._serialized);
}
}
module.exports = Broadcast;

View file

@ -20,5 +20,6 @@ module.exports = {
Payment: require('./Payment'),
Reaction: require('./Reaction'),
Poll: require('./Poll'),
PollVote: require('./PollVote')
PollVote: require('./PollVote'),
Broadcast: require('./Broadcast')
};

View file

@ -1014,4 +1014,15 @@ exports.LoadUtils = () => {
return response.messageSendResult === 'OK';
};
window.WWebJS.getStatusModel = status => {
let res = status.serialize();
delete res._msgs;
res.msgs = status._msgs.map(msg => window.WWebJS.getMessageModel(msg));
return res;
};
window.WWebJS.getAllStatuses = () => {
const statuses = window.Store.Status.getModelsArray();
return statuses.map(status => window.WWebJS.getStatusModel(status));
};
};