This commit is contained in:
Pedro Lopez
2020-02-22 17:28:15 -04:00
5 changed files with 86 additions and 52 deletions

View File

@@ -1,4 +1,4 @@
[![npm](https://img.shields.io/npm/v/whatsapp-web.js.svg)](https://www.npmjs.com/package/whatsapp-web.js) [![Depfu](https://badges.depfu.com/badges/4a65a0de96ece65fdf39e294e0c8dcba/overview.svg)](https://depfu.com/github/pedroslopez/whatsapp-web.js?project_id=9765) ![WhatsApp_Web 0.4.613](https://img.shields.io/badge/WhatsApp_Web-0.4.613-brightgreen.svg) [![npm](https://img.shields.io/npm/v/whatsapp-web.js.svg)](https://www.npmjs.com/package/whatsapp-web.js) [![Depfu](https://badges.depfu.com/badges/4a65a0de96ece65fdf39e294e0c8dcba/overview.svg)](https://depfu.com/github/pedroslopez/whatsapp-web.js?project_id=9765) ![WhatsApp_Web 0.4.930](https://img.shields.io/badge/WhatsApp_Web-0.4.930-brightgreen.svg)
# whatsapp-web.js # whatsapp-web.js
A WhatsApp API client that connects through the WhatsApp Web browser app A WhatsApp API client that connects through the WhatsApp Web browser app

View File

@@ -50,6 +50,15 @@ client.on('message', async msg => {
// Send a new message to the same chat // Send a new message to the same chat
client.sendMessage(msg.from, 'pong'); client.sendMessage(msg.from, 'pong');
} else if (msg.body.startsWith('!sendto ')) {
// Direct send a new message to specific id
let number = msg.body.split(' ')[1];
let messageIndex = msg.body.indexOf(number) + number.length;
let message = msg.body.slice(messageIndex, msg.body.length);
number = number.includes('@c.us') ? number : `${number}@c.us`;
client.sendMessage(number, message);
} else if (msg.body.startsWith('!subject ')) { } else if (msg.body.startsWith('!subject ')) {
// Change the group subject // Change the group subject
let chat = await msg.getChat(); let chat = await msg.getChat();

2
package-lock.json generated
View File

@@ -1,6 +1,6 @@
{ {
"name": "whatsapp-web.js", "name": "whatsapp-web.js",
"version": "0.3.2-post", "version": "1.0.2-post",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@@ -273,13 +273,30 @@ class Client extends EventEmitter {
} }
const newMessage = await this.pupPage.evaluate(async (chatId, message, options) => { const newMessage = await this.pupPage.evaluate(async (chatId, message, options) => {
const msg = await window.WWebJS.sendMessage(window.Store.Chat.get(chatId), message, options); let chat = window.Store.Chat.get(chatId);
let msg;
if (!chat) { // The chat is not available in the previously chatted list
let newChatId = await window.WWebJS.getNumberId(chatId);
if (newChatId) {
//get the topmost chat object and assign the new chatId to it .
//This is just a workaround.May cause problem if there are no chats at all. Need to dig in and emulate how whatsapp web does
let chat = window.Store.Chat.models[0];
let originalChatObjId = chat.id;
chat.id = newChatId;
msg = await window.WWebJS.sendMessage(chat, message, options);
chat.id = originalChatObjId; //replace the chat with its original id
}
}
else
msg = await window.WWebJS.sendMessage(chat, message, options);
return msg.serialize(); return msg.serialize();
}, chatId, content, internalOptions); }, chatId, content, internalOptions);
return new Message(this, newMessage); return new Message(this, newMessage);
} }
/** /**
* Get all current chat instances * Get all current chat instances
* @returns {Promise<Array<Chat>>} * @returns {Promise<Array<Chat>>}

View File

@@ -20,11 +20,19 @@ exports.ExposeStore = (moduleRaidStr) => {
window.Store.MediaUpload = window.mR.findModule('uploadMedia')[0]; window.Store.MediaUpload = window.mR.findModule('uploadMedia')[0];
window.Store.Cmd = window.mR.findModule('Cmd')[0].default; window.Store.Cmd = window.mR.findModule('Cmd')[0].default;
window.Store.MediaTypes = window.mR.findModule('msgToMediaType')[0]; window.Store.MediaTypes = window.mR.findModule('msgToMediaType')[0];
window.Store.UserConstructor = window.mR.findModule((module) => (module.default && module.default.prototype && module.default.prototype.isServer && module.default.prototype.isUser) ? module.default : null)[0].default;
}; };
exports.LoadUtils = () => { exports.LoadUtils = () => {
window.WWebJS = {}; window.WWebJS = {};
window.WWebJS.getNumberId = async (id) => {
let result = await window.Store.Wap.queryExist(id);
if (result.jid === undefined)
throw 'The number provided is not a registered whatsapp user';
return result.jid;
};
window.WWebJS.sendMessage = async (chat, content, options = {}) => { window.WWebJS.sendMessage = async (chat, content, options = {}) => {
let attOptions = {}; let attOptions = {};
if (options.attachment) { if (options.attachment) {