voice-calls-baileys 1.0.0
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/.eslintrc.json +42 -0
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/package.json +22 -0
- package/src/demo.ts +39 -0
- package/src/index.ts +3 -0
- package/src/services/transport.model.ts +157 -0
- package/src/services/transport.type.ts +48 -0
- package/tsconfig.json +30 -0
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"env": {
|
|
3
|
+
"browser": true,
|
|
4
|
+
"es2021": true
|
|
5
|
+
},
|
|
6
|
+
"extends": [
|
|
7
|
+
"eslint:recommended",
|
|
8
|
+
"plugin:@typescript-eslint/recommended"
|
|
9
|
+
],
|
|
10
|
+
"parser": "@typescript-eslint/parser",
|
|
11
|
+
"parserOptions": {
|
|
12
|
+
"sourceType": "module",
|
|
13
|
+
"project": "./tsconfig.json"
|
|
14
|
+
},
|
|
15
|
+
"plugins": [
|
|
16
|
+
"@typescript-eslint"
|
|
17
|
+
],
|
|
18
|
+
"rules": {
|
|
19
|
+
"@typescript-eslint/no-explicit-any": [
|
|
20
|
+
"warn",
|
|
21
|
+
{
|
|
22
|
+
"ignoreRestArgs": true
|
|
23
|
+
}
|
|
24
|
+
],
|
|
25
|
+
"@typescript-eslint/no-inferrable-types": [
|
|
26
|
+
"warn"
|
|
27
|
+
],
|
|
28
|
+
"@typescript-eslint/no-redundant-type-constituents": [
|
|
29
|
+
"warn"
|
|
30
|
+
],
|
|
31
|
+
"@typescript-eslint/no-unnecessary-type-assertion": [
|
|
32
|
+
"warn"
|
|
33
|
+
],
|
|
34
|
+
"no-restricted-syntax": [
|
|
35
|
+
"warn",
|
|
36
|
+
{
|
|
37
|
+
"selector": "TSEnumDeclaration",
|
|
38
|
+
"message": "Don't declare enums, use literals instead"
|
|
39
|
+
}
|
|
40
|
+
]
|
|
41
|
+
}
|
|
42
|
+
}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Bob
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# voice-calls-baileys
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "voice-calls-baileys",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Integração de chamadas de voz a biblioteca baileys",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "ts-node-dev --inspect --transpile-only --ignore-watch node_modules src/index.ts"
|
|
8
|
+
},
|
|
9
|
+
"author": "Wavoip",
|
|
10
|
+
"license": "ISC",
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@types/node": "^20.14.11",
|
|
13
|
+
"ts-node": "^10.9.2",
|
|
14
|
+
"typescript": "^5.5.3"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@types/socket.io-client": "^3.0.0",
|
|
18
|
+
"baileys": "^6.7.5",
|
|
19
|
+
"socket.io-client": "^4.7.5",
|
|
20
|
+
"ts-node-dev": "^2.0.0"
|
|
21
|
+
}
|
|
22
|
+
}
|
package/src/demo.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { useVoiceCallsBaileys } from "./services/transport.model";
|
|
2
|
+
|
|
3
|
+
import makeWASocket, { Browsers, DisconnectReason, useMultiFileAuthState } from "baileys";
|
|
4
|
+
import P from "pino";
|
|
5
|
+
import { Boom } from '@hapi/boom';
|
|
6
|
+
|
|
7
|
+
async function connectToWhatsApp() {
|
|
8
|
+
const { state, saveCreds } = await useMultiFileAuthState("voice_call_baileys");
|
|
9
|
+
const sock = makeWASocket({
|
|
10
|
+
printQRInTerminal: true,
|
|
11
|
+
auth: state,
|
|
12
|
+
browser: Browsers.macOS('Desktop'),
|
|
13
|
+
logger: P({ level: "error" }),
|
|
14
|
+
syncFullHistory: false,
|
|
15
|
+
markOnlineOnConnect: false
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
useVoiceCallsBaileys("your token", sock, P({ level: "debug" }))
|
|
19
|
+
|
|
20
|
+
sock.ev.on("creds.update", saveCreds);
|
|
21
|
+
|
|
22
|
+
sock.ev.on('connection.update', (update) => {
|
|
23
|
+
const { connection, lastDisconnect } = update
|
|
24
|
+
if (connection === 'open') {
|
|
25
|
+
console.log('opened connection');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (connection === "close") {
|
|
29
|
+
if ([DisconnectReason.loggedOut, DisconnectReason.forbidden].includes((lastDisconnect?.error as Boom)?.output?.statusCode) ) {
|
|
30
|
+
console.log("Connection close")
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
connectToWhatsApp()
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
connectToWhatsApp()
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { io, Socket } from "socket.io-client";
|
|
2
|
+
|
|
3
|
+
import { ClientToServerEvents, ServerToClientEvents } from "./transport.type";
|
|
4
|
+
|
|
5
|
+
import { ConnectionState, WAConnectionState, WASocket } from "baileys";
|
|
6
|
+
|
|
7
|
+
import { Logger } from "pino";
|
|
8
|
+
|
|
9
|
+
let baileys_connection_state: WAConnectionState = "close";
|
|
10
|
+
|
|
11
|
+
export const useVoiceCallsBaileys = async ( wavoip_token: string, baileys_sock: WASocket, logger?: Logger) => {
|
|
12
|
+
const socket: Socket<ServerToClientEvents, ClientToServerEvents> = io("https://devices.wavoip.com/baileys", {
|
|
13
|
+
transports: ['websocket'],
|
|
14
|
+
path: `/${wavoip_token}/websocket`,
|
|
15
|
+
forceNew: true
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
socket.on("connect", () => {
|
|
19
|
+
logger?.debug("[*] - Wavoip connected", socket.id);
|
|
20
|
+
|
|
21
|
+
socket.emit("init", baileys_sock.authState.creds.me, baileys_sock.authState.creds.account, baileys_connection_state);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
socket.on("disconnect", () => {
|
|
25
|
+
logger?.debug("[*] - Wavoip disconnect");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
socket.on("connect_error", (error) => {
|
|
29
|
+
if (socket.active) {
|
|
30
|
+
logger?.debug("[*] - Wavoip connection error temporary failure, the socket will automatically try to reconnect", error);
|
|
31
|
+
} else {
|
|
32
|
+
logger?.debug("[*] - Wavoip connection error", error.message);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
socket.on("onWhatsApp", async (jid, callback) => {
|
|
37
|
+
try {
|
|
38
|
+
const response = await baileys_sock.onWhatsApp(jid);
|
|
39
|
+
|
|
40
|
+
callback(response);
|
|
41
|
+
|
|
42
|
+
logger?.debug("[*] Success on call onWhatsApp function", response, jid);
|
|
43
|
+
} catch (error) {
|
|
44
|
+
logger?.error("[*] Error on call onWhatsApp function", error);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
socket.on("profilePictureUrl", async (jid, type, timeoutMs, callback) => {
|
|
49
|
+
try {
|
|
50
|
+
const response = await baileys_sock.profilePictureUrl(jid, type, timeoutMs);
|
|
51
|
+
|
|
52
|
+
callback(response);
|
|
53
|
+
|
|
54
|
+
logger?.debug("[*] Success on call profilePictureUrl function", response);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
logger?.error("[*] Error on call profilePictureUrl function", error);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
socket.on("assertSessions", async (jids, force, callback) => {
|
|
61
|
+
try {
|
|
62
|
+
const response = await baileys_sock.assertSessions(jids, force);
|
|
63
|
+
|
|
64
|
+
callback(response);
|
|
65
|
+
|
|
66
|
+
logger?.debug("[*] Success on call assertSessions function", response);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
logger?.error("[*] Error on call assertSessions function", error);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
socket.on("createParticipantNodes", async (jids, message, extraAttrs, callback) => {
|
|
73
|
+
try {
|
|
74
|
+
const response = await baileys_sock.createParticipantNodes(jids, message, extraAttrs);
|
|
75
|
+
|
|
76
|
+
callback(response);
|
|
77
|
+
|
|
78
|
+
logger?.debug("[*] Success on call createParticipantNodes function", response);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
logger?.error("[*] Error on call createParticipantNodes function", error);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
socket.on("getUSyncDevices", async (jids, useCache, ignoreZeroDevices, callback) => {
|
|
85
|
+
try {
|
|
86
|
+
const response = await baileys_sock.getUSyncDevices(jids, useCache, ignoreZeroDevices);
|
|
87
|
+
|
|
88
|
+
callback(response);
|
|
89
|
+
|
|
90
|
+
logger?.debug("[*] Success on call getUSyncDevices function", response);
|
|
91
|
+
} catch (error) {
|
|
92
|
+
logger?.error("[*] Error on call getUSyncDevices function", error);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
socket.on("generateMessageTag", async (callback) => {
|
|
97
|
+
try {
|
|
98
|
+
const response = await baileys_sock.generateMessageTag();
|
|
99
|
+
|
|
100
|
+
callback(response);
|
|
101
|
+
|
|
102
|
+
logger?.debug("[*] Success on call generateMessageTag function", response);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
logger?.error("[*] Error on call generateMessageTag function", error);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
socket.on("sendNode", async (stanza, callback) => {
|
|
109
|
+
try {
|
|
110
|
+
const response = await baileys_sock.sendNode(stanza);
|
|
111
|
+
|
|
112
|
+
callback(true);
|
|
113
|
+
|
|
114
|
+
logger?.debug("[*] Success on call sendNode function", response);
|
|
115
|
+
} catch (error) {
|
|
116
|
+
logger?.error("[*] Error on call sendNode function", error);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
socket.on("signalRepository:decryptMessage", async (jid, type, ciphertext, callback) => {
|
|
121
|
+
try {
|
|
122
|
+
const response = await baileys_sock.signalRepository.decryptMessage({ jid: jid, type: type, ciphertext: ciphertext });
|
|
123
|
+
|
|
124
|
+
callback(response);
|
|
125
|
+
|
|
126
|
+
logger?.debug("[*] Success on call signalRepository:decryptMessage function", response);
|
|
127
|
+
} catch (error) {
|
|
128
|
+
logger?.error("[*] Error on call signalRepository:decryptMessage function", error);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// we only use this connection data to inform the webphone that the device is connected and creeds account to generate e2e whatsapp key for make call packets
|
|
133
|
+
baileys_sock.ev.on("connection.update", (update: Partial<ConnectionState>) => {
|
|
134
|
+
const { connection } = update;
|
|
135
|
+
|
|
136
|
+
if (connection) {
|
|
137
|
+
baileys_connection_state = connection;
|
|
138
|
+
socket.timeout(5000).emit("connection.update:status", baileys_sock.authState.creds.me, baileys_sock.authState.creds.account, connection);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (update.qr) {
|
|
142
|
+
socket.timeout(5000).emit("connection.update:qr", update.qr);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
baileys_sock.ws.on("CB:call", (packet) => {
|
|
147
|
+
logger?.debug("[*] Signling received")
|
|
148
|
+
socket.volatile.timeout(5000).emit("CB:call", packet);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
baileys_sock.ws.on("CB:ack,class:call", (packet) => {
|
|
152
|
+
logger?.debug("[*] Signling ack received")
|
|
153
|
+
socket.volatile.timeout(5000).emit("CB:ack,class:call", packet);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
return socket;
|
|
157
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { WAConnectionState, JidWithDevice, Contact, BinaryNode, proto } from "baileys";
|
|
2
|
+
|
|
3
|
+
export interface ServerToClientEvents {
|
|
4
|
+
withAck: (d: string, callback: (e: number) => void) => void;
|
|
5
|
+
onWhatsApp: onWhatsAppType;
|
|
6
|
+
profilePictureUrl: ProfilePictureUrlType;
|
|
7
|
+
assertSessions: AssertSessionsType;
|
|
8
|
+
createParticipantNodes: CreateParticipantNodesType;
|
|
9
|
+
getUSyncDevices: GetUSyncDevicesType;
|
|
10
|
+
generateMessageTag: GenerateMessageTagType;
|
|
11
|
+
sendNode: SendNodeType;
|
|
12
|
+
"signalRepository:decryptMessage": SignalRepositoryDecryptMessageType;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ClientToServerEvents {
|
|
16
|
+
"init": (me: Contact | undefined, account: proto.IADVSignedDeviceIdentity | undefined, status: WAConnectionState) => void;
|
|
17
|
+
"CB:call": (packet: any) => void;
|
|
18
|
+
"CB:ack,class:call": (packet: any) => void;
|
|
19
|
+
"connection.update:status": (me: Contact | undefined, account: proto.IADVSignedDeviceIdentity | undefined, status: WAConnectionState) => void;
|
|
20
|
+
"connection.update:qr": (qr: string) => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type onWhatsAppType = (jid: string, callback: onWhatsAppCallback) => void;
|
|
24
|
+
export type onWhatsAppCallback = (response: {
|
|
25
|
+
exists: boolean;
|
|
26
|
+
jid: string;
|
|
27
|
+
}[]) => void;
|
|
28
|
+
|
|
29
|
+
export type ProfilePictureUrlType = (jid: string, type: "image" | "preview", timeoutMs: number | undefined, callback: ProfilePictureUrlCallback) => void;
|
|
30
|
+
export type ProfilePictureUrlCallback = (response: string | undefined) => void;
|
|
31
|
+
|
|
32
|
+
export type AssertSessionsType = (jids: string[], force: boolean, callback: AssertSessionsCallback) => void;
|
|
33
|
+
export type AssertSessionsCallback = (response: boolean) => void;
|
|
34
|
+
|
|
35
|
+
export type CreateParticipantNodesType = (jids: string[], message: any, extraAttrs: any, callback: CreateParticipantNodesCallback) => void;
|
|
36
|
+
export type CreateParticipantNodesCallback = (nodes: any, shouldIncludeDeviceIdentity: boolean) => void;
|
|
37
|
+
|
|
38
|
+
export type GetUSyncDevicesType = (jids: string[], useCache: boolean, ignoreZeroDevices: boolean, callback: GetUSyncDevicesTypeCallback) => void
|
|
39
|
+
export type GetUSyncDevicesTypeCallback = (jids: JidWithDevice[]) => void;
|
|
40
|
+
|
|
41
|
+
export type GenerateMessageTagType = (callback: GenerateMessageTagTypeCallback) => void
|
|
42
|
+
export type GenerateMessageTagTypeCallback = (response: string) => void;
|
|
43
|
+
|
|
44
|
+
export type SendNodeType = (stanza: BinaryNode, callback: SendNodeTypeCallback) => void
|
|
45
|
+
export type SendNodeTypeCallback = (response: boolean) => void;
|
|
46
|
+
|
|
47
|
+
export type SignalRepositoryDecryptMessageType = (jid: string, type: "pkmsg" | "msg", ciphertext: Buffer, callback: SignalRepositoryDecryptMessageCallback) => void
|
|
48
|
+
export type SignalRepositoryDecryptMessageCallback = (response: any) => void;
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2018",
|
|
4
|
+
"module": "NodeNext",
|
|
5
|
+
"moduleResolution": "NodeNext",
|
|
6
|
+
"experimentalDecorators": true,
|
|
7
|
+
"allowJs": false,
|
|
8
|
+
"checkJs": false,
|
|
9
|
+
"outDir": "lib",
|
|
10
|
+
"strict": false,
|
|
11
|
+
"strictNullChecks": true,
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
"noImplicitThis": true,
|
|
14
|
+
"esModuleInterop": true,
|
|
15
|
+
"resolveJsonModule": true,
|
|
16
|
+
"forceConsistentCasingInFileNames": true,
|
|
17
|
+
"declaration": true,
|
|
18
|
+
"lib": [
|
|
19
|
+
"es2020",
|
|
20
|
+
"esnext.array",
|
|
21
|
+
"DOM"
|
|
22
|
+
]
|
|
23
|
+
},
|
|
24
|
+
"include": [
|
|
25
|
+
"./src/**/*.ts"
|
|
26
|
+
],
|
|
27
|
+
"exclude": [
|
|
28
|
+
"node_modules"
|
|
29
|
+
]
|
|
30
|
+
}
|