-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.js
188 lines (158 loc) · 4.7 KB
/
api.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
const express = require("express");
const helmet = require("helmet");
const bodyParser = require("body-parser");
const fs = require('fs');
const axios = require('axios');
const shelljs = require('shelljs');
const config = require('./config.json');
const { Client } = require('whatsapp-web.js');
const SESSION_FILE_PATH = './wa-temp/session.json';
let sessionCfg;
if (fs.existsSync(SESSION_FILE_PATH)) {
sessionCfg = require(SESSION_FILE_PATH);
}
process.title = "Imarah Printing Whatsapp Gateway";
global.config = config;
global.client = new Client({
puppeteer: {
headless : true,
args : ['--no-sandbox','--disable-setuid-sandbox','--unhandled-rejections=strict'],
option : {}
},
session: sessionCfg
});
global.authed = false;
global.status_battery = '-';
global.status_plugged = '-';
const app = express();
const port = process.env.PORT || config.port;
app.use(helmet());
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));
//Set Request Size Limit 50 MB
app.use(bodyParser.json({limit: '50mb'}));
// Dapat QR
client.on('qr', qr => {
// console.log("Get QR", qr);
fs.writeFileSync('./wa-temp/last.qr',qr);
});
// Telah Auth
client.on('authenticated', (session) => {
console.log("AUTH!");
sessionCfg = session;
fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function(err) {
if (err) {
console.error(err);
}
authed=true;
});
try{
fs.unlinkSync('./wa-temp/last.qr')
}catch(err){}
});
// Gagal Auth
client.on('auth_failure', () => {
console.log("AUTH Failed !")
try{
fs.unlinkSync('./wa-temp/session.json')
client.initialize();
}catch(err){
console.log({msg : "Sudah Logout"})
}
//process.exit()
});
// Whatsapp Siap Digunakan
client.on('ready', () => {
console.log('Client is ready!');
});
// Pesan Diterima
client.on('message', msg => {
// console.log(msg)
let remote = msg.id.remote;
remote = remote.split('@');
let number = remote[0];
let type = remote[1];
if (type==="c.us") {
if(config.webhook.enabled){
axios.post(config.webhook.path, {msg : `Anda Menerima Pesan\nDari : wa.me/${number}\nPesan : ` + msg.body}).then(res => {
// console.log(`statusCode: ${res.statusCode}`)
console.log("Balasan Webhook", res.data)
})
.catch(error => {
// console.error(error)
})
}
}
});
// Pesan Dikirim
client.on('message_ack', (msg, ack) => {
switch (ack) {
case -1:
console.log("Error", msg);
break;
case 0:
console.log("Pending", msg);
break;
case 1:
console.log("Diterima Server", msg);
break;
case 2:
console.log("Pesan Diterima", msg);
break;
case 3:
console.log("Pesan Dibaca", msg);
break;
case 4:
console.log("Pesan Dibalas", msg);
break;
default:
console.log("Undefined", msg);
break;
}
});
client.on('change_battery', (batteryInfo) => {
// Battery percentage for attached device has changed
const { battery, plugged } = batteryInfo;
console.log(`Battery: ${battery}% - Charging? ${plugged}`);
status_battery = battery;
status_plugged = plugged;
});
client.on('disconnected', (reason) => {
try{
fs.unlinkSync('./wa-temp/session.json')
client.initialize();
}catch(err){
console.log({msg : "Sudah Logout"})
}
console.log('Client was logged out', reason);
});
client.on('change_state', (reason) => {
console.log('Client was change state', reason);
});
client.initialize();
const chatRoute = require('./components/chatting');
const groupRoute = require('./components/group');
const authRoute = require('./components/auth');
const contactRoute = require('./components/contact');
const {login, refresh} = require('./components/authentication')
const {verify} = require('./components/middleware')
app.use(function(req, res, next){
console.log(req.method + ' : ' + req.path);
next();
});
app.post('/api/v1/whatsapp/auth', login);
app.post('/api/v1/whatsapp/refresh', verify, refresh);
app.use('/api/v1/whatsapp', verify, authRoute);
app.use('/api/v1/whatsapp/send', verify, chatRoute);
app.use('/api/v1/whatsapp/send/group', verify, groupRoute);
app.use('/api/v1/whatsapp/endpoint/contact', verify, contactRoute);
app.use('*', function(req, res){
res.status(404).send({
status : false,
message : "Page Not Found!",
data : null
});
});
app.listen(port, () => {
console.log("Server Running Live on Port : " + port);
});