-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
266 lines (232 loc) · 9.39 KB
/
index.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
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
const {Builder, By, Key, until} = require('selenium-webdriver');
const { parse } = require('node-html-parser');
const axios = require('axios');
const config = require('./config');
const sendMail = require('./sendMail');
const emailTemplate = require('./emailTemplate');
const client = require('twilio')(config.TWILIO_ACC_SID, config.TWILIO_AUTH_TOKEN);
let driver1 = new Builder().forBrowser('chrome').build();
let driver2 = new Builder().forBrowser('chrome').build();
let interval1 = null, interval2 = null;
let prevUpdateID = 0;
async function makePostRequest(params) {
var headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'key': 'lkmqwer7f60b864976a08abh789t098'
}
/*var params = {
first_name: 'test f name',
last_name: 'test l name',
email: '[email protected]',
phone: '1234567890',
address: 'test',
zipcode: '121212',
city: 'test city'
}*/
var url = "http://homeinsurance.flquotes.com/external_api/insert_lead_from_external_source.php";
const formUrlEncoded = x => Object.keys(x).reduce((p, c) => p + `&${c}=${encodeURIComponent(x[c])}`, '')
axios({
method: 'POST',
url,
headers,
data: formUrlEncoded(params)
}).then(function(response) {
// console.log(response.data)
console.log(response.data.ResponseMsg)
}).catch(function(error) {
console.log(error)
})
}
const getLeadsForSMS = async () => {
try {
await driver1.wait(until.titleIs('FMAP - SearchResults'), 10000);
let html = await driver1.getPageSource();
let root = parse(html);
let rows = root.querySelector('#property tbody') ? root.querySelector('#property tbody').childNodes : [];
rows.forEach((tr, j) => {
if (tr.tagName === 'tr') {
if (!tr.childNodes[7].childNodes[0]) {
const checkID = tr.childNodes[19].childNodes[3].rawAttrs.match('onclick="interested\\((.*)\\)"')[1];
getConsumerInfo(checkID, j, tr.childNodes[5].childNodes[0].rawText);
}
}
})
} catch (error) {
console.log('getLeadsForSMS Error:', error)
}
}
const getLeadsForEmail = async () => {
try {
await driver2.wait(until.titleIs('FMAP - SearchResults'), 10000);
let html = await driver2.getPageSource();
let root = parse(html);
let rows = root.querySelector('#property tbody') ? root.querySelector('#property tbody').childNodes : [];
let updateIDs = [];
rows.forEach((tr, j) => {
if (tr.tagName === 'tr') {
if (tr.childNodes[7].childNodes[0]) {
const updateID = tr.childNodes[19].childNodes[1].rawAttrs.match('onclick="updateQuoteRequestStatus\\((.*)\\)"')[1];
let consumerInfo = tr.childNodes[7].childNodes[0].rawText.replace('Cell', ' Cell');//.replace('<', '<'). replace('>', ' >');
// closeLead(updateID);
if (updateID > prevUpdateID) {
updateIDs.push({updateID, consumerInfo});
prevUpdateID = updateID;
}
}
}
})
closeLeads(updateIDs);
} catch (error) {
console.log('getLeadsForEmail Error:', error)
}
}
const getConsumerInfo = async (checkID,index, id) => {
try {
await driver1.executeScript("interested(" + checkID + ")")
await driver1.switchTo().alert().accept();
await driver1.wait(until.titleIs('FMAP - SearchResults'), 10000);
let html = await driver1.getPageSource();
let root = parse(html);
let rows = root.querySelector('#property tbody').childNodes;
let consumerInfo = rows[index].childNodes[7].childNodes[0].rawText
let body = consumerInfo.replace('Cell', ' Cell')
// .replace('<', '<')
// .replace('>', ' >')
.replace('Home', '<br/>Home')
.replace('Cell', '<br/>Cell')
.replace('Email', '<br/>Email');
client.messages.create({
to: config.TWILIO_TO,
from: config.TWILIO_FROM,
body
})
.then(msg => console.log(msg.sid))
let params = {}
params.last_name = consumerInfo.substring(0, consumerInfo.indexOf(","));
params.first_name = consumerInfo.substring(consumerInfo.indexOf(",") + 1, consumerInfo.indexOf("Home:")).trim();
params.phone = consumerInfo.substring(consumerInfo.indexOf("Home:") + 5, consumerInfo.indexOf("Cell:")).trim();
params.phone.replace('(', '')
params.phone.replace(')', '')
params.phone.replace(' ', '')
params.phone.replace('-', '')
params.email = consumerInfo.substring(consumerInfo.indexOf("Email:") + 6).trim();
params.address = 'na'
params.zipcode = 123
params.city = 'na'
// console.log(params)
makePostRequest(params)
} catch (error) {
console.log('getConsumerInfo Error:', error)
}
}
const closeLeads = async (updateIDs) => {
try {
const len = updateIDs.length
for (let i = 0; i < len; i++) {
let updateID = updateIDs[i].updateID;
// console.log("updateQuoteRequestStatus(" + updateID + ")")
await driver2.executeScript("updateQuoteRequestStatus(" + updateID + ")")
await driver2.wait(until.titleIs('FMAP - Update Status'), 10000);
// let fieldset = await driver2.findElements(By.tagName("fieldset"));
let html = await driver2.getPageSource();
let root = parse(html);
let fieldset = root.querySelectorAll('fieldset.eightPt_fieldset')[1].outerHTML;
let consumerInfo = `<div style="font-weight: bold">Consumer Info</div>
<div>${updateIDs[i].consumerInfo}<div>`;
sendMail(emailTemplate(consumerInfo + fieldset))
.then(data => {
console.log("Lead mailed");
})
.catch(error => {
console.log("Mail error:", error);
});
await driver2.findElement(By.name('update')).click();
await driver2.get('https://fmap.citizensfla.com/fmap/searchcriteria.do');
await driver2.wait(until.titleIs('FMAP - Agent Home Page'), 10000);
await driver2.executeScript("runSearch('56139')")
await driver2.switchTo().alert().accept();
}
} catch (error) {
console.log('closeLeads Error:', error)
}
}
(async function example() {
try {
// Driver for SMS
await driver1.get('https://fmap.citizensfla.com/fmap/login.do');
await driver1.wait(until.titleIs('FMAP - Login'), 10000);
// let title1 = await driver1.getTitle();
// console.log(title1);
await driver1.findElement(By.name('loginId')).sendKeys(config.FMAP_USERNAME);
await driver1.findElement(By.name('password')).sendKeys(config.FMAP_PASSWORD);
await driver1.findElement(By.name('Login')).click();
await driver1.wait(until.titleIs('FMAP - Agent Home Page'), 10000);
// let title2 = await driver1.getTitle();
// console.log(title2);
await driver1.executeScript("runSearch('56139')")
await driver1.switchTo().alert().accept();
getLeadsForSMS();
// Driver for Email
await driver2.get('https://fmap.citizensfla.com/fmap/login.do');
await driver2.wait(until.titleIs('FMAP - Login'), 10000);
// let title1 = await driver2.getTitle();
// console.log(title1);
await driver2.findElement(By.name('loginId')).sendKeys(config.FMAP_USERNAME);
await driver2.findElement(By.name('password')).sendKeys(config.FMAP_PASSWORD);
await driver2.findElement(By.name('Login')).click();
await driver2.wait(until.titleIs('FMAP - Agent Home Page'), 10000);
// let title2 = await driver2.getTitle();
// console.log(title2);
await driver2.executeScript("runSearch('56139')")
await driver2.switchTo().alert().accept();
getLeadsForEmail();
interval1 = setInterval(async () => {
await driver1.navigate().refresh();
getLeadsForSMS();
}, 2500)
interval2 = setInterval(async () => {
await driver2.navigate().refresh();
getLeadsForEmail();
}, 4000)
} catch(err) {
clearInterval(interval1)
clearInterval(interval2)
console.log(err)
// Driver for SMS
await driver1.get('https://fmap.citizensfla.com/fmap/login.do');
await driver1.wait(until.titleIs('FMAP - Login'), 10000);
// let title1 = await driver1.getTitle();
// console.log(title1);
await driver1.findElement(By.name('loginId')).sendKeys(config.FMAP_USERNAME);
await driver1.findElement(By.name('password')).sendKeys(config.FMAP_PASSWORD);
await driver1.findElement(By.name('Login')).click();
await driver1.wait(until.titleIs('FMAP - Agent Home Page'), 10000);
// let title2 = await driver1.getTitle();
// console.log(title2);
await driver1.executeScript("runSearch('56139')")
await driver1.switchTo().alert().accept();
getLeadsForSMS();
// Driver for Email
await driver2.get('https://fmap.citizensfla.com/fmap/login.do');
await driver2.wait(until.titleIs('FMAP - Login'), 10000);
// let title1 = await driver2.getTitle();
// console.log(title1);
await driver2.findElement(By.name('loginId')).sendKeys(config.FMAP_USERNAME);
await driver2.findElement(By.name('password')).sendKeys(config.FMAP_PASSWORD);
await driver2.findElement(By.name('Login')).click();
await driver2.wait(until.titleIs('FMAP - Agent Home Page'), 10000);
// let title2 = await driver2.getTitle();
// console.log(title2);
await driver2.executeScript("runSearch('56139')")
await driver2.switchTo().alert().accept();
getLeadsForEmail();
interval1 = setInterval(async () => {
await driver1.navigate().refresh();
getLeadsForSMS();
}, 2500)
interval2 = setInterval(async () => {
await driver2.navigate().refresh();
getLeadsForEmail();
}, 4000)
}
})()