Skip to content

Commit

Permalink
feat: Simulate recording audio in chat, clear recording/typing state,…
Browse files Browse the repository at this point in the history
… standardize chatstate change function names
  • Loading branch information
pedroslopez committed Feb 29, 2020
1 parent d35f101 commit b07b38b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
11 changes: 10 additions & 1 deletion example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});

Expand Down
25 changes: 22 additions & 3 deletions src/structures/Chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
17 changes: 15 additions & 2 deletions src/util/Injected.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down

0 comments on commit b07b38b

Please sign in to comment.