-
Notifications
You must be signed in to change notification settings - Fork 282
/
bot.js
159 lines (124 loc) · 4.79 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
(() => {
//
// GLOBAL VARS AND CONFIGS
//
//const whitelist = ['Fotos salvass', 'Teste ignore']
const ignoreLastMsg = {}
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
//
// Get random value between a range
function rand(high, low = 0) {
return Math.floor(Math.random() * (high - low + 1) + low);
}
// Call the main function again
const goAgain = (fn, sec) => {
// const chat = document.querySelector('div.chat:not(.unread)')
// selectChat(chat)
setTimeout(fn, sec * 1000)
}
// Dispath an event (of click, por instance)
const eventFire = (el, etype) => {
let evObj = document.createEvent('Events')
evObj.initEvent(etype, true, false)
el.dispatchEvent(evObj)
}
// Select a chat to show the main box
const selectChat = (chat, cb) => {
const title = chat.querySelector('.emojitext').title
eventFire(chat, 'mousedown')
if (!cb) return
const loopFewTimes = () => {
setTimeout(() => {
const titleMain = document.querySelector('h2.chat-title').innerText
if (titleMain != title) {
console.log('not yet')
return loopFewTimes()
}
return cb()
}, 1)
}
loopFewTimes()
}
// Send a message
const sendMessage = (chat, message, cb) => {
//avoid duplicate sending
const title = chat.querySelector('.emojitext').title
ignoreLastMsg[title] = message
//add text into input field
document.querySelector('.input').innerHTML = message.replace(/ /gm,'')
//Force refresh
event = document.createEvent("UIEvents");
event.initUIEvent("input", true, true, window, 1)
document.querySelector('.input').dispatchEvent(event)
//Click at Send Button
eventFire(document.querySelector('.compose-btn-send'), 'click')
cb()
}
//
// MAIN LOGIC
//
const start = (_chats, cnt = 0) => {
// get next unread chat
const chats = _chats || document.querySelectorAll('.unread.chat')
const chat = chats[cnt]
if (chats.length == 0 || !chat) {
console.log(new Date(), 'nothing to do now... (1)', chats.length, chat)
return goAgain(start, 3)
}
// get infos
const title = chat.querySelector('.emojitext').title + ''
const lastMsg = (chat.querySelector('.last-msg') || { innerText: '' }).innerText //.last-msg returns null when some user is typing a message to me
// avoid sending duplicate messaegs
if ((ignoreLastMsg[title]) == lastMsg) {
console.log(new Date(), 'nothing to do now... (2)', title, lastMsg)
return goAgain(() => { start(chats, cnt + 1) }, 0.1)
}
// what to answer back?
let sendText
if (lastMsg.toUpperCase().indexOf('@HELP') > -1)
sendText = `
Cool ${title}! Some commands that you can send me:
1. *@TIME*
2. *@JOKE*`
if (lastMsg.toUpperCase().indexOf('@TIME') > -1)
sendText = `
Don't you have a clock, dude?
*${new Date()}*`
if (lastMsg.toUpperCase().indexOf('@JOKE') > -1)
sendText = jokeList[rand(jokeList.length - 1)]
// that's sad, there's not to send back...
if (!sendText) {
ignoreLastMsg[title] = lastMsg
console.log(new Date(), 'new message ignored -> ', title, lastMsg)
return goAgain(() => { start(chats, cnt + 1) }, 0.1)
}
console.log(new Date(), 'new message to process, uhull -> ', title, lastMsg)
// select chat and send message
selectChat(chat, () => {
sendMessage(chat, sendText.trim(), () => {
goAgain(() => { start(chats, cnt + 1) }, 0.1)
})
})
}
start()
})()