-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
59 lines (52 loc) · 1.61 KB
/
Copy pathapp.js
File metadata and controls
59 lines (52 loc) · 1.61 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
const express = require('express');
const { getCalendarClient } = require('./src/calendarClient');
const { wppClient } = require('./src/wppClient');
const app = express()
const port = 6001;
let lastQR = null;
let wppAuth = false;
const allResponses = {}
/**
* Starts the bot application, initializing the WhatsApp and calendar clients, and setting up event listeners for incoming messages and authentication.
*/
const start = () => {
const calendarClient = getCalendarClient()
wppClient({
onQrGenerate: (svg) => {
lastQR = svg;
},
onAuthenticated: () => {
wppAuth = true;
calendarClient.auth()
},
onMessage: async (msj) => {
const chat = await msj.getChat()
const contact = await chat.getContact()
const chatName = contact.name.split(" ")[0];
const isGroup = chat.isGroup;
if (!msj.fromMe && !isGroup) {
const eventsData = await calendarClient.getListEvents()
if (eventsData?.oneEventIsActive) {
if (chat.id.user in allResponses) {
// send message if userid is inside allResponses
// return
}
allResponses[chat.id.user] = {
lastResponse: new Date(),
counter: 0
}
await msj.reply(`🤖 ${chatName} Estoy en una reunion ahora mismo, escribeme luego, muchas gracias. ( _mensaje por bot_ )`)
await chat.markUnread()
}
}
}
});
}
app.get('/', (req, res) => {
if (wppAuth) return res.send("wpp authenticated");
res.send(lastQR)
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
start()
})