mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-22 21:40:08 +00:00
Basic Chat and Message model implementation
This commit is contained in:
@@ -19,7 +19,7 @@ client.on('ready', () => {
|
|||||||
client.on('message', (msg) => {
|
client.on('message', (msg) => {
|
||||||
console.log('MESSAGE RECEIVED', msg);
|
console.log('MESSAGE RECEIVED', msg);
|
||||||
|
|
||||||
if (!msg.id.fromMe && msg.body == 'ping') {
|
if (msg.body == 'ping') {
|
||||||
client.sendMessage(msg.from, 'pong');
|
client.sendMessage(msg.from, 'pong');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
2
package-lock.json
generated
2
package-lock.json
generated
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "whatsapp-web.js",
|
"name": "whatsapp-web.js",
|
||||||
"version": "1.0.0",
|
"version": "0.0.1",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "whatsapp-web.js",
|
"name": "whatsapp-web.js",
|
||||||
"version": "1.0.0",
|
"version": "0.0.1",
|
||||||
"description": "Library for interacting with the WhatsApp Web API ",
|
"description": "Library for interacting with the WhatsApp Web API ",
|
||||||
"main": "./src/index.js",
|
"main": "./src/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ const EventEmitter = require('events');
|
|||||||
const puppeteer = require('puppeteer');
|
const puppeteer = require('puppeteer');
|
||||||
const Util = require('../util/Util');
|
const Util = require('../util/Util');
|
||||||
const { WhatsWebURL, UserAgent, DefaultOptions, Events } = require('../util/Constants');
|
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
|
* Starting point for interacting with the WhatsApp Web API
|
||||||
@@ -40,17 +42,22 @@ class Client extends EventEmitter {
|
|||||||
// Check Store Injection
|
// Check Store Injection
|
||||||
await page.waitForFunction('window.Store != undefined');
|
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) => {
|
await page.exposeFunction('onAddMessageEvent', (msg) => {
|
||||||
this.emit('message', msg);
|
if (msg.id.fromMe) return;
|
||||||
|
|
||||||
|
this.emit('message', new Message(this, msg));
|
||||||
});
|
});
|
||||||
|
|
||||||
await page.evaluate(() => {
|
await page.evaluate(() => {
|
||||||
Store.Msg.on('add', onAddMessageEvent);
|
Store.Msg.on('add', onAddMessageEvent);
|
||||||
})
|
})
|
||||||
|
|
||||||
// // Mark all chats as read
|
|
||||||
// await page.evaluate(MarkAllRead);
|
|
||||||
|
|
||||||
this.pupBrowser = browser;
|
this.pupBrowser = browser;
|
||||||
this.pupPage = page;
|
this.pupPage = page;
|
||||||
|
|
||||||
@@ -67,6 +74,14 @@ class Client extends EventEmitter {
|
|||||||
}, chatId, message)
|
}, 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 = {
|
module.exports = {
|
||||||
Client: require('./client/Client'),
|
Client: require('./client/Client'),
|
||||||
version: require('../package.json').version
|
version: require('../package.json').version,
|
||||||
|
|
||||||
// Models
|
// Models
|
||||||
|
Chat: require('./models/Chat'),
|
||||||
}
|
Message: require('./models/Message')
|
||||||
|
};
|
||||||
@@ -11,6 +11,29 @@ class Base {
|
|||||||
*/
|
*/
|
||||||
Object.defineProperty(this, 'client', { value: client });
|
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;
|
module.exports = Base;
|
||||||
@@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
const Base = require('./Base');
|
const Base = require('./Base');
|
||||||
|
|
||||||
// TODO
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a Chat on WhatsApp
|
* Represents a Chat on WhatsApp
|
||||||
* @extends {Base}
|
* @extends {Base}
|
||||||
@@ -12,7 +10,28 @@ class Chat extends Base {
|
|||||||
constructor(client, data) {
|
constructor(client, data) {
|
||||||
super(client);
|
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');
|
const Base = require('./Base');
|
||||||
|
|
||||||
// TODO
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a Message on WhatsApp
|
* Represents a Message on WhatsApp
|
||||||
* @extends {Base}
|
* @extends {Base}
|
||||||
@@ -12,7 +10,27 @@ class Message extends Base {
|
|||||||
constructor(client, data) {
|
constructor(client, data) {
|
||||||
super(client);
|
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';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,3 +22,19 @@ exports.Events = {
|
|||||||
MESSAGE_CREATE: 'message',
|
MESSAGE_CREATE: 'message',
|
||||||
QR_RECEIVED: 'qr'
|
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
|
* Exposes the internal Store to the WhatsApp Web client
|
||||||
*/
|
*/
|
||||||
exports.ExposeStore = () => {
|
exports.ExposeStore = () => {
|
||||||
setTimeout(function () {
|
setTimeout(function() {
|
||||||
function getAllModules() {
|
function getAllModules() {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const id = _.uniqueId("fakeModule_");
|
const id = _.uniqueId("fakeModule_");
|
||||||
@@ -38,17 +38,25 @@ exports.ExposeStore = () => {
|
|||||||
return webpackJsonp([], null, [id]);
|
return webpackJsonp([], null, [id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
var store_id = 0;
|
let store_id = 0;
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
window.Store = _requireById(store_id).default;
|
window.Store = _requireById(store_id).default;
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(function () {
|
setTimeout(function() {
|
||||||
init();
|
init();
|
||||||
}, 5000);
|
}, 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 = () => {
|
exports.MarkAllRead = () => {
|
||||||
let Chats = Store.Chat.models;
|
let Chats = Store.Chat.models;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user