This repository was archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
152 lines (134 loc) · 4.21 KB
/
app.js
File metadata and controls
152 lines (134 loc) · 4.21 KB
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
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const CSVToJSON = require('csvtojson');
const cron = require('node-cron');
const qrcode = require('qrcode-terminal');
const readline = require('readline');
const { Client } = require('whatsapp-web.js');
const app = express();
const config = require('./config.json');
const SESSION_FILE_PATH = './session.json';
app.use(bodyParser.json({ limit: '50mb' }));
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true }));
let sessionCfg;
if (fs.existsSync(SESSION_FILE_PATH)) {
sessionCfg = require(SESSION_FILE_PATH);
}
process.title = 'waapi';
global.client = new Client({
puppeteer: {
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--unhandled-rejections=strict',
],
},
session: sessionCfg,
});
global.authed = false;
client.on('qr', (qr) => {
fs.writeFileSync('./last.qr', qr);
});
client.on('authenticated', (session) => {
console.log('You logged in!');
sessionCfg = session;
fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function (err) {
if (err) {
console.error(err);
}
authed = true;
});
try {
fs.unlinkSync('./last.qr');
} catch (err) {}
});
client.on('auth_failure', () => {
console.log('Authentication Failed!');
sessionCfg = '';
process.exit();
});
client.on('ready', () => {
console.log('Timers are active, have fun ☕');
// send message contacts in the contacts.csv file at 9 am every day.
cron.schedule(
'0 9 * * *',
() => {
CSVToJSON()
.fromFile('contacts.csv')
.then((contacts) => {
contacts.map((data) => {
client
.sendMessage(
data.number + '@c.us',
config.messages.at_9am
)
.then((response) => {
if (response.id.fromMe) {
console.log(`Sent to ${data.name}`);
}
});
});
});
},
{
scheduled: true,
timezone: config.timezone,
}
);
// send message my contacts at night.
cron.schedule(
'0 0 * * *',
() => {
client.getContacts().then((contacts) => {
contacts.map((data) => {
client
.sendMessage(
data.number + '@c.us',
config.messages.at_night
)
.then((response) => {
if (response.id.fromMe) {
console.log(`Sent to ${data.name}`);
}
});
});
});
},
{
scheduled: true,
timezone: config.timezone,
}
);
});
client.initialize();
const createQR = (qr) => {
qrcode.generate(qr, { small: true }, function (qrcode) {
console.log(qrcode);
console.log('=============================================');
console.log('Please scan the following QR code on whatsapp.');
console.log('Press the "r" to new QR Code.');
});
};
const port = process.env.PORT || 4000;
app.listen(port, () => {
console.log('Configuring server, Please wait...');
fs.readFile('./last.qr', (err, last_qr) => {
fs.readFile('session.json', async (serr) => {
if (!err && serr) {
createQR(last_qr.toString());
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
process.stdin.on('keypress', (str, key) => {
if (key.ctrl && key.name === 'c') {
process.exit();
} else if (key.name === 'r') {
createQR(last_qr.toString());
}
});
}
});
});
});