Get and send messages

This commit is contained in:
Pedro Lopez
2019-02-17 04:18:16 -04:00
parent bcbd16f196
commit c166e24ea5
6 changed files with 100 additions and 3 deletions

View File

@@ -1,6 +1,6 @@
const { Client } = require('./src')
const client = new Client();
const client = new Client({puppeteer: {headless: false}});
client.initialize();
@@ -14,6 +14,13 @@ client.on('authenticated', () => {
client.on('ready', () => {
console.log('READY');
client.destroy();
});
client.on('message', (msg) => {
console.log('MESSAGE RECEIVED', msg);
if (!msg.id.fromMe && msg.body == 'ping') {
client.sendMessage(msg.from, 'pong');
}
})

View File

@@ -4,7 +4,7 @@ const EventEmitter = require('events');
const puppeteer = require('puppeteer');
const Util = require('../util/Util');
const { WhatsWebURL, UserAgent, DefaultOptions, Events } = require('../util/Constants');
const { ExposeStore } = require('../util/Injected');
const { ExposeStore, MarkAllRead } = require('../util/Injected');
/**
* Starting point for interacting with the WhatsApp Web API
@@ -40,6 +40,17 @@ class Client extends EventEmitter {
// Check Store Injection
await page.waitForFunction('window.Store != undefined');
await page.exposeFunction('onAddMessageEvent', (msg) => {
this.emit('message', msg);
});
await page.evaluate(() => {
Store.Msg.on('add', onAddMessageEvent);
})
// // Mark all chats as read
// await page.evaluate(MarkAllRead);
this.pupBrowser = browser;
this.pupPage = page;
@@ -49,6 +60,14 @@ class Client extends EventEmitter {
async destroy() {
await this.pupBrowser.close();
}
async sendMessage(chatId, message) {
await this.pupPage.evaluate((chatId, message) => {
Store.Chat.get(chatId).sendMessage(message);
}, chatId, message)
}
}
module.exports = Client;

16
src/models/Base.js Normal file
View File

@@ -0,0 +1,16 @@
'use strict';
/**
* Represents a data model
*/
class Base {
constructor(client) {
/**
* The client that instantiated this
* @readonly
*/
Object.defineProperty(this, 'client', { value: client });
}
}
module.exports = Base;

19
src/models/Chat.js Normal file
View File

@@ -0,0 +1,19 @@
'use strict';
const Base = require('./Base');
// TODO
/**
* Represents a Chat on WhatsApp
* @extends {Base}
*/
class Chat extends Base {
constructor(client, data) {
super(client);
}
}
module.exports = Chat;

19
src/models/Message.js Normal file
View File

@@ -0,0 +1,19 @@
'use strict';
const Base = require('./Base');
// TODO
/**
* Represents a Message on WhatsApp
* @extends {Base}
*/
class Message extends Base {
constructor(client, data) {
super(client);
}
}
module.exports = Message;

View File

@@ -47,4 +47,21 @@ exports.ExposeStore = () => {
setTimeout(function () {
init();
}, 5000);
}
exports.MarkAllRead = () => {
let Chats = Store.Chat.models;
for (chatIndex in Chats) {
if (isNaN(chatIndex)) {
continue;
}
let chat = Chats[chatIndex];
if (chat.unreadCount > 0) {
chat.markSeen();
Store.Wap.sendConversationSeen(chat.id, chat.getLastMsgKeyForAction(), chat.unreadCount - chat.pendingSeenCount);
}
}
}