-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
185 lines (162 loc) · 5.74 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const Config = require('./config.json')
const Kurir = require('./kurir')
const fs = require('fs')
const qrcode = require('qrcode-terminal')
const Downloader = require('./downloader')
const WebSocket = require('ws')
const _ = require('lodash')
const {
Client,
MessageMedia,
LocalAuth
} = require('whatsapp-web.js');
const downloadManager = new Downloader()
let kurir
let filePath
const client = new Client({
authStrategy: new LocalAuth(),
puppeteer: Config.puppeteer
});
// You can use an existing session and avoid scanning a QR code by adding a "session" object to the client options.
// This object must include WABrowserId, WASecretBundle, WAToken1 and WAToken2.
client.initialize();
console.log(client)
client.on('qr', (qr) => {
// NOTE: This event will not be fired if a session is specified.
console.log('QR RECEIVED', qr)
if (Config.puppeteer.headless) {
qrcode.generate(qr, {small: true})
}
});
client.on('authenticated', (session) => {
console.log('AUTHENTICATED', session);
// sessionCfg = session;
// fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function (err) {
// if (err) {
// console.error(err);
// }
// });
});
client.on('auth_failure', msg => {
// Fired if session restore was unsuccessfull
console.error('AUTHENTICATION FAILURE', msg);
});
client.on('ready', () => {
console.log('READY');
kurir = new Kurir(client)
});
client.on('message', async msg => {
if(kurir === undefined) return
if (msg.body.startsWith('/')) {
console.log(msg.body)
// Send a new message to the same chat
kurir.chat(msg);
}
});
client.on('message_create', (msg) => {
// Fired on all message creations, including your own
if (msg.fromMe) {
// do stuff here
}
});
client.on('message_revoke_everyone', async (after, before) => {
// Fired whenever a message is deleted by anyone (including you)
console.log(after); // message after it was deleted.
if (before) {
console.log(before); // message before it was deleted.
}
});
client.on('message_revoke_me', async (msg) => {
// Fired whenever a message is only deleted in your own view.
console.log(msg.body); // message before it was deleted.
});
client.on('message_ack', (msg, ack) => {
/*
== ACK VALUES ==
ACK_ERROR: -1
ACK_PENDING: 0
ACK_SERVER: 1
ACK_DEVICE: 2
ACK_READ: 3
ACK_PLAYED: 4
*/
if (ack == 3) {
// The message was read
}
});
client.on('group_join', (notification) => {
// User has joined or been added to the group.
console.log('join', notification);
notification.reply('User joined.');
});
client.on('group_leave', (notification) => {
// User has left or been kicked from the group.
console.log('leave', notification);
notification.reply('User left.');
});
client.on('group_update', (notification) => {
// Group picture, subject or description has been updated.
console.log('update', notification);
});
client.on('change_battery', (batteryInfo) => {
// Battery percentage for attached device has changed
const {
battery,
plugged
} = batteryInfo;
console.log(`Battery: ${battery}% - Charging? ${plugged}`);
});
client.on('disconnected', (reason) => {
console.log('Client was logged out', reason);
});
const wss = new WebSocket.Server({
port: Config.websocketPort
})
wss.on('connection', ws => {
ws.on('message', async message => {
//console.log(`Received message => ${message}`)
handleMessage(message);
})
})
async function handleMessage(e) {
let obj = JSON.parse(e);
if (obj.type == 'sendWa') {
// let validType = ['text', 'image', 'document', 'location', 'video']
let numbers = obj.data
let options = {}, pesannya
let tmpAttachment = {}
for (var i in numbers) {
options = numbers[i].options !== undefined ? numbers[i].options : {}
if (options.media) {
options.media = new MessageMedia(options.media.mimetype, options.media.b64data, options.media.filename)
}
if (numbers[i].url_public) {
filePath = Config.folderDownload +"/"+ numbers[i].url_public.substring(numbers[i].url_public.lastIndexOf('/') + 1);
await downloadManager.download(numbers[i].url_public,filePath)
.then(fileInfo => numbers[i].attachment = fileInfo.path)
.catch(err => console.log(err))
}
if (numbers[i].attachment !== undefined) {
if (numbers[i].attachment) {
if (fs.existsSync(numbers[i].attachment)) {
if(tmpAttachment[numbers[i].attachment] == undefined){
tmpAttachment[numbers[i].attachment] = MessageMedia.fromFilePath(numbers[i].attachment)
}
options['media'] = tmpAttachment[numbers[i].attachment]
}
}
}
// jika mimetype tidak ada dalam list mimetypecaption maka kirim dulu captionnya scecara terpisah
if(options.media){
if(!_.includes(Config.mimetypeCaption,options.media.mimetype)){
if(!_.isEmpty(numbers[i].message)){
client.sendMessage(numbers[i].to, numbers[i].message)
}
}
}
pesannya = numbers[i].message.replace(/\\n/g, '\n').replace(/\\r/g, '\r')
client.sendMessage(numbers[i].to, pesannya, options)
}
tmpAttachment = {}
}
}