webmaxsocket 1.0.0 → 1.1.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/README.md +239 -13
- package/config/example.json +6 -0
- package/example-ios.js +186 -0
- package/example-sms.js +131 -0
- package/example-token.js +100 -0
- package/example.js +2 -0
- package/index.js +2 -0
- package/lib/client.js +345 -44
- package/lib/opcodes.js +3 -1
- package/lib/socketTransport.js +296 -0
- package/lib/userAgent.js +8 -4
- package/package.json +16 -3
package/example-token.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Пример входа по токену (token auth)
|
|
3
|
+
*
|
|
4
|
+
* Конфиг: config/default.json — token, agent
|
|
5
|
+
* node example-token.js
|
|
6
|
+
* node example-token.js other — config/other.json
|
|
7
|
+
* Вариант 3 — TOKEN в env: TOKEN="..." node example-token.js
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const { WebMaxClient } = require('./index');
|
|
11
|
+
|
|
12
|
+
const CONFIG_NAME = process.argv[2] || process.env.CONFIG || 'default';
|
|
13
|
+
|
|
14
|
+
async function main() {
|
|
15
|
+
let client;
|
|
16
|
+
try {
|
|
17
|
+
client = new WebMaxClient({
|
|
18
|
+
name: 'token_session',
|
|
19
|
+
configPath: CONFIG_NAME,
|
|
20
|
+
token: process.env.TOKEN,
|
|
21
|
+
saveToken: true,
|
|
22
|
+
debug: process.env.DEBUG === '1'
|
|
23
|
+
});
|
|
24
|
+
} catch (e) {
|
|
25
|
+
console.error('❌ Не удалось загрузить конфиг:', e.message);
|
|
26
|
+
console.error(' Создайте config/default.json (см. config/example.json)');
|
|
27
|
+
process.exit(1);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (!client._providedToken || client._providedToken.length < 50) {
|
|
31
|
+
console.error('❌ Укажите токен в config/default.json (поле "token")');
|
|
32
|
+
console.error(' Или: TOKEN="ваш_токен" node example-token.js');
|
|
33
|
+
process.exit(1);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
client.onStart(async () => {
|
|
37
|
+
if (client.me) {
|
|
38
|
+
console.log('\n📋 ДАННЫЕ ПОЛЬЗОВАТЕЛЯ:');
|
|
39
|
+
console.log('─'.repeat(40));
|
|
40
|
+
console.log(`👤 Имя: ${client.me.fullname || client.me.firstname}`);
|
|
41
|
+
console.log(`🆔 ID: ${client.me.id}`);
|
|
42
|
+
console.log(`📱 Телефон: +${client.me.phone || '—'}`);
|
|
43
|
+
console.log(`🖼 Avatar: ${client.me.avatar ? 'есть' : 'нет'}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
const chats = await client.getChats();
|
|
48
|
+
console.log('\n📂 ДИАЛОГИ (' + chats.length + '):');
|
|
49
|
+
console.log('─'.repeat(40));
|
|
50
|
+
chats.slice(0, 15).forEach((chat, i) => {
|
|
51
|
+
const chatId = chat.id ?? chat.chatId;
|
|
52
|
+
const title = chat.title || chat.name || `Chat ${chatId}`;
|
|
53
|
+
const lastMsg = chat.lastMessage?.text || chat.lastMessage?.message || '—';
|
|
54
|
+
const preview = String(lastMsg).slice(0, 40) + (String(lastMsg).length > 40 ? '…' : '');
|
|
55
|
+
console.log(`${i + 1}. [${chatId}] ${title}`);
|
|
56
|
+
console.log(` Последнее: ${preview}`);
|
|
57
|
+
});
|
|
58
|
+
if (chats.length > 15) console.log(` ... и ещё ${chats.length - 15}`);
|
|
59
|
+
|
|
60
|
+
if (chats.length > 0) {
|
|
61
|
+
const firstChat = chats[0];
|
|
62
|
+
const firstChatId = firstChat.id ?? firstChat.chatId;
|
|
63
|
+
const history = await client.getHistory(firstChatId, Date.now(), 5, 0);
|
|
64
|
+
console.log(`\n💬 Последние 5 сообщений в «${firstChat.title || firstChat.id}»:`);
|
|
65
|
+
console.log('─'.repeat(40));
|
|
66
|
+
history.reverse().forEach((m, i) => {
|
|
67
|
+
const from = m.senderId === client.me?.id ? 'Вы' : `User ${m.senderId}`;
|
|
68
|
+
console.log(` ${i + 1}. ${from}: ${String(m.text || '').slice(0, 50)}`);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
console.log('\n');
|
|
72
|
+
} catch (e) {
|
|
73
|
+
console.log('⚠️ Не удалось загрузить диалоги:', e.message);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
client.onMessage(async (message) => {
|
|
78
|
+
if (message.senderId === client.me?.id) return;
|
|
79
|
+
console.log(`💬 ${message.getSenderName()}: ${message.text}`);
|
|
80
|
+
await message.reply({ text: 'Получено!', cid: Date.now() });
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
client.onError(async (err) => console.error('❌', err.message));
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
await client.start();
|
|
87
|
+
console.log('🤖 Токен-авторизация успешна. Бот работает.\n');
|
|
88
|
+
} catch (error) {
|
|
89
|
+
console.error('❌ Ошибка:', error.message);
|
|
90
|
+
process.exit(1);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
process.on('SIGINT', () => {
|
|
95
|
+
console.log('\n\n👋 Завершение работы...');
|
|
96
|
+
console.log('\n💝 Нравится библиотека? Поддержите разработку:');
|
|
97
|
+
console.log(' USDT (TRC20): TXfs1iVbp2aLd3rbc4cenVzMoTevP5RbBE');
|
|
98
|
+
process.exit(0);
|
|
99
|
+
});
|
|
100
|
+
main().catch(console.error);
|
package/example.js
CHANGED
|
@@ -113,6 +113,8 @@ async function main() {
|
|
|
113
113
|
// Обработка завершения
|
|
114
114
|
process.on('SIGINT', async () => {
|
|
115
115
|
console.log('\n\n👋 Завершение работы...');
|
|
116
|
+
console.log('\n💝 Нравится библиотека? Поддержите разработку:');
|
|
117
|
+
console.log(' USDT (TRC20): TXfs1iVbp2aLd3rbc4cenVzMoTevP5RbBE');
|
|
116
118
|
process.exit(0);
|
|
117
119
|
});
|
|
118
120
|
|
package/index.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
const WebMaxClient = require('./lib/client');
|
|
9
|
+
const { MaxSocketTransport } = require('./lib/socketTransport');
|
|
9
10
|
const { User, Message, ChatAction } = require('./lib/entities');
|
|
10
11
|
const { ChatActions, EventTypes, MessageTypes } = require('./lib/constants');
|
|
11
12
|
const { Opcode, getOpcodeName } = require('./lib/opcodes');
|
|
@@ -13,6 +14,7 @@ const { UserAgentPayload } = require('./lib/userAgent');
|
|
|
13
14
|
|
|
14
15
|
module.exports = {
|
|
15
16
|
WebMaxClient,
|
|
17
|
+
MaxSocketTransport,
|
|
16
18
|
User,
|
|
17
19
|
Message,
|
|
18
20
|
ChatAction,
|