From b07b38bbe8eebd9d6bc12ddec1bcc8e21d18ab49 Mon Sep 17 00:00:00 2001 From: Pedro Lopez Date: Sat, 29 Feb 2020 14:07:47 -0400 Subject: [PATCH] feat: Simulate recording audio in chat, clear recording/typing state, standardize chatstate change function names --- example.js | 11 ++++++++++- src/structures/Chat.js | 25 ++++++++++++++++++++++--- src/util/Injected.js | 17 +++++++++++++++-- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/example.js b/example.js index 6e92427..10a606d 100644 --- a/example.js +++ b/example.js @@ -170,7 +170,16 @@ client.on('message', async msg => { chat.archive(); } else if (msg.body === '!typing') { const chat = await msg.getChat(); - chat.typing(); + // simulates typing in the chat + chat.sendStateTyping(); + } else if (msg.body === '!recording') { + const chat = await msg.getChat(); + // simulates recording audio in the chat + chat.sendStateRecording(); + } else if (msg.body === '!clearstate') { + const chat = await msg.getChat(); + // stops typing or recording in the chat + chat.clearState(); } }); diff --git a/src/structures/Chat.js b/src/structures/Chat.js index 2f7ac52..810bdfe 100644 --- a/src/structures/Chat.js +++ b/src/structures/Chat.js @@ -104,15 +104,34 @@ class Chat extends Base { } /** - * Start typing in chat. This will last for 25 seconds. + * Simulate typing in chat. This will last for 25 seconds. */ - async sendTyping() { + async sendStateTyping() { return this.client.pupPage.evaluate(chatId => { - window.WWebJS.typing(chatId); + window.WWebJS.sendChatstate('typing', chatId); return true; }, this.id._serialized); } + /** + * Simulate recording audio in chat. This will last for 25 seconds. + */ + async sendStateRecording() { + return this.client.pupPage.evaluate(chatId => { + window.WWebJS.sendChatstate('recording', chatId); + return true; + }, this.id._serialized); + } + + /** + * Stops typing or recording in chat immediately. + */ + async clearState() { + return this.client.pupPage.evaluate(chatId => { + window.WWebJS.sendChatstate('stop', chatId); + return true; + }, this.id._serialized); + } } module.exports = Chat; diff --git a/src/util/Injected.js b/src/util/Injected.js index b4728ca..1b9f820 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -256,8 +256,21 @@ exports.LoadUtils = () => { return false; }; - window.WWebJS.typing = async (chatId) => { - await window.Store.Wap.sendChatstateComposing(chatId); + window.WWebJS.sendChatstate = async (state, chatId) => { + switch(state) { + case 'typing': + await window.Store.Wap.sendChatstateComposing(chatId); + break; + case 'recording': + await window.Store.Wap.sendChatstateRecording(chatId); + break; + case 'stop': + await window.Store.Wap.sendChatstatePaused(chatId); + break; + default: + throw 'Invalid chatstate'; + } + return true; };