@awangxcode/qiro-baileys 7.0.0-rc25.6 → 7.0.0-rc26.3
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/LICENSE +15 -0
- package/README.md +1445 -1041
- package/WAProto/WAProto.proto +4 -0
- package/WAProto/index.d.ts +4067 -243
- package/WAProto/index.js +74957 -43330
- package/assets/branding/qiro-logo.svg +24 -0
- package/assets/branding/qiro-mark.png +0 -0
- package/assets/branding/qiro-mark.svg +34 -0
- package/assets/branding/qiro-social-preview.png +0 -0
- package/lib/Defaults/index.d.ts +15 -0
- package/lib/Defaults/index.d.ts.map +1 -1
- package/lib/Defaults/index.js +40 -7
- package/lib/Defaults/index.js.map +1 -1
- package/lib/Extensions/experimental.d.ts +0 -2
- package/lib/Extensions/experimental.d.ts.map +1 -1
- package/lib/Extensions/experimental.js +0 -2
- package/lib/Extensions/experimental.js.map +1 -1
- package/lib/MessageBuilder/index.d.ts.map +1 -1
- package/lib/MessageBuilder/index.js +10 -1
- package/lib/MessageBuilder/index.js.map +1 -1
- package/lib/Signal/lid-mapping.js +3 -3
- package/lib/Signal/lid-mapping.js.map +1 -1
- package/lib/Socket/business.d.ts +283 -1
- package/lib/Socket/business.d.ts.map +1 -1
- package/lib/Socket/chats.d.ts +114 -1
- package/lib/Socket/chats.d.ts.map +1 -1
- package/lib/Socket/chats.js +285 -10
- package/lib/Socket/chats.js.map +1 -1
- package/lib/Socket/communities.d.ts +283 -1
- package/lib/Socket/communities.d.ts.map +1 -1
- package/lib/Socket/groups.d.ts +106 -0
- package/lib/Socket/groups.d.ts.map +1 -1
- package/lib/Socket/index.d.ts +283 -1
- package/lib/Socket/index.d.ts.map +1 -1
- package/lib/Socket/index.js +4 -1
- package/lib/Socket/index.js.map +1 -1
- package/lib/Socket/messages-recv.d.ts +284 -1
- package/lib/Socket/messages-recv.d.ts.map +1 -1
- package/lib/Socket/messages-recv.js +73 -22
- package/lib/Socket/messages-recv.js.map +1 -1
- package/lib/Socket/messages-send.d.ts +297 -1
- package/lib/Socket/messages-send.d.ts.map +1 -1
- package/lib/Socket/messages-send.js +164 -28
- package/lib/Socket/messages-send.js.map +1 -1
- package/lib/Socket/newsletter.d.ts +322 -37
- package/lib/Socket/newsletter.d.ts.map +1 -1
- package/lib/Socket/newsletter.js +341 -1
- package/lib/Socket/newsletter.js.map +1 -1
- package/lib/Socket/socket.d.ts +13 -0
- package/lib/Socket/socket.d.ts.map +1 -1
- package/lib/Socket/socket.js +84 -31
- package/lib/Socket/socket.js.map +1 -1
- package/lib/Store/index.d.ts +4 -0
- package/lib/Store/index.js +4 -0
- package/lib/Store/make-in-memory-store.d.ts +63 -0
- package/lib/Store/make-in-memory-store.js +397 -0
- package/lib/Store/make-ordered-dictionary.d.ts +13 -0
- package/lib/Store/make-ordered-dictionary.js +79 -0
- package/lib/Store/object-repository.d.ts +11 -0
- package/lib/Store/object-repository.js +24 -0
- package/lib/Types/Chat.d.ts +13 -0
- package/lib/Types/Chat.d.ts.map +1 -1
- package/lib/Types/Chat.js +7 -0
- package/lib/Types/Chat.js.map +1 -1
- package/lib/Types/Message.d.ts +65 -1
- package/lib/Types/Message.d.ts.map +1 -1
- package/lib/Types/Message.js +11 -0
- package/lib/Types/Message.js.map +1 -1
- package/lib/Types/Mex.d.ts +68 -39
- package/lib/Types/Mex.d.ts.map +1 -1
- package/lib/Types/Mex.js +66 -0
- package/lib/Types/Mex.js.map +1 -1
- package/lib/Types/Socket.d.ts +45 -0
- package/lib/Types/Socket.d.ts.map +1 -1
- package/lib/Utils/audio-sticker.d.ts +14 -0
- package/lib/Utils/audio-sticker.js +172 -0
- package/lib/Utils/auth-store.d.ts +30 -0
- package/lib/Utils/auth-store.js +77 -0
- package/lib/Utils/chat-theme.d.ts +34 -0
- package/lib/Utils/chat-theme.js +336 -0
- package/lib/Utils/chat-utils.js +2 -2
- package/lib/Utils/chat-utils.js.map +1 -1
- package/lib/Utils/decode-wa-message.d.ts +14 -0
- package/lib/Utils/decode-wa-message.d.ts.map +1 -1
- package/lib/Utils/decode-wa-message.js +55 -0
- package/lib/Utils/decode-wa-message.js.map +1 -1
- package/lib/Utils/event-buffer.d.ts +2 -1
- package/lib/Utils/event-buffer.d.ts.map +1 -1
- package/lib/Utils/event-buffer.js +3 -4
- package/lib/Utils/event-buffer.js.map +1 -1
- package/lib/Utils/generics.d.ts +3 -2
- package/lib/Utils/generics.d.ts.map +1 -1
- package/lib/Utils/generics.js +10 -19
- package/lib/Utils/generics.js.map +1 -1
- package/lib/Utils/group-metadata-cache.d.ts +4 -0
- package/lib/Utils/group-metadata-cache.js +25 -0
- package/lib/Utils/history.js +1 -1
- package/lib/Utils/history.js.map +1 -1
- package/lib/Utils/html-app.d.ts +19 -0
- package/lib/Utils/html-app.js +96 -0
- package/lib/Utils/html-multiplayer.d.ts +10 -0
- package/lib/Utils/html-multiplayer.js +94 -0
- package/lib/Utils/index.d.ts +25 -0
- package/lib/Utils/index.d.ts.map +1 -1
- package/lib/Utils/index.js +27 -0
- package/lib/Utils/index.js.map +1 -1
- package/lib/Utils/link-preview-metadata.d.ts +47 -0
- package/lib/Utils/link-preview-metadata.js +88 -0
- package/lib/Utils/messages-media.d.ts +31 -2
- package/lib/Utils/messages-media.d.ts.map +1 -1
- package/lib/Utils/messages-media.js +66 -19
- package/lib/Utils/messages-media.js.map +1 -1
- package/lib/Utils/messages.d.ts +42 -2
- package/lib/Utils/messages.d.ts.map +1 -1
- package/lib/Utils/messages.js +117 -26
- package/lib/Utils/messages.js.map +1 -1
- package/lib/Utils/modern-messages.d.ts +150 -0
- package/lib/Utils/modern-messages.js +496 -0
- package/lib/Utils/native-flow.d.ts +11 -0
- package/lib/Utils/native-flow.js +116 -0
- package/lib/Utils/newsletter-status.d.ts +158 -0
- package/lib/Utils/newsletter-status.js +651 -0
- package/lib/Utils/optional-media.d.ts +3 -0
- package/lib/Utils/optional-media.js +46 -0
- package/lib/Utils/payment-messages.d.ts +38 -0
- package/lib/Utils/payment-messages.js +160 -0
- package/lib/Utils/process-message.d.ts +11 -0
- package/lib/Utils/process-message.d.ts.map +1 -1
- package/lib/Utils/process-message.js +80 -0
- package/lib/Utils/process-message.js.map +1 -1
- package/lib/Utils/scheduled-message.d.ts +51 -0
- package/lib/Utils/scheduled-message.js +113 -0
- package/lib/Utils/sender-key-memory.d.ts +19 -0
- package/lib/Utils/sender-key-memory.js +51 -0
- package/lib/Utils/status-stickers.d.ts +127 -0
- package/lib/Utils/status-stickers.js +261 -0
- package/lib/Utils/ttl-cache.d.ts +29 -0
- package/lib/Utils/ttl-cache.js +121 -0
- package/lib/Utils/use-mongo-auth-state.d.ts +22 -0
- package/lib/Utils/use-mongo-auth-state.js +68 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts +1 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts.map +1 -1
- package/lib/Utils/use-multi-file-auth-state.js +26 -13
- package/lib/Utils/use-multi-file-auth-state.js.map +1 -1
- package/lib/Utils/use-mysql-auth-state.d.ts +20 -0
- package/lib/Utils/use-mysql-auth-state.js +80 -0
- package/lib/Utils/use-nekodb-auth.d.ts +19 -0
- package/lib/Utils/use-nekodb-auth.js +111 -0
- package/lib/Utils/use-postgres-auth-state.d.ts +20 -0
- package/lib/Utils/use-postgres-auth-state.js +80 -0
- package/lib/Utils/use-redis-auth-state.d.ts +20 -0
- package/lib/Utils/use-redis-auth-state.js +90 -0
- package/lib/Utils/use-single-file-auth-state.d.ts +11 -0
- package/lib/Utils/use-single-file-auth-state.js +109 -0
- package/lib/Utils/use-sqlite-auth-state.d.ts +13 -0
- package/lib/Utils/use-sqlite-auth-state.js +121 -0
- package/lib/Utils/username.d.ts +12 -0
- package/lib/Utils/username.js +59 -0
- package/lib/Utils/voice-recognition.d.ts +53 -0
- package/lib/Utils/voice-recognition.js +219 -0
- package/lib/VoIP/audio-feeder.d.ts +24 -0
- package/lib/VoIP/audio-feeder.js +276 -0
- package/lib/VoIP/index.d.ts +93 -0
- package/lib/VoIP/index.js +998 -0
- package/lib/VoIP/relay-transport.d.ts +5 -0
- package/lib/VoIP/relay-transport.js +824 -0
- package/lib/VoIP/signaling.d.ts +11 -0
- package/lib/VoIP/signaling.js +1099 -0
- package/lib/VoIP/types.d.ts +2 -0
- package/lib/VoIP/types.js +12 -0
- package/lib/VoIP/video-feeder.d.ts +24 -0
- package/lib/VoIP/video-feeder.js +276 -0
- package/lib/VoIP/wasm-engine.d.ts +26 -0
- package/lib/VoIP/wasm-engine.js +1318 -0
- package/lib/VoIP/worker-bootstrap.js +1022 -0
- package/lib/WABinary/generic-utils.d.ts +2 -0
- package/lib/WABinary/generic-utils.d.ts.map +1 -1
- package/lib/WABinary/generic-utils.js +2 -0
- package/lib/WABinary/generic-utils.js.map +1 -1
- package/lib/WABinary/jid-utils.d.ts +2 -0
- package/lib/WABinary/jid-utils.d.ts.map +1 -1
- package/lib/WABinary/jid-utils.js +3 -1
- package/lib/WABinary/jid-utils.js.map +1 -1
- package/lib/extensions/awang-MessageBuilder.js +1 -1
- package/lib/index.d.ts +4 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +4 -0
- package/lib/index.js.map +1 -1
- package/package.json +78 -31
- package/MIGRASI-QIRO-BAILEYS-RC25.md +0 -102
- package/QIRO-BAILEYS-RC25.md +0 -168
- package/THIRD_PARTY_NOTICES.md +0 -49
- /package/{engine-requirements.js → config/engine-requirements.js} +0 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/* QIRO utility module. */
|
|
2
|
+
import { proto } from '../../WAProto/index.js';
|
|
3
|
+
import { initAuthCreds } from './auth-utils.js';
|
|
4
|
+
import { BufferJSON } from './generics.js';
|
|
5
|
+
async function loadBetterSqlite3() {
|
|
6
|
+
try {
|
|
7
|
+
|
|
8
|
+
const mod = (await import('better-sqlite3'));
|
|
9
|
+
return mod.default ?? mod;
|
|
10
|
+
}
|
|
11
|
+
catch (err) {
|
|
12
|
+
const helpful = new Error('`better-sqlite3` is required for `useSqliteAuthState`. Install it as a peer dependency: `npm install better-sqlite3` (or `yarn add better-sqlite3`).');
|
|
13
|
+
|
|
14
|
+
helpful.cause = err;
|
|
15
|
+
throw helpful;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
const CREDS_ROW_KEY = '__creds__';
|
|
19
|
+
const CREATE_SCHEMA_SQL = `
|
|
20
|
+
CREATE TABLE IF NOT EXISTS creds (
|
|
21
|
+
key TEXT PRIMARY KEY,
|
|
22
|
+
value TEXT NOT NULL
|
|
23
|
+
);
|
|
24
|
+
CREATE TABLE IF NOT EXISTS signal_keys (
|
|
25
|
+
type TEXT NOT NULL,
|
|
26
|
+
id TEXT NOT NULL,
|
|
27
|
+
value TEXT NOT NULL,
|
|
28
|
+
PRIMARY KEY (type, id)
|
|
29
|
+
);
|
|
30
|
+
CREATE INDEX IF NOT EXISTS signal_keys_type_idx ON signal_keys(type);
|
|
31
|
+
`;
|
|
32
|
+
export async function useSqliteAuthState(opts) {
|
|
33
|
+
let db;
|
|
34
|
+
let owned = false;
|
|
35
|
+
if (opts.database) {
|
|
36
|
+
db = opts.database;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
const Database = await loadBetterSqlite3();
|
|
40
|
+
db = new Database(opts.dbPath);
|
|
41
|
+
owned = true;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
db.pragma('journal_mode = WAL');
|
|
45
|
+
db.pragma('synchronous = NORMAL');
|
|
46
|
+
db.exec(CREATE_SCHEMA_SQL);
|
|
47
|
+
const stmts = {
|
|
48
|
+
credsSelect: db.prepare('SELECT value FROM creds WHERE key = ?'),
|
|
49
|
+
credsUpsert: db.prepare('INSERT INTO creds (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value'),
|
|
50
|
+
keySelect: db.prepare('SELECT value FROM signal_keys WHERE type = ? AND id = ?'),
|
|
51
|
+
keyUpsert: db.prepare('INSERT INTO signal_keys (type, id, value) VALUES (?, ?, ?) ON CONFLICT(type, id) DO UPDATE SET value = excluded.value'),
|
|
52
|
+
keyDelete: db.prepare('DELETE FROM signal_keys WHERE type = ? AND id = ?'),
|
|
53
|
+
keyListIds: db.prepare('SELECT id FROM signal_keys WHERE type = ?'),
|
|
54
|
+
keyList: db.prepare('SELECT id, value FROM signal_keys WHERE type = ?'),
|
|
55
|
+
clearKeys: db.prepare('DELETE FROM signal_keys'),
|
|
56
|
+
clearCreds: db.prepare('DELETE FROM creds')
|
|
57
|
+
};
|
|
58
|
+
const loadCreds = () => {
|
|
59
|
+
const row = stmts.credsSelect.get(CREDS_ROW_KEY);
|
|
60
|
+
if (!row)
|
|
61
|
+
return initAuthCreds();
|
|
62
|
+
return JSON.parse(row.value, BufferJSON.reviver);
|
|
63
|
+
};
|
|
64
|
+
const persistCreds = (creds) => {
|
|
65
|
+
stmts.credsUpsert.run(CREDS_ROW_KEY, JSON.stringify(creds, BufferJSON.replacer));
|
|
66
|
+
};
|
|
67
|
+
const creds = loadCreds();
|
|
68
|
+
return {
|
|
69
|
+
state: {
|
|
70
|
+
creds,
|
|
71
|
+
keys: {
|
|
72
|
+
get: async (type, ids) => {
|
|
73
|
+
const data = {};
|
|
74
|
+
for (const id of ids) {
|
|
75
|
+
const row = stmts.keySelect.get(type, id);
|
|
76
|
+
if (row) {
|
|
77
|
+
let value = JSON.parse(row.value, BufferJSON.reviver);
|
|
78
|
+
if (type === 'app-state-sync-key' && value) {
|
|
79
|
+
value = proto.Message.AppStateSyncKeyData.fromObject(value);
|
|
80
|
+
}
|
|
81
|
+
data[id] = value;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return data;
|
|
85
|
+
},
|
|
86
|
+
set: async (data) => {
|
|
87
|
+
const writeTx = db.transaction(() => {
|
|
88
|
+
for (const category in data) {
|
|
89
|
+
for (const id in data[category]) {
|
|
90
|
+
const value = data[category][id];
|
|
91
|
+
if (value) {
|
|
92
|
+
const stringified = JSON.stringify(value, BufferJSON.replacer);
|
|
93
|
+
stmts.keyUpsert.run(category, id, stringified);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
stmts.keyDelete.run(category, id);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
writeTx();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
saveCreds: async () => {
|
|
106
|
+
persistCreds(creds);
|
|
107
|
+
},
|
|
108
|
+
clearAuth: async () => {
|
|
109
|
+
const wipe = db.transaction(() => {
|
|
110
|
+
stmts.clearKeys.run();
|
|
111
|
+
stmts.clearCreds.run();
|
|
112
|
+
});
|
|
113
|
+
wipe();
|
|
114
|
+
},
|
|
115
|
+
close: async () => {
|
|
116
|
+
if (owned) {
|
|
117
|
+
db.close();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare const USERNAME_MIN_LENGTH = 3;
|
|
2
|
+
export declare const USERNAME_MAX_LENGTH = 35;
|
|
3
|
+
export declare const USERNAME_PIN_LENGTH = 4;
|
|
4
|
+
export type UsernameValidationErrorType = 'INVALID_CHARACTER' | 'INVALID_LENGTH' | 'INVALID_NO_LETTERS' | 'INVALID_PERIODS' | 'INVALID_DOMAIN_SUFFIX' | 'INVALID_WWW_PREFIX' | 'INVALID_WORD';
|
|
5
|
+
export declare const UsernameValidationError: Record<UsernameValidationErrorType, UsernameValidationErrorType>;
|
|
6
|
+
export declare const stripUsernamePrefix: (username: string) => string;
|
|
7
|
+
export declare const displayUsername: (username: string) => string;
|
|
8
|
+
export declare const validateUsername: (username: string) => {
|
|
9
|
+
isValid: boolean;
|
|
10
|
+
errorType?: UsernameValidationErrorType;
|
|
11
|
+
};
|
|
12
|
+
export declare const isUsernamePin: (pin: string) => boolean;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/* QIRO utility module. */
|
|
2
|
+
export const USERNAME_MIN_LENGTH = 3;
|
|
3
|
+
export const USERNAME_MAX_LENGTH = 35;
|
|
4
|
+
export const USERNAME_PIN_LENGTH = 4;
|
|
5
|
+
const LETTERS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
6
|
+
const DIGITS = '0123456789';
|
|
7
|
+
const LETTER_SET = new Set(LETTERS.split(''));
|
|
8
|
+
const ALLOWED_SET = new Set([...LETTERS.split(''), ...DIGITS.split(''), '_', '.']);
|
|
9
|
+
const RESERVED_SUFFIXES = ['.com', '.org', '.net', '.int', '.edu', '.gov', '.mil', '.arpa', '.html', '.htm', '.txt', '.xml'];
|
|
10
|
+
const RESERVED_WORDS = ['whatsapp', 'instagram', 'facebook', 'oculus'];
|
|
11
|
+
const PIN_PATTERN = /^[0-9]{4}$/;
|
|
12
|
+
export const UsernameValidationError = {
|
|
13
|
+
INVALID_CHARACTER: 'INVALID_CHARACTER',
|
|
14
|
+
INVALID_LENGTH: 'INVALID_LENGTH',
|
|
15
|
+
INVALID_NO_LETTERS: 'INVALID_NO_LETTERS',
|
|
16
|
+
INVALID_PERIODS: 'INVALID_PERIODS',
|
|
17
|
+
INVALID_DOMAIN_SUFFIX: 'INVALID_DOMAIN_SUFFIX',
|
|
18
|
+
INVALID_WWW_PREFIX: 'INVALID_WWW_PREFIX',
|
|
19
|
+
INVALID_WORD: 'INVALID_WORD'
|
|
20
|
+
};
|
|
21
|
+
export const stripUsernamePrefix = (username) => {
|
|
22
|
+
return typeof username === 'string' && username.startsWith('@') ? username.slice(1) : username;
|
|
23
|
+
};
|
|
24
|
+
export const displayUsername = (username) => {
|
|
25
|
+
return '@' + stripUsernamePrefix(username);
|
|
26
|
+
};
|
|
27
|
+
export const validateUsername = (username) => {
|
|
28
|
+
const value = stripUsernamePrefix(username);
|
|
29
|
+
if (typeof value !== 'string') {
|
|
30
|
+
return { isValid: false, errorType: UsernameValidationError.INVALID_CHARACTER };
|
|
31
|
+
}
|
|
32
|
+
const characters = Array.from(value);
|
|
33
|
+
if (!characters.every(character => ALLOWED_SET.has(character))) {
|
|
34
|
+
return { isValid: false, errorType: UsernameValidationError.INVALID_CHARACTER };
|
|
35
|
+
}
|
|
36
|
+
if (value.length < USERNAME_MIN_LENGTH || value.length > USERNAME_MAX_LENGTH) {
|
|
37
|
+
return { isValid: false, errorType: UsernameValidationError.INVALID_LENGTH };
|
|
38
|
+
}
|
|
39
|
+
if (!characters.some(character => LETTER_SET.has(character))) {
|
|
40
|
+
return { isValid: false, errorType: UsernameValidationError.INVALID_NO_LETTERS };
|
|
41
|
+
}
|
|
42
|
+
if (value.startsWith('.') || value.endsWith('.') || value.includes('..')) {
|
|
43
|
+
return { isValid: false, errorType: UsernameValidationError.INVALID_PERIODS };
|
|
44
|
+
}
|
|
45
|
+
const lowered = value.toLowerCase();
|
|
46
|
+
if (lowered.startsWith('www.')) {
|
|
47
|
+
return { isValid: false, errorType: UsernameValidationError.INVALID_WWW_PREFIX };
|
|
48
|
+
}
|
|
49
|
+
if (RESERVED_SUFFIXES.some(suffix => lowered.endsWith(suffix))) {
|
|
50
|
+
return { isValid: false, errorType: UsernameValidationError.INVALID_DOMAIN_SUFFIX };
|
|
51
|
+
}
|
|
52
|
+
if (RESERVED_WORDS.some(word => lowered.includes(word))) {
|
|
53
|
+
return { isValid: false, errorType: UsernameValidationError.INVALID_WORD };
|
|
54
|
+
}
|
|
55
|
+
return { isValid: true };
|
|
56
|
+
};
|
|
57
|
+
export const isUsernamePin = (pin) => {
|
|
58
|
+
return typeof pin === 'string' && PIN_PATTERN.test(pin);
|
|
59
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export type VoiceRecognitionContext = {
|
|
2
|
+
message: any;
|
|
3
|
+
audioMessage: any;
|
|
4
|
+
chatJid: string;
|
|
5
|
+
senderJid: string;
|
|
6
|
+
isGroup: boolean;
|
|
7
|
+
};
|
|
8
|
+
export type VoiceTranscriptionInput = VoiceRecognitionContext & {
|
|
9
|
+
buffer: Buffer;
|
|
10
|
+
mimetype: string;
|
|
11
|
+
};
|
|
12
|
+
export type VoiceTranscriptionResult = string | {
|
|
13
|
+
text?: string;
|
|
14
|
+
transcript?: string;
|
|
15
|
+
language?: string;
|
|
16
|
+
confidence?: number;
|
|
17
|
+
[key: string]: any;
|
|
18
|
+
};
|
|
19
|
+
export type VoiceRecognitionOptions = {
|
|
20
|
+
enabled?: boolean;
|
|
21
|
+
transcribe: (input: VoiceTranscriptionInput) => VoiceTranscriptionResult | Promise<VoiceTranscriptionResult>;
|
|
22
|
+
wakePhrases?: string | string[];
|
|
23
|
+
getWakePhrases?: (context: VoiceRecognitionContext) => string | string[] | undefined | Promise<string | string[] | undefined>;
|
|
24
|
+
shouldProcess?: (context: VoiceRecognitionContext) => boolean | Promise<boolean>;
|
|
25
|
+
allowQuotedActivation?: boolean;
|
|
26
|
+
pttOnly?: boolean;
|
|
27
|
+
maxDuration?: number;
|
|
28
|
+
maxBytes?: number;
|
|
29
|
+
downloadOptions?: any;
|
|
30
|
+
};
|
|
31
|
+
export type VoiceRecognitionEvent = VoiceRecognitionContext & {
|
|
32
|
+
transcript: string;
|
|
33
|
+
commandText: string;
|
|
34
|
+
activation: 'quoted' | 'wake-phrase' | null;
|
|
35
|
+
wakePhrase: string | null;
|
|
36
|
+
language?: string;
|
|
37
|
+
confidence?: number;
|
|
38
|
+
[key: string]: any;
|
|
39
|
+
};
|
|
40
|
+
export function normalizeVoiceText(value: any): string;
|
|
41
|
+
export function matchVoiceWakePhrase(transcript: any, phrases?: string | string[]): {
|
|
42
|
+
phrase: string;
|
|
43
|
+
normalizedPhrase: string;
|
|
44
|
+
normalizedTranscript: string;
|
|
45
|
+
} | null;
|
|
46
|
+
export function removeVoiceWakePhrase(transcript: any, wakePhrase: any): string;
|
|
47
|
+
export function bindVoiceRecognition(sock: any, config?: {
|
|
48
|
+
voiceRecognition?: VoiceRecognitionOptions;
|
|
49
|
+
getMessage?: (key: any) => Promise<any>;
|
|
50
|
+
logger?: any;
|
|
51
|
+
}, dependencies?: {
|
|
52
|
+
downloadContentFromMessage?: (...args: any[]) => Promise<any>;
|
|
53
|
+
}): () => void;
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { downloadContentFromMessage } from './messages-media.js';
|
|
2
|
+
import { normalizeMessageContent } from './messages.js';
|
|
3
|
+
import { areJidsSameUser, isJidGroup, jidNormalizedUser } from '../WABinary/index.js';
|
|
4
|
+
|
|
5
|
+
const DEFAULT_MAX_DURATION = 60;
|
|
6
|
+
const DEFAULT_MAX_BYTES = 16 * 1024 * 1024;
|
|
7
|
+
|
|
8
|
+
const streamToBuffer = async (stream, maxBytes) => {
|
|
9
|
+
const chunks = [];
|
|
10
|
+
let total = 0;
|
|
11
|
+
for await (const chunk of stream) {
|
|
12
|
+
const buffer = Buffer.from(chunk);
|
|
13
|
+
total += buffer.length;
|
|
14
|
+
if (maxBytes && total > maxBytes) {
|
|
15
|
+
throw new Error('voice message exceeds maxBytes');
|
|
16
|
+
}
|
|
17
|
+
chunks.push(buffer);
|
|
18
|
+
}
|
|
19
|
+
return Buffer.concat(chunks);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const normalizeVoiceText = value => String(value ?? '')
|
|
23
|
+
.normalize('NFKD')
|
|
24
|
+
.replace(/\p{M}/gu, '')
|
|
25
|
+
.toLowerCase()
|
|
26
|
+
.replace(/[^\p{L}\p{N}]+/gu, ' ')
|
|
27
|
+
.replace(/\s+/g, ' ')
|
|
28
|
+
.trim();
|
|
29
|
+
|
|
30
|
+
export const matchVoiceWakePhrase = (transcript, phrases = []) => {
|
|
31
|
+
const normalizedTranscript = normalizeVoiceText(transcript);
|
|
32
|
+
if (!normalizedTranscript) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
const candidates = [...new Map((Array.isArray(phrases) ? phrases : [phrases])
|
|
36
|
+
.map(phrase => ({ raw: String(phrase ?? '').trim(), normalized: normalizeVoiceText(phrase) }))
|
|
37
|
+
.filter(phrase => phrase.normalized)
|
|
38
|
+
.map(phrase => [phrase.normalized, phrase])).values()]
|
|
39
|
+
.sort((a, b) => b.normalized.length - a.normalized.length);
|
|
40
|
+
for (const phrase of candidates) {
|
|
41
|
+
const index = ` ${normalizedTranscript} `.indexOf(` ${phrase.normalized} `);
|
|
42
|
+
if (index >= 0) {
|
|
43
|
+
return {
|
|
44
|
+
phrase: phrase.raw,
|
|
45
|
+
normalizedPhrase: phrase.normalized,
|
|
46
|
+
normalizedTranscript
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return null;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export const removeVoiceWakePhrase = (transcript, wakePhrase) => {
|
|
54
|
+
const normalizedTranscript = normalizeVoiceText(transcript);
|
|
55
|
+
const normalizedPhrase = normalizeVoiceText(wakePhrase);
|
|
56
|
+
if (!normalizedPhrase) {
|
|
57
|
+
return normalizedTranscript;
|
|
58
|
+
}
|
|
59
|
+
const words = normalizedTranscript.split(' ');
|
|
60
|
+
const phraseWords = normalizedPhrase.split(' ');
|
|
61
|
+
for (let index = 0; index <= words.length - phraseWords.length; index += 1) {
|
|
62
|
+
if (phraseWords.every((word, offset) => words[index + offset] === word)) {
|
|
63
|
+
words.splice(index, phraseWords.length);
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return words.join(' ').trim();
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const getVoiceSenderJid = message => jidNormalizedUser(message?.key?.participantAlt ||
|
|
71
|
+
message?.key?.participant ||
|
|
72
|
+
message?.key?.remoteJidAlt ||
|
|
73
|
+
message?.key?.remoteJid);
|
|
74
|
+
|
|
75
|
+
const isQuotedToSelf = async ({ message, audioMessage, meId, meLid, getMessage }) => {
|
|
76
|
+
const contextInfo = audioMessage?.contextInfo;
|
|
77
|
+
if (!contextInfo?.quotedMessage && !contextInfo?.stanzaId) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
const quotedAuthor = contextInfo.participantAlt || contextInfo.participant;
|
|
81
|
+
if (quotedAuthor && (areJidsSameUser(quotedAuthor, meId) || areJidsSameUser(quotedAuthor, meLid))) {
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
if (!contextInfo.stanzaId || typeof getMessage !== 'function') {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
const target = await getMessage({
|
|
88
|
+
remoteJid: message.key.remoteJid,
|
|
89
|
+
remoteJidAlt: message.key.remoteJidAlt,
|
|
90
|
+
participant: quotedAuthor,
|
|
91
|
+
id: contextInfo.stanzaId
|
|
92
|
+
});
|
|
93
|
+
return target?.key?.fromMe === true;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const normalizeTranscriptionResult = value => {
|
|
97
|
+
if (typeof value === 'string') {
|
|
98
|
+
return { text: value };
|
|
99
|
+
}
|
|
100
|
+
if (value && typeof value === 'object') {
|
|
101
|
+
return {
|
|
102
|
+
...value,
|
|
103
|
+
text: String(value.text ?? value.transcript ?? '')
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
return { text: '' };
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const getWakePhrases = async (voiceRecognition, context) => {
|
|
110
|
+
const staticPhrases = Array.isArray(voiceRecognition.wakePhrases)
|
|
111
|
+
? voiceRecognition.wakePhrases
|
|
112
|
+
: voiceRecognition.wakePhrases
|
|
113
|
+
? [voiceRecognition.wakePhrases]
|
|
114
|
+
: [];
|
|
115
|
+
const dynamicPhrases = typeof voiceRecognition.getWakePhrases === 'function'
|
|
116
|
+
? await voiceRecognition.getWakePhrases(context)
|
|
117
|
+
: [];
|
|
118
|
+
return [...new Set([
|
|
119
|
+
...staticPhrases,
|
|
120
|
+
...(Array.isArray(dynamicPhrases) ? dynamicPhrases : dynamicPhrases ? [dynamicPhrases] : [])
|
|
121
|
+
].map(value => String(value ?? '').trim()).filter(Boolean))];
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export const bindVoiceRecognition = (sock, config = {}, dependencies = {}) => {
|
|
125
|
+
const voiceRecognition = config.voiceRecognition;
|
|
126
|
+
if (!voiceRecognition?.enabled || typeof voiceRecognition.transcribe !== 'function') {
|
|
127
|
+
return () => undefined;
|
|
128
|
+
}
|
|
129
|
+
const downloadContent = dependencies.downloadContentFromMessage || downloadContentFromMessage;
|
|
130
|
+
const getMessage = config.getMessage;
|
|
131
|
+
const logger = config.logger;
|
|
132
|
+
const handler = async ({ messages, type }) => {
|
|
133
|
+
if (type !== 'notify') {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
for (const message of messages || []) {
|
|
137
|
+
try {
|
|
138
|
+
if (
|
|
139
|
+
!message?.message ||
|
|
140
|
+
(message.key?.fromMe && !voiceRecognition.allowFromMe)
|
|
141
|
+
) {
|
|
142
|
+
continue
|
|
143
|
+
}
|
|
144
|
+
const content = normalizeMessageContent(message.message);
|
|
145
|
+
const audioMessage = content?.audioMessage;
|
|
146
|
+
if (!audioMessage) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if ((voiceRecognition.pttOnly ?? true) && !audioMessage.ptt) {
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
const maxDuration = Number.isFinite(voiceRecognition.maxDuration)
|
|
153
|
+
? Math.max(0, Number(voiceRecognition.maxDuration))
|
|
154
|
+
: DEFAULT_MAX_DURATION;
|
|
155
|
+
if (maxDuration && Number(audioMessage.seconds || 0) > maxDuration) {
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const chatJid = jidNormalizedUser(message.key.remoteJid);
|
|
159
|
+
const senderJid = getVoiceSenderJid(message);
|
|
160
|
+
const context = {
|
|
161
|
+
message,
|
|
162
|
+
audioMessage,
|
|
163
|
+
chatJid,
|
|
164
|
+
senderJid,
|
|
165
|
+
isGroup: isJidGroup(chatJid)
|
|
166
|
+
};
|
|
167
|
+
if (typeof voiceRecognition.shouldProcess === 'function' && !(await voiceRecognition.shouldProcess(context))) {
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
const maxBytes = Number.isFinite(voiceRecognition.maxBytes)
|
|
171
|
+
? Math.max(0, Number(voiceRecognition.maxBytes))
|
|
172
|
+
: DEFAULT_MAX_BYTES;
|
|
173
|
+
const declaredBytes = Number(audioMessage.fileLength?.toString?.() || audioMessage.fileLength || 0);
|
|
174
|
+
if (maxBytes && declaredBytes > maxBytes) {
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
const stream = await downloadContent(audioMessage, 'audio', voiceRecognition.downloadOptions || {});
|
|
178
|
+
const buffer = await streamToBuffer(stream, maxBytes);
|
|
179
|
+
const transcription = normalizeTranscriptionResult(await voiceRecognition.transcribe({
|
|
180
|
+
...context,
|
|
181
|
+
buffer,
|
|
182
|
+
mimetype: audioMessage.mimetype || 'audio/ogg; codecs=opus'
|
|
183
|
+
}));
|
|
184
|
+
const transcript = transcription.text.trim();
|
|
185
|
+
if (!transcript) {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
const meId = sock.authState?.creds?.me?.id;
|
|
189
|
+
const meLid = sock.authState?.creds?.me?.lid;
|
|
190
|
+
const quoted = (voiceRecognition.allowQuotedActivation ?? true)
|
|
191
|
+
? await isQuotedToSelf({ message, audioMessage, meId, meLid, getMessage })
|
|
192
|
+
: false;
|
|
193
|
+
const phrases = quoted ? [] : await getWakePhrases(voiceRecognition, context);
|
|
194
|
+
const wake = quoted ? null : matchVoiceWakePhrase(transcript, phrases);
|
|
195
|
+
const activation = quoted ? 'quoted' : wake ? 'wake-phrase' : null;
|
|
196
|
+
const commandText = wake
|
|
197
|
+
? removeVoiceWakePhrase(transcript, wake.normalizedPhrase)
|
|
198
|
+
: normalizeVoiceText(transcript);
|
|
199
|
+
const event = {
|
|
200
|
+
...context,
|
|
201
|
+
...transcription,
|
|
202
|
+
transcript,
|
|
203
|
+
commandText,
|
|
204
|
+
activation,
|
|
205
|
+
wakePhrase: wake?.phrase || null
|
|
206
|
+
};
|
|
207
|
+
sock.ev.emit('voice.transcription', event);
|
|
208
|
+
if (activation) {
|
|
209
|
+
sock.ev.emit('voice.command', event);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
logger?.warn({ err: error, key: message?.key }, 'voice recognition failed');
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
sock.ev.on('messages.upsert', handler);
|
|
218
|
+
return () => sock.ev.off('messages.upsert', handler);
|
|
219
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare class AudioFeeder {
|
|
2
|
+
constructor(sampleRate: number, channels: number, framesPerChunk: number, onChunk: (chunk: Float32Array) => void, source?: string, hooks?: {
|
|
3
|
+
onTrackStart?: (track: string) => void;
|
|
4
|
+
onTrackEnd?: (track: string) => void;
|
|
5
|
+
onIdle?: () => void;
|
|
6
|
+
ffmpegPath?: string;
|
|
7
|
+
});
|
|
8
|
+
readonly queuedChunks: number;
|
|
9
|
+
droppedChunks: number;
|
|
10
|
+
underflowChunks: number;
|
|
11
|
+
rebuffers: number;
|
|
12
|
+
bytesProduced: number;
|
|
13
|
+
chunksEmitted: number;
|
|
14
|
+
readonly playlistLength: number;
|
|
15
|
+
readonly currentTrack: string | null;
|
|
16
|
+
readonly idle: boolean;
|
|
17
|
+
tracksPlayed: number;
|
|
18
|
+
enqueue(source: string | string[]): number;
|
|
19
|
+
clearPlaylist(): void;
|
|
20
|
+
skip(): boolean;
|
|
21
|
+
start(playlist?: string[]): void;
|
|
22
|
+
stop(): void;
|
|
23
|
+
}
|
|
24
|
+
export default AudioFeeder;
|