mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-19 20:19:14 +00:00
Basic Chat and Message model implementation
This commit is contained in:
@@ -19,7 +19,7 @@ client.on('ready', () => {
|
||||
client.on('message', (msg) => {
|
||||
console.log('MESSAGE RECEIVED', msg);
|
||||
|
||||
if (!msg.id.fromMe && msg.body == 'ping') {
|
||||
if (msg.body == 'ping') {
|
||||
client.sendMessage(msg.from, 'pong');
|
||||
}
|
||||
})
|
||||
|
||||
2
package-lock.json
generated
2
package-lock.json
generated
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "whatsapp-web.js",
|
||||
"version": "1.0.0",
|
||||
"version": "0.0.1",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "whatsapp-web.js",
|
||||
"version": "1.0.0",
|
||||
"version": "0.0.1",
|
||||
"description": "Library for interacting with the WhatsApp Web API ",
|
||||
"main": "./src/index.js",
|
||||
"scripts": {
|
||||
|
||||
@@ -4,7 +4,9 @@ const EventEmitter = require('events');
|
||||
const puppeteer = require('puppeteer');
|
||||
const Util = require('../util/Util');
|
||||
const { WhatsWebURL, UserAgent, DefaultOptions, Events } = require('../util/Constants');
|
||||
const { ExposeStore, MarkAllRead } = require('../util/Injected');
|
||||
const { ExposeStore, LoadExtraProps } = require('../util/Injected');
|
||||
const Chat = require('../models/Chat');
|
||||
const Message = require('../models/Message')
|
||||
|
||||
/**
|
||||
* Starting point for interacting with the WhatsApp Web API
|
||||
@@ -39,18 +41,23 @@ class Client extends EventEmitter {
|
||||
|
||||
// Check Store Injection
|
||||
await page.waitForFunction('window.Store != undefined');
|
||||
|
||||
// Load extra serialized props
|
||||
const models = [Chat, Message];
|
||||
for (let model of models) {
|
||||
await page.evaluate(LoadExtraProps, model.WAppModel, model.extraFields);
|
||||
}
|
||||
|
||||
await page.exposeFunction('onAddMessageEvent', (msg) => {
|
||||
this.emit('message', msg);
|
||||
if (msg.id.fromMe) return;
|
||||
|
||||
this.emit('message', new Message(this, msg));
|
||||
});
|
||||
|
||||
await page.evaluate(() => {
|
||||
Store.Msg.on('add', onAddMessageEvent);
|
||||
})
|
||||
|
||||
// // Mark all chats as read
|
||||
// await page.evaluate(MarkAllRead);
|
||||
|
||||
this.pupBrowser = browser;
|
||||
this.pupPage = page;
|
||||
|
||||
@@ -67,6 +74,14 @@ class Client extends EventEmitter {
|
||||
}, chatId, message)
|
||||
}
|
||||
|
||||
async getChatById(chatId) {
|
||||
let chat = await this.pupPage.evaluate(chatId => {
|
||||
return Store.Chat.get(chatId).serialize();
|
||||
}, chatId);
|
||||
|
||||
return new Chat(this, chat);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
|
||||
module.exports = {
|
||||
Client: require('./client/Client'),
|
||||
version: require('../package.json').version
|
||||
version: require('../package.json').version,
|
||||
|
||||
// Models
|
||||
|
||||
}
|
||||
Chat: require('./models/Chat'),
|
||||
Message: require('./models/Message')
|
||||
};
|
||||
@@ -11,6 +11,29 @@ class Base {
|
||||
*/
|
||||
Object.defineProperty(this, 'client', { value: client });
|
||||
}
|
||||
|
||||
_clone() {
|
||||
return Object.assign(Object.create(this), this);
|
||||
}
|
||||
|
||||
_patch(data) { return data; }
|
||||
|
||||
/**
|
||||
* Name that represents this model in the WhatsApp Web Store
|
||||
* @readonly
|
||||
*/
|
||||
static get WAppModel() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extra fields to add to model serialization
|
||||
* @readonly
|
||||
*/
|
||||
static get extraFields() {
|
||||
return [];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = Base;
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
const Base = require('./Base');
|
||||
|
||||
// TODO
|
||||
|
||||
/**
|
||||
* Represents a Chat on WhatsApp
|
||||
* @extends {Base}
|
||||
@@ -12,7 +10,28 @@ class Chat extends Base {
|
||||
constructor(client, data) {
|
||||
super(client);
|
||||
|
||||
if(data) this._patch(data);
|
||||
}
|
||||
|
||||
_patch(data) {
|
||||
this.id = data.id;
|
||||
|
||||
this.isGroup = data.isGroup;
|
||||
this.isReadOnly = data.isReadOnly;
|
||||
this.name = data.name;
|
||||
this.unreadCount = data.unreadCount;
|
||||
this.timestamp = data.t;
|
||||
}
|
||||
|
||||
sendMessage(message) {
|
||||
return this.client.sendMessage(this.id._serialized, message);
|
||||
}
|
||||
|
||||
static get extraFields() {
|
||||
return [
|
||||
'formattedTitle',
|
||||
'isGroup'
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
const Base = require('./Base');
|
||||
|
||||
// TODO
|
||||
|
||||
/**
|
||||
* Represents a Message on WhatsApp
|
||||
* @extends {Base}
|
||||
@@ -12,7 +10,27 @@ class Message extends Base {
|
||||
constructor(client, data) {
|
||||
super(client);
|
||||
|
||||
if(data) this._patch(data);
|
||||
}
|
||||
|
||||
_patch(data) {
|
||||
this.id = data.id;
|
||||
this.body = data.body;
|
||||
this.type = data.type;
|
||||
this.timestame = data.t;
|
||||
this.from = data.from;
|
||||
this.to = data.to;
|
||||
this.author = data.author;
|
||||
this.isForwarded = data.isForwarded;
|
||||
this.broadcast = data.broadcast;
|
||||
}
|
||||
|
||||
getChat() {
|
||||
return this.client.getChatById(this.from);
|
||||
}
|
||||
|
||||
static get WAppModel() {
|
||||
return 'Msg';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,4 +21,20 @@ exports.Events = {
|
||||
READY: 'ready',
|
||||
MESSAGE_CREATE: 'message',
|
||||
QR_RECEIVED: 'qr'
|
||||
}
|
||||
|
||||
exports.MessageTypes = {
|
||||
TEXT: 'chat',
|
||||
AUDIO: 'audio',
|
||||
VOICE: 'ptt',
|
||||
IMAGE: 'image',
|
||||
VIDEO: 'video',
|
||||
DOCUMENT: 'document',
|
||||
STICKER: 'sticker'
|
||||
}
|
||||
|
||||
exports.ChatTypes = {
|
||||
SOLO: 'solo',
|
||||
GROUP: 'group',
|
||||
UNKNOWN: 'unknown'
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
* Exposes the internal Store to the WhatsApp Web client
|
||||
*/
|
||||
exports.ExposeStore = () => {
|
||||
setTimeout(function () {
|
||||
setTimeout(function() {
|
||||
function getAllModules() {
|
||||
return new Promise((resolve) => {
|
||||
const id = _.uniqueId("fakeModule_");
|
||||
@@ -38,17 +38,25 @@ exports.ExposeStore = () => {
|
||||
return webpackJsonp([], null, [id]);
|
||||
}
|
||||
|
||||
var store_id = 0;
|
||||
let store_id = 0;
|
||||
|
||||
function init() {
|
||||
window.Store = _requireById(store_id).default;
|
||||
}
|
||||
|
||||
setTimeout(function () {
|
||||
setTimeout(function() {
|
||||
init();
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds extra props to the serialization of a model
|
||||
*/
|
||||
exports.LoadExtraProps = (model, props) => {
|
||||
console.log(model, props);
|
||||
Store[model].models[0].__props = Store[model].models[0].__props.concat(props);
|
||||
}
|
||||
|
||||
exports.MarkAllRead = () => {
|
||||
let Chats = Store.Chat.models;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user