mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-17 19:26:20 +00:00
Get and send messages
This commit is contained in:
11
example.js
11
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');
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -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
16
src/models/Base.js
Normal 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
19
src/models/Chat.js
Normal 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
19
src/models/Message.js
Normal 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;
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user