Skip to content

Instantly share code, notes, and snippets.

@embrilliant
Last active May 26, 2018 09:40
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save embrilliant/f1c2670f027d466a0979f56a4a47bdb0 to your computer and use it in GitHub Desktop.
Auto-respond on Whatsapp
class Robot {
constructor() {
this.cases = {
"how are you": "I am fine. What about you?",
"what are you doing": "I am talking to you.",
"hungry": "How about eating something?",
"you like": "I like cake.",
"why": "You will find out why.",
"hey": "Hey.",
"hello": "Hi.",
"bye": "Bye.",
"cool": "Cool stuff.",
"awesome": "Awesome.",
"good": "Sounds good.",
};
this.randomResponses = ["Right.", "Ok.", "Oh.", "Really?", "Good point.", "Alright.", "I don't know.", "And then?"];
}
isHi(textString) {
let hi = textString.replace(/[^\w\s]+/g, ""), // remove special characters except white space
regex = /\bhi\b/; // check if it's "hi" alone
return regex.test(hi);
}
getResponse(textString) {
const stringLowerCased = textString.toLowerCase();
let response = "";
if (this.isHi(stringLowerCased)) {
response = "Hello.";
} else {
for (let key in this.cases) {
if (stringLowerCased.indexOf(key) >= 0) {
response = this.cases[key];
return response;
} else {
let ran = Math.floor((Math.random() * (this.randomResponses.length + 1)));
if (ran < this.randomResponses.length) {
response = this.randomResponses[ran];
} else {
response = textString;
}
}
}
}
return response;
}
}
function autoRespond(interval) {
let prevNumberOfMsgIns = document.querySelectorAll(".message-in").length;
function respond(currentNumberOfMsgIns) {
const bot = new Robot(),
lastMsgInElement = document.querySelectorAll(".message-in")[currentNumberOfMsgIns - 1],
textElement = lastMsgInElement.querySelector("span.selectable-text");
let incomingText = "",
botMsg = "😊"; // Respond with this smiley if the incoming msg is one smiley without text.
function composeAndSend(message) {
const input = document.getElementsByClassName("_2S1VP")[0], // "_2S1VP" is the current class name of the input element.
event = new Event("input", {bubbles: true});
let send = null;
input.textContent = message;
input.dispatchEvent(event);
send = document.querySelector("[data-icon='send']");
send.click();
}
if (textElement) { // If the latest message is text
let lengthOfTextMsgIns = document.querySelectorAll(".message-in div div span.selectable-text").length,
lastTextMsgInElement = document.querySelectorAll(".message-in div div span.selectable-text")[lengthOfTextMsgIns - 1];
incomingText = lastTextMsgInElement.innerText;
botMsg = bot.getResponse(incomingText);
}
composeAndSend(botMsg);
}
setInterval(() => {
let currentNumberOfMsgIns = document.querySelectorAll(".message-in").length;
if (currentNumberOfMsgIns > prevNumberOfMsgIns) {
respond(currentNumberOfMsgIns);
}
prevNumberOfMsgIns = currentNumberOfMsgIns;
}, interval);
}
@embrilliant
Copy link
Author

embrilliant commented Mar 26, 2018

Although there are existing apps that auto-respond messenger messages such as AutoResponder for WhatsApp, this is something that I came up with on my own with only JavaScript. Nothing needs to be installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment