swan-api 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Message.js +2 -2
- package/Socket.js +42 -12
- package/package.json +1 -1
package/Message.js
CHANGED
|
@@ -31,7 +31,7 @@ export default class Message {
|
|
|
31
31
|
const ctx = m.extendedTextMessage?.contextInfo;
|
|
32
32
|
if (ctx?.quotedMessage){
|
|
33
33
|
this.quoted = new Message(socket, {
|
|
34
|
-
key: {remoteJid: this.fromId, fromMe: ctx.participant === socket.
|
|
34
|
+
key: {remoteJid: this.fromId, fromMe: ctx.participant === socket.userId},
|
|
35
35
|
message: ctx.quotedMessage
|
|
36
36
|
});
|
|
37
37
|
}
|
|
@@ -52,7 +52,7 @@ export default class Message {
|
|
|
52
52
|
|
|
53
53
|
// returns the sender's name and phone number
|
|
54
54
|
async getContact() {
|
|
55
|
-
const jid = this.fromMe ? this.socket.
|
|
55
|
+
const jid = this.fromMe ? this.socket.userId : this.author;
|
|
56
56
|
const number = jid.split('@')[0];
|
|
57
57
|
return {name: this.fromName, number}
|
|
58
58
|
}
|
package/Socket.js
CHANGED
|
@@ -10,13 +10,35 @@ import MessageMedia from './MessageMedia.js';
|
|
|
10
10
|
|
|
11
11
|
//Wrapper Class For Baileys Socket
|
|
12
12
|
export default class Socket extends EventEmitter {
|
|
13
|
-
|
|
14
|
-
constructor() {
|
|
13
|
+
constructor(){
|
|
15
14
|
super();
|
|
16
|
-
this.
|
|
15
|
+
this.muteSessionErrors()
|
|
16
|
+
this.connect()
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
//
|
|
19
|
+
// hide internal session errors that does not affect the use
|
|
20
|
+
muteSessionErrors(){
|
|
21
|
+
const oldLog = console.log;
|
|
22
|
+
console.log = (...args) => {
|
|
23
|
+
if (String(args[0]).includes("Closing session"))
|
|
24
|
+
return;
|
|
25
|
+
|
|
26
|
+
oldLog(...args);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const oldError = console.error;
|
|
30
|
+
console.error = (...args) => {
|
|
31
|
+
if (
|
|
32
|
+
String(args[0]).includes("Failed to decrypt") ||
|
|
33
|
+
String(args[0]).includes("Session error")
|
|
34
|
+
)
|
|
35
|
+
return;
|
|
36
|
+
|
|
37
|
+
oldError(...args);
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// creates a Socket instance and tries to authenticate
|
|
20
42
|
async connect() {
|
|
21
43
|
await this.authenticate();
|
|
22
44
|
this.bindEvents();
|
|
@@ -26,16 +48,22 @@ export default class Socket extends EventEmitter {
|
|
|
26
48
|
async authenticate(){
|
|
27
49
|
const { state, saveCreds } = await useMultiFileAuthState('auth')
|
|
28
50
|
const { version } = await fetchLatestBaileysVersion()
|
|
51
|
+
const logger = P({level: 'silent'})
|
|
29
52
|
|
|
30
|
-
this.socket = makeWASocket({version, auth: state, logger
|
|
31
|
-
this.
|
|
53
|
+
this.socket = makeWASocket({version, auth: state, logger});
|
|
54
|
+
this.userId = this.socket.user.id;
|
|
32
55
|
this.socket.ev.on('creds.update', saveCreds)
|
|
33
56
|
}
|
|
34
57
|
|
|
35
58
|
//bind events from baileys to default treatment functions
|
|
36
59
|
bindEvents(){
|
|
60
|
+
let socketReady = false
|
|
61
|
+
|
|
37
62
|
//each new message emits a 'newMessage' event and a 'Message' object
|
|
38
63
|
this.socket.ev.on('messages.upsert', ({messages}) => {
|
|
64
|
+
if (!socketReady)
|
|
65
|
+
return;
|
|
66
|
+
|
|
39
67
|
for (const msg of messages){
|
|
40
68
|
if (!msg.message)
|
|
41
69
|
continue;
|
|
@@ -45,7 +73,7 @@ export default class Socket extends EventEmitter {
|
|
|
45
73
|
|
|
46
74
|
//emits 'ready' when opens connection
|
|
47
75
|
//on connnection lost, tries to reconnect
|
|
48
|
-
this.socket.ev.on('connection.update', (update) => {
|
|
76
|
+
this.socket.ev.on('connection.update', async (update) => {
|
|
49
77
|
const { connection, lastDisconnect, qr } = update;
|
|
50
78
|
if (qr)
|
|
51
79
|
qrcode.generate(qr, {small: true})
|
|
@@ -54,9 +82,11 @@ export default class Socket extends EventEmitter {
|
|
|
54
82
|
const shouldReconnect = lastDisconnect?.error?.output?.statusCode
|
|
55
83
|
!== DisconnectReason.loggedOut;
|
|
56
84
|
if (shouldReconnect)
|
|
57
|
-
this.connect()
|
|
58
|
-
} else if (connection == 'open')
|
|
85
|
+
await this.connect()
|
|
86
|
+
} else if (connection == 'open'){
|
|
87
|
+
soketReady = true
|
|
59
88
|
this.emit('ready');
|
|
89
|
+
}
|
|
60
90
|
});
|
|
61
91
|
}
|
|
62
92
|
|
|
@@ -80,7 +110,7 @@ export default class Socket extends EventEmitter {
|
|
|
80
110
|
const isVideo = mimetype?.startsWith('video') || mimetype === 'image/gif'
|
|
81
111
|
// video proportion must be 'full' to not get corrupted frames
|
|
82
112
|
if (isVideo)
|
|
83
|
-
opts.
|
|
113
|
+
opts.stickerType = 'full'
|
|
84
114
|
|
|
85
115
|
// tries to create the sticker at the highest quality,
|
|
86
116
|
// if the resulting file exceeds WhatsApp's 1MB limit
|
|
@@ -89,9 +119,9 @@ export default class Socket extends EventEmitter {
|
|
|
89
119
|
let final_quality = 1;
|
|
90
120
|
while (final_quality >= 0) {
|
|
91
121
|
const sticker = new Sticker(buffer, {
|
|
92
|
-
pack: opts.
|
|
122
|
+
pack: opts.stickerPack,
|
|
93
123
|
author: opts.stickerAuthor,
|
|
94
|
-
type: opts.
|
|
124
|
+
type: opts.stickerType,
|
|
95
125
|
quality: final_quality,
|
|
96
126
|
});
|
|
97
127
|
|