-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAgent_script
339 lines (263 loc) · 7.97 KB
/
Agent_script
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Phantombuster configuration {
"phantombuster command: nodejs"
"phantombuster package: 5"
"phantombuster flags: save-folder"
"phantombuster dependencies: lib-StoreUtilities.js, lib-Facebook.js"
const Buster = require("phantombuster")
const buster = new Buster()
const Nick = require("nickjs")
const nick = new Nick()
const StoreUtilities = require("./lib-StoreUtilities")
const utils = new StoreUtilities(nick, buster)
const Facebook = require("./lib-Facebook")
const facebook = new Facebook(nick, buster, utils)
// }
// require local obejct as follow
/*
{
"cookie_user": "",
"cookie_xs": "",
"google_url": ""
}
*/
//function to open tab and visit a website
async function open_tab(link){
try{
const tab = await nick.newTab()
console.log("tab open")
await tab.open(link)
console.log("link open")
// It's considered a best-practice to always wait for the DOM element that interests you
// when manipulating a website (like when clicking a button or loading a page)
return tab
}
catch(err) {
console.log("error opening tab", err)
}
}
pageTimeout = 10000
async function random_wait(tab){
//get random number from 1 to 10
random_number = Math.floor((Math.random() * 10) + 1)
await tab.scroll(0,random_number)
await tab.scroll(0,-random_number)
await tab.wait(random_number*1000)
}
/*
// issue with login for new device
//facebook login function
async function fb_login(password, email){
try {
main_tab = await open_tab("https://www.facebook.com/")
await random_wait(main_tab)
await main_tab.waitUntilPresent("#email", pageTimeout)
await main_tab.sendKeys("#email", email)
console.log("email taped")
await main_tab.waitUntilPresent("#pass", pageTimeout)
await main_tab.sendKeys("#pass", password)
await random_wait(main_tab)
console.log("password taped")
main_tab.click("#loginbutton")
console.log("clicked login buton")
//wait for news feed loaded
await main_tab.waitUntilPresent("._5u5j", pageTimeout*2)
console.log("login completed")
return main_tab
} catch (err) {
//send email notification about wrong login
subject = "reddit account login issue"
text = "The following account doesn't look to work" + email
buster.mail(subject, text)
}
}
*/
//deprecated send message without line break
async function send_fb_message(url, message, tab){
await tab.open(url)
// get url to
await tab.wait(1000)
await random_wait(tab)
profile_url = await tab.getUrl()
profile_extension = profile_url.substring(profile_url.indexOf("facebook.com/")+13)
selector = 'a['+ 'href*="/messages/t/' + profile_extension + '"]'
await tab.waitUntilPresent(selector)
await tab.click(selector)
console.log("open chat tab")
await tab.wait(1000)
await tab.waitUntilPresent("._5rpu")
//type actuall message
await tab.sendKeys("._5rpu", message)
try {
const path = await tab.screenshot("message_taped.jpg")
console.log("Screenshot saved at")
// Your screenshot is available at this path
} catch (err) {
console.log("Could not take a screenshot:", err)
}
//send by clicking on send buton
await tab.sendKeys("._5rpu", message)
await tab.click('[label=send]')
await tab.wait(1000)
await tab.click(".close")
console.log("message send")
}
async function open_chat_tab(url, tab){
await tab.open(url)
// get url to
await tab.wait(1000)
await random_wait(tab)
profile_url = await tab.getUrl()
profile_extension = profile_url.substring(profile_url.indexOf("facebook.com/")+13)
try{
selector = 'a['+ 'href*="/messages/t/' + profile_extension + '"]'
await tab.waitUntilPresent(selector)
await tab.click(selector)
} catch (err) {
//handled profile with url like https://www.facebook.com/profile.php?id=xxxxx
profile_extension = profile_extension.substring(profile_extension.indexOf("php?id=")+7)
selector = 'a['+ 'href*="/messages/t/' + profile_extension + '"]'
await tab.waitUntilPresent(selector)
await tab.click(selector)
}
console.log("open chat tab")
await tab.wait(1000)
await tab.waitUntilPresent("._5rpu")
}
async function get_name_script(arr, callback){
stemp = document.querySelector('._2nlw')
stemp = stemp.text
stemp = stemp.slice(0,stemp.indexOf(' '))
;
callback(null, stemp)
}
async function type_line(line, tab){
await tab.wait(500)
await tab.sendKeys("#myid", line)
await tab.sendKeys("#myid", '\r')
}
//function to mark proper entry fild with specific id
function console_script_marking(arr, callback){
stemp = document.querySelectorAll('._5rpu')
if(stemp.length == 2){
stemp = stemp[1]
}else {
stemp = stemp[0]
}
stemp.id = 'myid';
callback(null)
}
async function send_and_close_tab(tab){
await tab.wait(500)
try{
await tab.waitUntilPresent('[label=send]')
//await tab.evaluate(send_script)
await tab.click("[label=send]")
await tab.wait(1000)
} catch (err) {
await tab.waitUntilPresent('._2lum')
await tab.click("._2lum")
await tab.wait(1000)
}
await tab.click(".close")
await tab.wait(500)
console.log("message send")
console.log('window closed')
}
async function get_name(tab){
try{
name = await tab.evaluate(get_name_script)
console.log("consol script tur ")
console.log(name)
return name
} catch (err) {
name = " "
return name
}
}
async function main(){
//
//
// get agent object
local_object = await buster.getAgentObject()
//
//
//part 1 initiatialisation
//get list of link
link_list = await utils.getDataFromCsv(local_object.google_url, "url_list")
console.log(link_list)
Number_per_day = await utils.getDataFromCsv(local_object.google_url, "Number_per_day")
Number_per_day = Number_per_day[0]
console.log("I will send " + Number_per_day + "messages per day")
main_tab = await open_tab("https://www.facebook.com/")
log_control = await facebook.login(main_tab, local_object.cookie_user, local_object.cookie_xs)
//console.log("login completed")
//console.log(log_control)
//get column with message
message = await utils.getDataFromCsv(local_object.google_url, "message")
//restrict to firt line
message = message[0]
console.log(message)
//console.log(message)
messqge_seq = message.split('\n')
//console.log(messqge_seq)
//
//
//part 2 loop to send all the messages
// set sending sequence if not set
if (typeof local_object["Stemp_log_seq"] == "undefined") {
local_object.Stemp_log_seq = Array.from(Array(link_list.length).keys())
// save sending sequence on local object
buster.setAgentObject(local_object)
}
// Number of message to be send = user parmeter or number of remaining link in the list
if (local_object.Stemp_log_seq.length < Number_per_day){
Number_per_day = local_object.Stemp_log_seq.length
} else {
Number_per_day = Number_per_day
}
// remove from Stemp_log_seq the login which will be contacted today
var i;
for (i = 0; i < Number_per_day; i++) {
//select url based on Stemp_log_seq value
console.log("in the loop to send messages")
//console.log("i is" + i)
//console.log('N per day is '+ Number_per_day )
//console.log(link_list)
//console.log(local_object.Stemp_log_seq)
url = link_list[local_object.Stemp_log_seq[i]]
console.log(url)
await open_chat_tab(url, main_tab)
console.log('tab open')
//try to get user name
name = await get_name(main_tab)
console.log(name)
await main_tab.wait(2000)
//mark the proper send field
await main_tab.evaluate(console_script_marking)
for (ii = 0; ii < messqge_seq.length; ii++){
lines = messqge_seq[messqge_seq.length - ii - 1]
if (lines.includes('##Name') == false) {
await type_line( lines, main_tab)
//console.log('key send')
} else {
lines = await lines.replace('##Name', name)
await type_line( lines, main_tab)
//console.log('taped line with name')
}
console.log(ii)
//debug fuction to get screenshot line per line
//image_name = 'image' + ii + ".jpg"
//path1 = await main_tab.screenshot(image_name)
//await buster.save(path1)
}
//console.log('exit typing loop')
//debug function for screenshot
//const path = await main_tab.screenshot("image.jpg")
//await buster.save(path)
await send_and_close_tab(main_tab)
}
local_object.Stemp_log_seq = local_object.Stemp_log_seq.slice(Number_per_day)
await buster.setAgentObject(local_object)
nick.exit()
}
main()