Created
March 14, 2018 23:01
-
-
Save LevitatingBusinessMan/543ae891b12aad657c0979680ac5dffe to your computer and use it in GitHub Desktop.
Whatsapp bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Stupid small project of mine, if you run this in a web.whatsapp console you should be good to go | |
Note, it looks like sending a msg in a group is bugged. And the group command doesnt work because of it | |
*/ | |
var lastMessage; | |
setInterval(getLastMessage, 500); | |
var prefix = '!'; | |
var messagesMap = new Map(); | |
$('header ._1wjpf').click(); | |
var commands = { | |
about: { | |
run: (message, args) => { | |
reply('I am reins Whatsapp bot'); | |
} | |
}, choose: { | |
run: (message, args) => { | |
if (!args[0]) {reply("No arguments :v"); return;} | |
if (!args.join("").includes(";")) {reply('Options must be seperated by a *;*'); return;} | |
else var choose = args.join(" ").split(";").filter(x => x && x != " "); | |
reply(`I choose *${choose[Math.floor(Math.random()*(choose.length))].trim()}*!`); | |
} | |
}, help: { | |
run : (message, args) => { | |
msg = '*Commands*:\n'; | |
for (var property in commands) msg += property + '\n'; | |
reply(msg); | |
} | |
}, group: { | |
run : (message, args) => { | |
if (!document.getElementsByClassName('_1xGbt')[0]) {reply('An error occured!'); return;} | |
if (!document.getElementsByClassName('Cpiae')[0].innerHTML.startsWith('Created')) {reply ('This is not a group chat!'); return;} | |
let participants = document.getElementsByClassName('_1sYdX')[1].innerHTML.split(/\s/g)[0]; | |
let name = document.getElementsByClassName('_2S1VP')[0].innerHTML; | |
let createdAt = document.getElementsByClassName('Cpiae')[0].innerHTML; | |
let admins = 1; | |
let users = document.getElementsByClassName('RLfQR')[0].childNodes; | |
for (i = 0; i < users.length; i++) if (users[i].getElementsByClassName('_2e8Mu')[0]) admins++; | |
console.log(`*${name}*\n Participants: ${participants}\n Admins: ${admins}\n CreatedAt: ${createdAt}`); | |
reply(`*${name}*\n Participants: ${participants}\n Admins: ${admins}\n CreatedAt: ${createdAt}`); | |
} | |
} | |
} | |
function getLastMessage() { | |
let messages = document.getElementsByClassName('Tkt2p'); | |
let message = messages[messages.length - 1]; | |
if (message.getAttribute('id')) return false;//Already handled | |
let id = createID(); | |
message.setAttribute('id', id); | |
//Need some author shit | |
let content = message.getElementsByTagName("span")[0].innerHTML; | |
let createdAt = message.getElementsByClassName("_3EFt_")[0].innerHTML; | |
var status = message.getElementsByTagName("span")[2].getAttribute('data-icon'); //gotta change that up | |
let newMessage = { | |
id: id, | |
//author: author, | |
content: content, | |
createdAt: createdAt, | |
status: status | |
}; | |
eventMessage(newMessage); | |
lastMessage = newMessage; | |
messagesMap.set(newMessage.id, newMessage); | |
return newMessage; | |
} | |
function eventMessage(message) { | |
if (!message.content.startsWith(prefix)) return; | |
let command = message.content.substr(prefix.length).split(/\s/g)[0]; | |
let args = message.content.substr(prefix.length).split(/\s/g); | |
args.shift(); | |
if (commands[command]) commands[command].run(message, args); | |
} | |
function reply(msg) { | |
document.getElementsByClassName('_39LWd')[0].setAttribute('style', 'visibility: hidden'); | |
let input = document.getElementsByClassName('_2S1VP')[0] | |
input.innerHTML = msg; | |
//Somehow triggers the send button | |
window.InputEvent = window.Event || window.InputEvent; //wtf does this do | |
let event = new InputEvent('input', {bubbles: true}); //wtf does this do | |
input.dispatchEvent(event); | |
document.getElementsByClassName('_2lkdt')[0].click(); | |
} | |
function createID() { | |
let string = ''; | |
let characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |
for (var i = 0; i < 20; i++) | |
string += characters.charAt(Math.floor(Math.random() * characters.length)); | |
return string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment