Professional Documents
Culture Documents
Bot Whtas App - Notepad
Bot Whtas App - Notepad
Bot Whtas App - Notepad
//
// GLOBAL VARS AND CONFIGS
//
var lastMessageOnChat = false;
var ignoreLastMsg = {};
var elementConfig = {
"chats": [0, 0, 5, 2, 0, 3, 0, 0, 0],
"chat_icons": [0, 0, 1, 1, 1, 0],
"chat_title": [0, 0, 1, 0, 0, 0, 0],
"chat_lastmsg": [0, 0, 1, 1, 0, 0],
"chat_active": [0, 0],
"selected_title": [0, 0, 5, 3, 0, 1, 1, 0, 0, 0, 0]
};
const jokeList = [
`
Husband and Wife had a Fight.
Wife called Mom : He fought with me again,
I am coming to you.
Mom : No beta, he must pay for his mistake,
I am comming to stay with U!`,
`
Husband: Darling, years ago u had a figure like Coke bottle.
Wife: Yes darling I still do, only difference is earlier it was
300ml now it's 1.5 ltr.`,
`
God created the earth,
God created the woods,
God created you too,
But then, even God makes mistakes sometimes!`,
`
What is a difference between a Kiss, a Car and a Monkey?
A kiss is so dear, a car is too dear and a monkey is U dear.`
]
//
// FUNCTIONS
//
function getLastMsg(){
var messages = document.querySelectorAll('.msg');
var pos = messages.length-1;
function getUnreadChats(){
var unreadchats = [];
var chats = getElement("chats");
if (chats){
chats = chats.childNodes;
for (var i in chats){
if (!(chats[i] instanceof Element)){
continue;
}
var icons = getElement("chat_icons",
chats[i]).childNodes;
if (!icons){
continue;
}
for (var j in icons){
if (icons[j] instanceof Element){
if
(!(icons[j].childNodes[0].getAttribute('data-icon') == 'muted' ||
icons[j].childNodes[0].getAttribute('data-icon') == 'pinned')){
unreadchats.push(chats[i]);
break;
}
}
}
}
}
return unreadchats;
}
function didYouSendLastMsg(){
var messages = document.querySelectorAll('.msg');
if (messages.length <= 0){
return false;
}
var pos = messages.length-1;
loopFewTimes();
}
// Send a message
const sendMessage = (chat, message, cb) => {
//avoid duplicate sending
var title;
if (chat){
title = getElement("chat_title",chat).title;
} else {
title = getElement("selected_title").title;
}
ignoreLastMsg[title] = message;
messageBox =
document.querySelectorAll("[contenteditable='true']")[0];
//Force refresh
event = document.createEvent("UIEvents");
event.initUIEvent("input", true, true, window, 1);
messageBox.dispatchEvent(event);
cb();
}
//
// MAIN LOGIC
//
const start = (_chats, cnt = 0) => {
// get next unread chat
const chats = _chats || getUnreadChats();
const chat = chats[cnt];
if (!lastMessageOnChat){
if (false === (lastMessageOnChat = getLastMsg())){
lastMessageOnChat = true; //to prevent the first
"if" to go true everytime
} else {
lastMsg = lastMessageOnChat;
}
} else if (lastMessageOnChat != getLastMsg() && getLastMsg() !==
false && !didYouSendLastMsg()){
lastMessageOnChat = lastMsg = getLastMsg();
processLastMsgOnChat = true;
}
// get infos
var title;
if (!processLastMsgOnChat){
title = getElement("chat_title",chat).title + '';
lastMsg = (getElement("chat_lastmsg", chat) || {
innerText: '' }).title.replace(/[\u2000-\u206F]/g, ""); //.last-msg returns null
when some user is typing a message to me
} else {
title = getElement("selected_title").title;
}
// avoid sending duplicate messaegs
if (ignoreLastMsg[title] && (ignoreLastMsg[title]) == lastMsg) {
console.log(new Date(), 'nothing to do now... (2)',
title, lastMsg);
return goAgain(() => { start(chats, cnt + 1) }, 0.1);
}