Repository navigation
Expand file tree
/
Copy pathapi.js
More file actions
117 lines (100 loc) · 2.77 KB
/
Copy pathapi.js
File metadata and controls
117 lines (100 loc) · 2.77 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
const express = require("express");
const bodyParser = require("body-parser");
const fs = require("fs");
const axios = require("axios");
const cors = require('cors');
// const childProcess = require('child_process');
const config = require("./config.json");
const { Client, LocalAuth } = require("whatsapp-web.js");
process.title = "whatsapp-node-api";
global.client = new Client({
authStrategy: new LocalAuth(),
puppeteer: {
headless: true,
args: [
"--no-sandbox",
"--disable-setuid-sandbox",
"--unhandled-rejections=strict",
"--disable-dev-shm-usage",
"--fast-start",
]
},
});
global.authed = false;
const app = express();
const port = process.env.PORT || config.port;
//Set Request Size Limit 50 MB
app.use(bodyParser.json({ limit: "50mb" }));
app.use(cors());
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true }));
const generateQR = () => {
console.log("Running Generate QR");
client.on("qr", (qr) => {
console.log("qr ", qr );
fs.writeFileSync("./components/last.qr", qr);
});
}
const selfRestart = () => {
console.log("This is pid " + process.pid);
setTimeout(function () {
process.on("exit", function () {
require("child_process").spawn(process.argv.shift(), process.argv, {
cwd: process.cwd(),
detached : true,
stdio: "inherit"
});
});
process.exit();
}, 5000);
}
generateQR();
client.on("authenticated", () => {
console.log("AUTH!");
authed = true;
try {
fs.unlinkSync("./components/last.qr");
} catch (err) {}
});
client.on("auth_failure", () => {
console.log("AUTH Failed !");
process.exit();
});
client.on("ready", () => {
console.log("Client is ready!");
});
client.on("message", async (msg) => {
if (config.webhook.enabled) {
if (msg.hasMedia) {
const attachmentData = await msg.downloadMedia();
msg.attachmentData = attachmentData;
}
axios.post(config.webhook.path, { msg });
}
});
client.on("disconnected", () => {
console.log("disconnected");
authed = false;
setTimeout(() => {
selfRestart();
// process.exit(1);
}, 500);
});
client.initialize();
// const chatRoute = require("./components/chatting");
// const groupRoute = require("./components/group");
// const authRoute = require("./components/auth");
// const contactRoute = require("./components/contact");
const puteriRoute = require("./components/puteri");
app.use(function (req, res, next) {
console.log(req.method + " : " + req.path);
next();
});
// app.use("/chat", chatRoute);
// app.use("/group", groupRoute);
// app.use("/auth", authRoute);
// app.use("/contact", contactRoute);
app.use("/puteri", puteriRoute);
app.listen(port, () => {
console.log("Server Running Live on Port : " + port);
});