Instantly share code, notes, and snippets.

@embrilliant /autoRespond.js Secret
Last active May 26, 2018

Embed
What would you like to do?
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

This comment has been minimized.

Show comment
Hide comment
@embrilliant

embrilliant 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.

Owner

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