forked from aaronlam/textnow-autosend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (84 loc) · 2.9 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
const actionFunc = async (username, password, recipient, message) => {
console.log("textnow bot start...");
const path = require("path");
const fs = require("fs").promises;
const puppeteer = require("puppeteer");
const textNowHelper = require("./utils/helper");
let browser = null;
let page = null;
let md5Username = textNowHelper.md5(username).substr(0, 8);
try {
browser = await puppeteer.launch({
headless: true,
});
page = await browser.newPage();
const client = await page.target().createCDPSession();
let cookies = null;
// Importing exsiting cookies from file
try {
console.log("Importing existing cookies...");
const cookiesJSON = await fs.readFile(
path.resolve(__dirname, `.cahce/${md5Username}.cookies.json`)
);
cookies = JSON.parse(cookiesJSON);
} catch (error) {
console.log("Failed to import existing cookies.");
}
// Log into TextNow and get cookies
try {
console.log("Logging in with existing cookies");
await page.setCookie(...cookies);
cookies = await textNowHelper.logIn(page, client);
} catch (error) {
console.log("Failed to log in with existing cookies.");
console.log("Logging in with account credentials...");
cookies = await textNowHelper.logIn(page, client, username, password);
}
try {
console.log("Successfully logged into TextNow!");
// Save cookies to file
await fs.writeFile(
path.resolve(__dirname, `.cahce/${md5Username}.cookies.json`),
JSON.stringify(cookies)
);
} catch (error) {
console.log("Failed to save cookies to file.");
}
// Select a conversation using recipient info
console.log("Selecting conversation...");
await textNowHelper.selectConversation(page, recipient);
// Send a message to the current recipient
console.log("Sending message...");
await textNowHelper.sendMessage(page, message);
console.log("Message sent!");
await browser.close();
} catch (error) {
console.log(error);
if (page) {
await page.screenshot({ path: "./error-screenshot.jpg", type: "jpeg" });
}
if (browser) {
await browser.close();
}
process.exit(1);
}
};
(async () => {
console.log("start...");
const config = require("./config");
const { username, password, recipient, message } = config;
const arrUsername = username.split("|");
const arrPassword = password.split("|");
if (arrUsername.length === arrPassword.length) {
for (let i = 0, length = arrUsername.length; i < length; i++) {
const strUsername = arrUsername[i];
const strPassword = arrPassword[i];
console.log(`User:${strUsername} start...`);
await actionFunc(strUsername, strPassword, recipient, message);
console.log(`User:${strUsername} end...`);
}
} else {
console.log("User information is error.");
}
console.log("end...");
})();