-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
114 lines (97 loc) · 2.5 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
import { create } from 'venom-bot';
export class InitVenomClient {
#sessionName;
#client;
constructor(sessionName) {
this.#sessionName = sessionName
}
async init() {
create({ session: this.#sessionName })
.then((client) => {
this.#client = client
this.start(client)
})
.catch((erro) => {
console.log(erro);
});
}
chooseFnc(message) {
const fnc = message.body;
const from = message.from;
const functions = {
text: () => this.sendText(from),
image: () => this.sendImage(from),
buttons: () => this.sendButtons(from),
default: 'Invalid Option!'
}
return functions[fnc] ? functions[fnc]() : functions.default;
}
/*
Recebe uma mensagem e retorna a função equivalente.
Ex: Escuta a mensagem 'text' e chama a função 'sendText', que envia um texto simples.
*/
start(client) {
client.onMessage(async (message) => {
try {
if (!message.isGroupMsg) {
await this.chooseFnc(message)
}
} catch (error) {
throw error
}
});
}
async sendText(to, text) {
const message = text || '👋 Hello from venom! This is a basic text exemple.';
const response = await this.#client
.sendText(to, message)
.then(() => {
return 'success'
})
.catch((erro) => {
throw erro
});
return response;
}
/* Visíveis apenas em apenas WhatsApp web e IOS */
async sendButtons(to, buttons) {
const buttonsArr = buttons || [
{
"buttonText": {
"displayText": "Text of Button 1"
}
},
{
"buttonText": {
"displayText": "Text of Button 2"
}
}
];
const response = await this.#client
.sendButtons(to, 'Title: Buttons example', buttonsArr, 'Description: This is a basic buttons exemple.')
.then(() => 'success')
.catch((erro) => {
throw erro
});
return response;
}
/* Necessário alteração no node_modules (V<=^5.0.21) */
async sendImage(to, image, imageName, captionText) {
const img = image || './assets/sad_christmas_tree.jpg';
const imgName = imageName || 'Image name';
const captionTxt = captionText || 'Caption text';
const response = await this.#client
.sendImage(
to,
img,
imgName,
captionTxt
)
.then(() => 'success')
.catch((erro) => {
console.log('erro', erro)
throw erro
});
return response
}
}