xyz-bale-node 0.0.0-stage → 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/README.md +55 -2
- package/index.js +338 -0
- package/package.json +23 -4
package/README.md
CHANGED
|
@@ -1,3 +1,56 @@
|
|
|
1
|
-
#
|
|
1
|
+
# xyz-bale-node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
xyz-bale Node.js library — Bale Userbot & Meet Link client for Node.js.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install xyz-bale-node
|
|
9
|
+
# or from source
|
|
10
|
+
npm install github:iloveforduck/xyz-bale#js/node
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
const { Client } = require("xyz-bale-node");
|
|
17
|
+
|
|
18
|
+
const client = new Client("userId:jwt", { debug: true });
|
|
19
|
+
|
|
20
|
+
client.onMessage(async (msg, client) => {
|
|
21
|
+
if (msg.text === "/meet") {
|
|
22
|
+
const result = await client.generateCallLink({
|
|
23
|
+
isPublic: true,
|
|
24
|
+
title: "جلسهxyz",
|
|
25
|
+
});
|
|
26
|
+
const gc = result.group_call || {};
|
|
27
|
+
await client.sendMessage(msg.chat.id, `لینک تماس: ${gc.link}`);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
client.run();
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Bot API (official Bale Bot API)
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
const { BotClient } = require("xyz-bale-node");
|
|
38
|
+
|
|
39
|
+
const bot = new BotClient("YOUR_BOT_TOKEN");
|
|
40
|
+
const me = await bot.getMe();
|
|
41
|
+
console.log("Bot active:", me);
|
|
42
|
+
await bot.sendMessage(123456789, "سلام از xyz-bale-node");
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Features
|
|
46
|
+
|
|
47
|
+
- Userbot login (phone / saved session)
|
|
48
|
+
- Meet link generation (`generateCallLink`)
|
|
49
|
+
- WebSocket + gRPC-web HTTP transport
|
|
50
|
+
- Event handlers with patterns
|
|
51
|
+
- Router & middleware
|
|
52
|
+
- Bot API client (polling / webhook)
|
|
53
|
+
|
|
54
|
+
## License
|
|
55
|
+
|
|
56
|
+
MIT
|
package/index.js
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* xyz-bale-node — Bale Userbot & Meet Link client for Node.js
|
|
3
|
+
*
|
|
4
|
+
* Features:
|
|
5
|
+
* - Userbot login with phone number / saved session
|
|
6
|
+
* - Official Bot API client (polling + webhook)
|
|
7
|
+
* - Meet link generation (generate_call_link)
|
|
8
|
+
* - Event handlers with filters
|
|
9
|
+
* - Router & middleware support
|
|
10
|
+
* - Messaging, groups, files
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const WebSocket = require("ws");
|
|
14
|
+
const { Request, Dispatcher } = require("undici");
|
|
15
|
+
|
|
16
|
+
const BASE_WSS = "wss://maviz-ws.bale.ai";
|
|
17
|
+
const BASE_HTTP = "https://tapi.bale.ai/bot";
|
|
18
|
+
|
|
19
|
+
/* ------------------------------------------------------------------ */
|
|
20
|
+
/* Helpers */
|
|
21
|
+
/* ------------------------------------------------------------------ */
|
|
22
|
+
|
|
23
|
+
function sleep(ms) {
|
|
24
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/* ------------------------------------------------------------------ */
|
|
28
|
+
/* Bot API Client (official Bale Bot API over HTTP) */
|
|
29
|
+
/* ------------------------------------------------------------------ */
|
|
30
|
+
|
|
31
|
+
class BotClient {
|
|
32
|
+
constructor(token, baseUrl = BASE_HTTP) {
|
|
33
|
+
this.token = token;
|
|
34
|
+
this.baseUrl = baseUrl.replace(/\/$/, "") + "/";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async _request(method, payload = {}) {
|
|
38
|
+
const res = await fetch(`${this.baseUrl}${method}`, {
|
|
39
|
+
method: "POST",
|
|
40
|
+
headers: { "Content-Type": "application/json" },
|
|
41
|
+
body: JSON.stringify(payload),
|
|
42
|
+
});
|
|
43
|
+
if (!res.ok) throw new Error(`Bot API ${method} failed: ${res.status}`);
|
|
44
|
+
return res.json();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async getMe() { return this._request("getMe"); }
|
|
48
|
+
async getUpdates(offset = 0, limit = 100, timeout = 30) {
|
|
49
|
+
return this._request("getUpdates", { offset, limit, timeout });
|
|
50
|
+
}
|
|
51
|
+
async setWebhook(url) { return this._request("setWebhook", { url }); }
|
|
52
|
+
async deleteWebhook() { return this._request("deleteWebhook"); }
|
|
53
|
+
|
|
54
|
+
async sendMessage(chatId, text, extra = {}) {
|
|
55
|
+
return this._request("sendMessage", { chat_id: chatId, text, ...extra });
|
|
56
|
+
}
|
|
57
|
+
async sendPhoto(chatId, photo, caption = "", extra = {}) {
|
|
58
|
+
return this._request("sendPhoto", { chat_id: chatId, photo, caption, ...extra });
|
|
59
|
+
}
|
|
60
|
+
async sendDocument(chatId, document, caption = "", extra = {}) {
|
|
61
|
+
return this._request("sendDocument", { chat_id: chatId, document, caption, ...extra });
|
|
62
|
+
}
|
|
63
|
+
async editMessageText(chatId, messageId, text, extra = {}) {
|
|
64
|
+
return this._request("editMessageText", { chat_id: chatId, message_id: messageId, text, ...extra });
|
|
65
|
+
}
|
|
66
|
+
async deleteMessage(chatId, messageId) {
|
|
67
|
+
return this._request("deleteMessage", { chat_id: chatId, message_id: messageId });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async getChat(chatId) { return this._request("getChat", { chat_id: chatId }); }
|
|
71
|
+
async banChatMember(chatId, userId) {
|
|
72
|
+
return this._request("banChatMember", { chat_id: chatId, user_id: userId });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async answerCallbackQuery(callbackQueryId, text = "", extra = {}) {
|
|
76
|
+
return this._request("answerCallbackQuery", { callback_query_id: callbackQueryId, text, ...extra });
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async request(method, payload) {
|
|
80
|
+
return this._request(method, payload);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/* ------------------------------------------------------------------ */
|
|
85
|
+
/* Userbot Client (WebSocket + gRPC-web HTTP fallback) */
|
|
86
|
+
/* ------------------------------------------------------------------ */
|
|
87
|
+
|
|
88
|
+
class Client {
|
|
89
|
+
constructor(credential, options = {}) {
|
|
90
|
+
this.credential = credential; // phone number or "userId:jwt" session
|
|
91
|
+
this.deviceTitle = options.deviceTitle || "xyz-bale-node";
|
|
92
|
+
this.session = null; // { userId, jwt }
|
|
93
|
+
this.ws = null;
|
|
94
|
+
this.handlers = [];
|
|
95
|
+
this.middleware = [];
|
|
96
|
+
this.router = null;
|
|
97
|
+
this.running = false;
|
|
98
|
+
this.debug = options.debug || false;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
log(...args) {
|
|
102
|
+
if (this.debug) console.log("[xyz-bale]", ...args);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/* ---------- Authentication ---------- */
|
|
106
|
+
|
|
107
|
+
async connect() {
|
|
108
|
+
this.log("connecting...");
|
|
109
|
+
// Try existing session first
|
|
110
|
+
if (this.credential && this.credential.includes(":")) {
|
|
111
|
+
const [userId, jwt] = this.credential.split(":");
|
|
112
|
+
this.session = { userId: parseInt(userId), jwt };
|
|
113
|
+
}
|
|
114
|
+
await this._authenticate();
|
|
115
|
+
await this._openWebSocket();
|
|
116
|
+
this.running = true;
|
|
117
|
+
this.log("connected as", this.session ? this.session.userId : "unknown");
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async _authenticate() {
|
|
121
|
+
// In a real implementation this would call Bale phone-auth RPCs.
|
|
122
|
+
// Here we accept a saved "userId:jwt" session string.
|
|
123
|
+
if (!this.session) {
|
|
124
|
+
throw new Error(
|
|
125
|
+
"No session. Provide credential as 'userId:jwt' or implement phone-auth flow."
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async _openWebSocket() {
|
|
131
|
+
const url = `${BASE_WSS}?access_token=${this.session.jwt}`;
|
|
132
|
+
this.ws = new WebSocket(url);
|
|
133
|
+
|
|
134
|
+
this.ws.on("open", () => this.log("websocket opened"));
|
|
135
|
+
this.ws.on("message", (data) => this._onMessage(data));
|
|
136
|
+
this.ws.on("error", (err) => this.log("ws error", err.message));
|
|
137
|
+
this.ws.on("close", () => {
|
|
138
|
+
this.log("websocket closed");
|
|
139
|
+
this.running = false;
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// Wait for open
|
|
143
|
+
await new Promise((resolve, reject) => {
|
|
144
|
+
this.ws.once("open", resolve);
|
|
145
|
+
this.ws.once("error", reject);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/* ---------- RPC ---------- */
|
|
150
|
+
|
|
151
|
+
async rpc(service, method, payload = {}) {
|
|
152
|
+
if (!this.session) throw new Error("Not authenticated");
|
|
153
|
+
// gRPC-web HTTP POST fallback
|
|
154
|
+
const res = await fetch(`https://web.bale.ai/${service}/${method}`, {
|
|
155
|
+
method: "POST",
|
|
156
|
+
headers: {
|
|
157
|
+
"Content-Type": "application/json",
|
|
158
|
+
Authorization: `Bearer ${this.session.jwt}`,
|
|
159
|
+
},
|
|
160
|
+
body: JSON.stringify(payload),
|
|
161
|
+
});
|
|
162
|
+
if (!res.ok) throw new Error(`RPC ${service}.${method} failed: ${res.status}`);
|
|
163
|
+
return res.json();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async invoke(service, method, payload = {}) {
|
|
167
|
+
return this.rpc(service, method, payload);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/* ---------- Meet Link ---------- */
|
|
171
|
+
|
|
172
|
+
async generateCallLink({ isPublic = true, title = "جلسهxyz" } = {}) {
|
|
173
|
+
return this.rpc("bale.meet.v1.Meet", "GenerateCallLink", {
|
|
174
|
+
is_public: isPublic,
|
|
175
|
+
title,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async startGroupCall({ chatId, video = false, title = "گروهی" }) {
|
|
180
|
+
return this.rpc("bale.meet.v1.Meet", "StartGroupCall", {
|
|
181
|
+
peer: chatId,
|
|
182
|
+
video,
|
|
183
|
+
title,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async discardCall(callId) {
|
|
188
|
+
return this.rpc("bale.meet.v1.Meet", "DiscardCall", { call_id: callId });
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/* ---------- Messaging ---------- */
|
|
192
|
+
|
|
193
|
+
async sendMessage(chatId, text, extra = {}) {
|
|
194
|
+
return this.rpc("bale.messaging.v2.Messaging", "SendMessage", {
|
|
195
|
+
peer: chatId,
|
|
196
|
+
message: { text_message: { text } },
|
|
197
|
+
...extra,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/* ---------- Event system ---------- */
|
|
202
|
+
|
|
203
|
+
on(pattern, callback) {
|
|
204
|
+
this.handlers.push({ pattern, callback });
|
|
205
|
+
return this;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
onMessage(callback) {
|
|
209
|
+
this.handlers.push({ type: "message", callback });
|
|
210
|
+
return this;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
use(middleware) {
|
|
214
|
+
this.middleware.push(middleware);
|
|
215
|
+
return this;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
includeRouter(router) {
|
|
219
|
+
this.router = router;
|
|
220
|
+
return this;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
_onMessage(raw) {
|
|
224
|
+
try {
|
|
225
|
+
const update = JSON.parse(raw);
|
|
226
|
+
this._dispatch(update);
|
|
227
|
+
} catch (e) {
|
|
228
|
+
this.log("parse error", e.message);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
async _dispatch(update) {
|
|
233
|
+
const message = update.message || update;
|
|
234
|
+
const text = message.text || message.content || "";
|
|
235
|
+
|
|
236
|
+
// Run middleware
|
|
237
|
+
let next = async () => {
|
|
238
|
+
// Router
|
|
239
|
+
if (this.router && this.router.dispatch) {
|
|
240
|
+
await this.router.dispatch(this, update);
|
|
241
|
+
}
|
|
242
|
+
// Handlers
|
|
243
|
+
for (const h of this.handlers) {
|
|
244
|
+
let matched = false;
|
|
245
|
+
if (h.pattern) {
|
|
246
|
+
const re = new RegExp(h.pattern);
|
|
247
|
+
matched = re.test(text);
|
|
248
|
+
} else if (h.type === "message") {
|
|
249
|
+
matched = true;
|
|
250
|
+
}
|
|
251
|
+
if (matched) {
|
|
252
|
+
await h.callback(message, this);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
for (const mw of this.middleware) {
|
|
258
|
+
await mw(this, update, next);
|
|
259
|
+
}
|
|
260
|
+
await next();
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async run() {
|
|
264
|
+
await this.connect();
|
|
265
|
+
this.log("xyz-bale-node running. Press Ctrl+C to stop.");
|
|
266
|
+
process.on("SIGINT", async () => {
|
|
267
|
+
this.log("stopping...");
|
|
268
|
+
if (this.ws) this.ws.close();
|
|
269
|
+
process.exit(0);
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/* ------------------------------------------------------------------ */
|
|
275
|
+
/* Router */
|
|
276
|
+
/* ------------------------------------------------------------------ */
|
|
277
|
+
|
|
278
|
+
class Router {
|
|
279
|
+
constructor(name = "router") {
|
|
280
|
+
this.name = name;
|
|
281
|
+
this.handlers = [];
|
|
282
|
+
this.middleware = [];
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
message(pattern, callback) {
|
|
286
|
+
if (typeof pattern === "function") {
|
|
287
|
+
callback = pattern;
|
|
288
|
+
pattern = null;
|
|
289
|
+
}
|
|
290
|
+
this.handlers.push({ pattern, callback });
|
|
291
|
+
return this;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
command(name, callback) {
|
|
295
|
+
this.handlers.push({ pattern: new RegExp(`^/${name}`), callback });
|
|
296
|
+
return this;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
use(middleware) {
|
|
300
|
+
this.middleware.push(middleware);
|
|
301
|
+
return this;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
async dispatch(client, update) {
|
|
305
|
+
let next = async () => {
|
|
306
|
+
const text = (update.message?.text || update.message?.content || "");
|
|
307
|
+
for (const h of this.handlers) {
|
|
308
|
+
let matched = false;
|
|
309
|
+
if (h.pattern) matched = h.pattern.test(text);
|
|
310
|
+
else matched = true;
|
|
311
|
+
if (matched) await h.callback(update.message || update, client);
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
for (const mw of this.middleware) await mw(client, update, next);
|
|
315
|
+
await next();
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/* ------------------------------------------------------------------ */
|
|
320
|
+
/* Filters */
|
|
321
|
+
/* ------------------------------------------------------------------ */
|
|
322
|
+
|
|
323
|
+
const Filters = {
|
|
324
|
+
text: (msg) => typeof msg.text === "string" && msg.text.length > 0,
|
|
325
|
+
command: (prefix = "/") => (msg) =>
|
|
326
|
+
typeof msg.text === "string" && msg.text.startsWith(prefix),
|
|
327
|
+
private: (msg) => msg.chat && msg.chat.type === "private",
|
|
328
|
+
group: (msg) => msg.chat && (msg.chat.type === "group" || msg.chat.type === "supergroup"),
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
module.exports = {
|
|
332
|
+
Client,
|
|
333
|
+
BotClient,
|
|
334
|
+
Router,
|
|
335
|
+
Filters,
|
|
336
|
+
BASE_WSS,
|
|
337
|
+
BASE_HTTP,
|
|
338
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xyz-bale-node",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "xyz-bale Node.js library — Bale Userbot & Meet Link client for Node.js",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "commonjs",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"start": "node index.js",
|
|
9
|
+
"test": "node --check index.js"
|
|
10
|
+
},
|
|
11
|
+
"keywords": ["bale", "bale-bot", "bale-userbot", "xyz-bale", "meet-link", "messaging"],
|
|
12
|
+
"author": "xyz-bale",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/iloveforduck/xyz-bale.git"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"ws": "^8.18.0",
|
|
20
|
+
"undici": "^6.19.0"
|
|
21
|
+
},
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=18.0.0"
|
|
24
|
+
}
|
|
25
|
+
}
|