From c166e24ea53edc17b678fb9889ab70fabb94a81f Mon Sep 17 00:00:00 2001 From: Pedro Lopez Date: Sun, 17 Feb 2019 04:18:16 -0400 Subject: [PATCH] Get and send messages --- example.js | 11 +++++++++-- src/client/Client.js | 21 ++++++++++++++++++++- src/models/Base.js | 16 ++++++++++++++++ src/models/Chat.js | 19 +++++++++++++++++++ src/models/Message.js | 19 +++++++++++++++++++ src/util/Injected.js | 17 +++++++++++++++++ 6 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 src/models/Base.js create mode 100644 src/models/Chat.js create mode 100644 src/models/Message.js diff --git a/example.js b/example.js index 57db267..562a2a8 100644 --- a/example.js +++ b/example.js @@ -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'); + } +}) + diff --git a/src/client/Client.js b/src/client/Client.js index 908b20c..06c34ab 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -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; \ No newline at end of file diff --git a/src/models/Base.js b/src/models/Base.js new file mode 100644 index 0000000..b2e3735 --- /dev/null +++ b/src/models/Base.js @@ -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; \ No newline at end of file diff --git a/src/models/Chat.js b/src/models/Chat.js new file mode 100644 index 0000000..5e71800 --- /dev/null +++ b/src/models/Chat.js @@ -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; \ No newline at end of file diff --git a/src/models/Message.js b/src/models/Message.js new file mode 100644 index 0000000..005993b --- /dev/null +++ b/src/models/Message.js @@ -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; \ No newline at end of file diff --git a/src/util/Injected.js b/src/util/Injected.js index 445c3d1..4bde774 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -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); + } + } } \ No newline at end of file