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