forked from RynKings/wabotjs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.js
411 lines (382 loc) · 13.3 KB
/
helper.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
const {
MessageType,
Mimetype
} = require("@adiwajshing/baileys");
const fs = require('fs');
const con = require("./connect")
const fetch = require('node-fetch');
const request = require('request');
const { exec } = require("child_process");
const util = require('util')
const streamPipeline = util.promisify(require('stream').pipeline)
const wa = con.Whatsapp
async function downloadURL(url, filename) {
const response = await fetch(url)
if (!response.ok) throw new Error(`unexpected response ${response.statusText}`)
await streamPipeline(response.body, fs.createWriteStream(filename))
return response.headers.get('content-type')
}
function normalizeMention(to, txt, mention=[]){
if(! mention.length > 0){
return "Not Mentioned"
}
if(txt.includes("@!")){
const texts = txt.split('@!')
let textx = ''
if(texts.length -1 !== mention.length){
return "Invalid Number"
}
for(i of mention){
textx += texts[mention.indexOf(i)]
textx +="@"+i.replace("@s.whatsapp.net", "")
}
textx += texts[mention.length]
return textx
}else return "Invalid Mention Position"
}
exports.getRandom = (ext) => {
return `${Math.floor(Math.random() * 10000)}${ext}`
}
exports.serialize = function(chat){
m = JSON.parse(JSON.stringify(chat))
content = m.message
//.text = m.message.conversation
m.isGroup = m.key.remoteJid.endsWith('@g.us')
try{
const berak = Object.keys(content)[0]
m.type = berak
} catch {
m.type = null
}
try{
const context = m.message.extendedTextMessage.contextInfo.quotedMessage
m.quoted = context
}catch{
m.quoted = null
}
try{
const mention = m.message[m.type].contextInfo.mentionedJid
m.mentionedJid = mention
}catch{
m.mentionedJid = null
}
if (m.isGroup){
m.sender = m.participant
}else{
m.sender = m.key.remoteJid
}
if (m.key.fromMe){
m.sender = wa.user.jid
}
txt = (m.type === 'conversation' && m.message.conversation) ? m.message.conversation : (m.type == 'imageMessage') && m.message.imageMessage.caption ? m.message.imageMessage.caption : (m.type == 'documentMessage') && m.message.documentMessage.caption ? m.message.documentMessage.caption : (m.type == 'videoMessage') && m.message.videoMessage.caption ? m.message.videoMessage.caption : (m.type == 'extendedTextMessage') && m.message.extendedTextMessage.text ? m.message.extendedTextMessage.text : ""
m.text = txt
return m
}
//serialize number
function serializeNumberList(numbers){
var n = []
for (i in numbers){
n.push(serializeNumber(numbers[i]))
}
return n
}
function serializeNumber(number){
return normalizeNumber(number) + "@s.whatsapp.net"
}
function normalizeNumber(number){
return number.replace("@s.whatsapp.net", "").replace("@c.us","")
}
//download file url
exports.downloadFile = function (uri, filename, callback) {
request.head(uri, function (err, res, body) {
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
})
}
exports.sleep = async function(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
//sebd message
exports.sendMessage = async function(to, text){
return wa.sendMessage(to, text, MessageType.text)
}
//send image
exports.sendImage = function(to, filename, text=""){
const bufer = fs.readFileSync(filename)
return wa.sendMessage(to, bufer, MessageType.image, {caption: text})
}
//send Audio
exports.sendAudio = function(to, filename, text=""){
const bufer = fs.readFileSync(filename)
wa.sendMessage(to, bufer, MessageType.mp4Audio, { mimetype: Mimetype.mp4Audio, ptt: true})
}
//send Pdf
exports.sendPdf = function(to, filename, text){
const bufer = fs.readFileSync(filename)
wa.sendMessage(to, bufer, MessageType.document, { mimetype: Mimetype.pdf, filename: text})
}
//send Video
exports.sendVideo = function(to, filename, text=""){
const bufer = fs.readFileSync(filename)
wa.sendMessage(to, bufer, MessageType.video, {caption: text})
}
//send GIF
exports.sendGif = function(to, filename, text=""){
const bufer = fs.readFileSync(filename)
wa.sendMessage(to, bufer,MessageType.video, { mimetype: Mimetype.gif, caption: text })
}
exports.sendSticker = function(to, filename){
const bufer = fs.readFileSync(filename)
wa.sendMessage(to, bufer, MessageType.sticker)
}
//
exports.sendStickerUrl = async(to, url) => {
var names = Date.now() / 10000;
var download = function (uri, filename, callback) {
request.head(uri, function (err, res, body) {
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
download(url, './media/sticker/' + names + '.png', async function () {
console.log('done');
let filess = './media/sticker/' + names + '.png'
let asw = './media/sticker/' + names + '.webp'
exec(`ffmpeg -i ${filess} -vcodec libwebp -filter:v fps=fps=20 -lossless 1 -loop 0 -preset default -an -vsync 0 -s 512:512 ${asw}`, (err) => {
let media = fs.readFileSync(asw)
wa.sendMessage(to, media, MessageType.sticker)
});
});
}
//send media with url
exports.sendMediaURL = async(to, url, text="", mids=[]) =>{
if(mids.length > 0){
text = normalizeMention(to, text, mids)
}
const fn = Date.now() / 10000;
const filename = fn.toString()
let mime = ""
var download = function (uri, filename, callback) {
request.head(uri, function (err, res, body) {
mime = res.headers['content-type']
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
download(url, filename, async function () {
console.log('done');
let media = fs.readFileSync(filename)
let type = mime.split("/")[0]+"Message"
if(mime === "image/gif"){
type = MessageType.video
mime = Mimetype.gif
}
if(mime.split("/")[0] === "audio"){
mime = Mimetype.mp4Audio
}
wa.sendMessage(to, media, type, { mimetype: mime, caption: text,contextInfo: {"mentionedJid": mids}})
fs.unlinkSync(filename)
});
}
//send contact
exports.sendContact = function(to, id, name){
const vcard = 'BEGIN:VCARD\n' + 'VERSION:3.0\n' + 'FN:' + name + '\n' + 'ORG:Kontak\n' + 'TEL;type=CELL;type=VOICE;waid=' + id.split("@s.whatsapp.net")[0] + ':+' + id.split("@s.whatsapp.net")[0] + '\n' + 'END:VCARD'
wa.sendMessage(to, {displayname: name, vcard: vcard}, MessageType.contact)
}
//send reply
exports.sendReply = function(to, text){
wa.sendMessage(to, txt, MessageType.text, { quoted: ma})
/*const txt = normalizeMention(to, text, mids)
if (txt) {
wa.sendMessage(to, txt, MessageType.text, { quoted: m})
}*/
}
exports.sendMention = function(to, text, mids=[]){
const txt = normalizeMention(to, text, mids)
if(txt){
wa.sendMessage(to, txt, MessageType.text, {contextInfo: {"mentionedJid": mids}})
}
}
// GROUP
exports.getGroup = async function(to) {
const response = await wa.groupMetadata(to)
return JSON.parse(JSON.stringify(response))
}
exports.getParticipantIds = async function(to) {
var mat = await wa.groupMetadata(to)
var members = mat.participants
let mids = []
for (let member of members) {
mids.push(member.jid)
}
return JSON.parse(JSON.stringify(mids))
}
exports.getAdminIds = async function(to) {
var group = await wa.groupMetadata(to)
var participants = group.participants
let admin = []
for (let members of participants) {
if (members.isAdmin) {
admin.push(members.jid)
}
}
return JSON.parse(JSON.stringify(admin))
}
exports.getOwnerIds = async function(to) {
var group = await wa.groupMetadata(to)
var participants = group.participants
let admin = []
for (let members of participants) {
if (members.isSuperAdmin) {
admin.push(members.jid)
}
}
return JSON.parse(JSON.stringify(admin))
}
// All Groups
exports.getGroups = async function() {
const totalchat = await wa.chats.all()
let a = []
let gid = []
for (mem of totalchat){
a.push(mem.jid)
}
for (id of a){
if (id && id.includes('g.us')){
gid.push(id)
}
}
return JSON.parse(JSON.stringify(gid))
}
//Link Group
exports.getGroupInvitationCode = async function(to) {
const linkgc = await wa.groupInviteCode(to)
const code = "https://chat.whatsapp.com/"+linkgc
return code
}
exports.kickMember = async function(to, target=[]){
if(!target.length > 0) { return wa.sendMessage(to, "No target..") }
const g = await wa.groupMetadata(to)
const owner = g.owner.replace("c.us","s.whatsapp.net")
const me = wa.user.jid
for (i of target){
if (!i.includes(me) && !i.includes(owner)){
const res = await wa.groupRemove(to, [i])
console.log(res)
}else{
wa.sendMessage(to, "NOT PREMITED")
}
}
}
exports.promoteAdmin = async function(to, target=[]){
if(!target.length > 0) { return wa.sendMessage(to, "No target..") }
const g = await wa.groupMetadata(to)
const owner = g.owner.replace("c.us","s.whatsapp.net")
const me = wa.user.jid
for (i of target){
if (!i.includes(me) && !i.includes(owner)){
const res = await wa.groupMakeAdmin(to, [i])
console.log(res)
}else{
wa.sendMessage(to, "NOT PREMITED")
}
}
}
exports.demoteAdmin = async function(to, target=[]){
if(!target.length > 0) { return wa.sendMessage(to, "No target..") }
const g = await wa.groupMetadata(to)
const owner = g.owner.replace("c.us","s.whatsapp.net")
const me = wa.user.jid
for (i of target){
if (!i.includes(me) && !i.includes(owner)){
const res = await wa.groupDemoteAdmin(to, [i])
console.log(res)
}else{
wa.sendMessage(to, "NOT PREMITED")
}
}
}
exports.leaveGroup = function(to){
res = wa.groupLeave(to)
return res
}
exports.getContact = function(idx) {
try {
const r = JSON.parse(JSON.stringify(wa.contacts[idx]))
return r
} catch (e) {
console.log('Error : '+ e)
}
}
exports.getBio = async function(mids) {
const pdata = await wa.getStatus(mids)
if (pdata.status == 401) { // Untuk Self-Bot
return pdata.status
} else {
return pdata.status
}
}
exports.getPict = async function(mids) {
try {
var url = await wa.getProfilePicture(mids)
} catch {
var url = 'https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_960_720.png?q=60'
}
return url
}
//Hide tag
exports.hideTag = async function(to, text){
var mat = await wa.groupMetadata(to)
var members = mat.participants
let mids = []
for (let member of members) {
mids.push(member.jid)
}
wa.sendMessage(to, text, MessageType.text, {
contextInfo: {
"mentionedJid": mids
}
})
}
//Fake Reply
exports.fakeReply = async function(to, target, text, prevtext, mention=[], msgId="B826873620DD5947E683E3ABE663F263"){
mention = m.message.extendedTextMessage.contextInfo.mentionedJid[0]
wa.sendMessage(to, text, MessageType.text, {
contextInfo: {"mentionedJid": mention, "stanzaId": msgId,"participant": target,"quotedMessage": {"conversation": prevtext}}
})
}
exports.fakeReply2 = async function(to, target, text, prevtext, mention=[], msgId="B826873620DD5947E683E3ABE663F263"){
mention = m.message.extendedTextMessage.contextInfo.mentionedJid[0]
wa.sendMessage(to, text, MessageType.text, {
contextInfo: {"mentionedJid": mention, "stanzaId": msgId, "participant": target,"quotedMessage": {"conversation": prevtext}}
})
}
exports.setName = async function(query){
const response = await wa.updateProfileName(query)
return response
}
exports.setBio = async function(query){
const response = await wa.setStatus(query)
return response
}
exports.sendReply = function(to, text, mids=[]){
if(mids.length > 0){
text = normalizeMention(to, text, mids)
}
if (m.isGroup){
m.sender = m.participant
}else{
m.sender = m.key.remoteJid
}
ini = m.key.id
wa.sendMessage(to, text, MessageType.extendedText, { contextInfo: {"mentionedJid": mids,"stanzaId": ini, "participant": m.sender, "quotedMessage": { "conversation": text}} })
}
exports.sendReplyWA = function(to, text, prevtext, mids=[]){
if(mids.length > 0){
text = normalizeMention(to, text, mids)
}
waid = "[email protected]"
ini = m.key.id
wa.sendMessage(to, text, MessageType.extendedText, { contextInfo: {"mentionedJid": mids,"stanzaId": ini, "participant": waid, "quotedMessage": { "conversation": prevtext}} })
}
exports.serializeNumberList = serializeNumberList;
exports.serializeNumber = serializeNumber;
exports.normalizeNumber = normalizeNumber;
exports.downloadURL = downloadURL;