-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
164 lines (162 loc) · 5.11 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
const venom = require("venom-bot");
const express = require("express");
const { body, validationResult } = require("express-validator");
const { phoneNumberFormatter } = require("./helpers/index");
const app = express();
const port = 3000;
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const swaggerUi = require("swagger-ui-express");
const swaggerDocument = require("./swagger.json");
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
venom
.create(
"venomBot",
asciiQR => {
console.log("Terminal qrcode: ");
},
statusSession => {
console.log("Status Session: ", statusSession); //return isLogged || notLogged || browserClose || qrReadSuccess || qrReadFail || autocloseCalled
},
{
folderNameToken: "tokens", //folder name when saving tokens
mkdirFolderToken: "", //folder directory tokens, just inside the venom folder, example: { mkdirFolderToken: '/node_modules', } //will save the tokens folder in the node_modules directory
headless: true, // Headless chrome
devtools: false, // Open devtools by default
useChrome: true, // If false will use Chromium instance
debug: false, // Opens a debug session
logQR: true, // Logs QR automatically in terminal
browserArgs: ["--no-sandbox"], // Parameters to be added into the chrome browser instance
disableSpins: true, // Will disable Spinnies animation, useful for containers (docker) for a better log
disableWelcome: true, // Will disable the welcoming message which appears in the beginning
updatesLog: true, // Logs info updates automatically in terminal
autoClose: 60000, // Automatically closes the venom-bot only when scanning the QR code (default 60 seconds, if you want to turn it off, assign 0 or false)
}
)
.then(client => start(client))
.catch(erro => {
console.log(erro);
});
function start(client) {
app.get("/", (req, res) => {
res.send({
status: true,
message: "Bot Running",
});
});
app.post(
"/sendText",
[body("number").notEmpty(), body("message").notEmpty()],
async (req, res) => {
const errors = validationResult(req).formatWith(({ msg }) => {
return msg;
});
if (!errors.isEmpty()) {
return res.status(422).json({
status: false,
message: errors.mapped(),
});
}
const { number, message, image, caption } = req.body;
const formatterNumber = phoneNumberFormatter(number);
await client
.sendText(formatterNumber, message)
.then(result => {
res.status(200).json({
status: true,
response: result,
});
console.log(
`{susccess: true,message : "pesan kirim ke ${number} : '${message}'"}`
);
})
.catch(erro => {
res.status(500).json({
status: false,
response: erro,
});
});
}
);
app.post(
"/sendPdf",
[
body("number").notEmpty(),
body("message").notEmpty(),
body("pdf").notEmpty(),
body("caption").notEmpty(),
],
async (req, res) => {
const errors = validationResult(req).formatWith(({ msg }) => {
return msg;
});
if (!errors.isEmpty()) {
return res.status(422).json({
status: false,
message: errors.mapped(),
});
}
const { number, message, pdf, caption } = req.body;
const formatterNumber = phoneNumberFormatter(number);
await client
.sendFile(formatterNumber, pdf, "pdf-name", caption)
.then(result => {
res.status(200).json({
status: true,
response: result,
});
console.log(
`{susccess: true,message : "pesan pdf kirim ke ${number} : '${message}'"}`
);
})
.catch(erro => {
res.status(500).json({
status: false,
response: erro,
});
});
}
);
app.post(
"/sendImage",
[
body("number").notEmpty(),
body("message").notEmpty(),
body("image").notEmpty(),
body("caption").notEmpty(),
],
async (req, res) => {
const errors = validationResult(req).formatWith(({ msg }) => {
return msg;
});
if (!errors.isEmpty()) {
return res.status(422).json({
status: false,
message: errors.mapped(),
});
}
const { number, message, image, caption } = req.body;
const formatterNumber = phoneNumberFormatter(number);
await client
.sendImage(formatterNumber, image, "image-name", caption)
.then(result => {
res.status(200).json({
status: true,
response: result,
});
console.log(
`{susccess: true,message : "pesan image kirim ke ${number} : '${message}'"}`
);
})
.catch(erro => {
res.status(500).json({
status: false,
response: erro,
});
});
}
);
}