-
Notifications
You must be signed in to change notification settings - Fork 0
/
rabbit-archiver.js
185 lines (150 loc) · 4.33 KB
/
rabbit-archiver.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*jshint esversion: 6 */
/* Handle the Facebook Messenger Inbox Mess when you plug a Chat Bot into it. */
var pageName;
var isRunning = false;
function run() {
if (isRunning) {
isRunning = false;
} else {
isRunning = true;
pageName = $(sel.page_name).text();
msgStart();
accessInbox();
startManaging();
}
}
/* Go to the inbox if you're not */
function accessInbox() {
//if ($(sel.inbox).text() !== 'Caixa de Entrada') {
$(sel.inbox_combo).click();
$(sel.inbox_combo_item).click();
//}
}
var chatItems;
var index;
/* Capture the chat items as array object and define the current index
This function starts the process */
function startManaging() {
chatItems = $(sel.chat_items);
index = chatItems.length;
handleChats();
}
function handleChats() {
if (isRunning) {
index--;
if (index >= 0) {
paintBrush(chatItems[index - 1], $(chatItems[index - 1]).text());
if (tryMarkAsDoneChatItem()) {
later(sel.fast_delay).then(() => {
handleChats();
});
} else {
chatItems[index].click();
checkOpenChat(sel.delay);
}
} else {
if (chatItems.length > 0) {
startManaging();
} else {
//No more messages is showing
msgFinished();
tryToRestart(sel.long_delay);
}
}
} else {
msgAborted();
}
}
/* If we can determine that the page was the last one who spoke,
we can mark as done on the chat item instead of opening it to check */
function tryMarkAsDoneChatItem() {
var current = $(chatItems[index]);
var preview = current.find(sel.preview_last_msg);
//First look out for the paper clips
var done = preview.find(sel.preview_glyph).length > 0;
if (done) {
//Fine! Your page sent the last message, we can mark this conversation as done.
markAsDoneOnChatList();
return true;
} else if (needIgnore(preview.text())) {
//If the last sent message need to be ignored
markAsDoneOnChatList(true);
return true;
}
return false;
}
//Mark as done the current message
function markAsDoneOnChatList(skipMessage) {
if (!skipMessage)
msgDone(pageName);
$(chatItems[index]).find(sel.done_item).click();
}
/* Check the last person who spoke on the current opened conversation and apply
the criteria to mark as done or as spam */
function checkOpenChat(delay) {
//We can't wait to much time to analyze and process one single conversation. Time is money
//Wait 10 sec max
if (delay > sel.big_long_delay) {
markAsDoneOnChatList();
handleChats();
} else {
//As we don't have a load callback to track when the conversation is loaded
//We have to check when it's ready to us time to time.
later(delay).then(() => {
//If we can't access the last one who sent a message, means the conversation didn't load yet.
if ($(sel.last_spoke).length === 0) {
//Try it again, now add more 500 ms
checkOpenChat(delay + sel.delay);
} else {
var whoSentTheLastMessage = $(sel.last_spoke).last().text().trim();
//Check if the last message sent was by the person who we are chatting with
if ($(sel.name).text().contains(whoSentTheLastMessage)) {
//Ok, later we need to access the spam folder to reply this message
$(sel.spam).click();
msgSpam(whoSentTheLastMessage);
} else {
//Don't worry, this message already was responded
$(sel.done).click();
msgDone(whoSentTheLastMessage);
}
//Go to the next message
handleChats();
}
}).catch((e) => {
console.log(e);
});
}
}
function needIgnore(msg) {
var found = true;
if (msg.length) {
msg = msg.toLowerCase();
var word;
found = ignore.some((item) => {
word = item;
return msg.includes(item);
});
if (found)
msgNeedToIgnore(word);
}
return found;
}
function tryToRestart(wait) {
msgTryAgain(wait);
accessInbox();
//Wait some time to search for new messages
later(wait).then(() => {
//Now, if we find any messages, we continue, if not, the process is stopped.
if ($(sel.chat_items).length > 0) {
msgProceedProcess();
startManaging();
} else {
if (wait < sel.big_long_delay) {
wait += sel.long_delay;
tryToRestart(wait);
} else {
msgNoMoreMessages();
}
}
});
}