rubjs 3.2.2 → 3.2.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.
@@ -35,7 +35,8 @@ export default class Methods {
35
35
  start(this: Bot, ...args: Parameters<typeof Utilities.start>): Promise<any>;
36
36
  run(this: Bot, ...args: Parameters<typeof Utilities.run>): Promise<any>;
37
37
  getUpdates(this: Bot, ...args: Parameters<typeof Utilities.getUpdates>): Promise<any>;
38
- setupWebhook(this: Bot, ...args: Parameters<typeof Utilities.setupWebhook>): Promise<any>;
38
+ __setupWebhook(this: Bot, ...args: Parameters<typeof Utilities.setupWebhook>): Promise<any>;
39
+ __polling(this: Bot, ...args: Parameters<typeof Utilities.polling>): Promise<any>;
39
40
  setCommands(this: Bot, ...args: Parameters<typeof Settings.setCommands>): Promise<any>;
40
41
  updateBotEndpoints(this: Bot, ...args: Parameters<typeof Settings.updateBotEndpoints>): Promise<any>;
41
42
  }
@@ -123,9 +123,12 @@ class Methods {
123
123
  async getUpdates(...args) {
124
124
  return Utilities.getUpdates.apply(this, args);
125
125
  }
126
- async setupWebhook(...args) {
126
+ async __setupWebhook(...args) {
127
127
  return Utilities.setupWebhook.apply(this, args);
128
128
  }
129
+ async __polling(...args) {
130
+ return Utilities.polling.apply(this, args);
131
+ }
129
132
  // settings
130
133
  async setCommands(...args) {
131
134
  return Settings.setCommands.apply(this, args);
@@ -1,5 +1,6 @@
1
1
  import run from './run';
2
2
  import start from './start';
3
+ import polling from './polling';
3
4
  import getUpdates from './getUpdates';
4
5
  import setupWebhook from './webhook';
5
- export { run, start, getUpdates, setupWebhook };
6
+ export { run, start, getUpdates, setupWebhook, polling };
@@ -3,11 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.setupWebhook = exports.getUpdates = exports.start = exports.run = void 0;
6
+ exports.polling = exports.setupWebhook = exports.getUpdates = exports.start = exports.run = void 0;
7
7
  const run_1 = __importDefault(require("./run"));
8
8
  exports.run = run_1.default;
9
9
  const start_1 = __importDefault(require("./start"));
10
10
  exports.start = start_1.default;
11
+ const polling_1 = __importDefault(require("./polling"));
12
+ exports.polling = polling_1.default;
11
13
  const getUpdates_1 = __importDefault(require("./getUpdates"));
12
14
  exports.getUpdates = getUpdates_1.default;
13
15
  const webhook_1 = __importDefault(require("./webhook"));
@@ -0,0 +1,2 @@
1
+ import Bot from '../../bot';
2
+ export default function polling(this: Bot): Promise<void>;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.default = polling;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const update_context_1 = __importDefault(require("../../contexts/update.context"));
9
+ const models_1 = require("../../types/models");
10
+ const checkFilters_1 = require("../../../../utils/checkFilters");
11
+ const checkTypes = [models_1.UpdateTypeEnum.UpdatedMessage, models_1.UpdateTypeEnum.NewMessage];
12
+ async function polling() {
13
+ console.log('start robot with polling mode...');
14
+ let next_offset_id = loadOffset();
15
+ setInterval(async () => {
16
+ try {
17
+ const res = await this.getUpdates(next_offset_id);
18
+ for (let m of res.updates) {
19
+ if (isNaN(m.new_message?.time || m.updated_message?.time))
20
+ continue;
21
+ const messageTime = Number(m.new_message?.time || m.updated_message?.time) | 0;
22
+ const nowTime = Math.floor(Date.now() / 1000);
23
+ if (nowTime - messageTime < 5) {
24
+ for (let { prefix, filters, handler } of this.handlers.update) {
25
+ const ctx = new update_context_1.default(this, m);
26
+ const passed = await (0, checkFilters_1.checkFilters)(ctx, filters);
27
+ if (passed) {
28
+ if (prefix) {
29
+ if (!checkTypes.includes(ctx.type))
30
+ continue;
31
+ const text = ctx.updated_message?.text || ctx.new_message?.text || '';
32
+ if (typeof prefix === 'string' && text !== prefix)
33
+ continue;
34
+ if (prefix instanceof RegExp && !prefix.test(text))
35
+ continue;
36
+ }
37
+ await handler(ctx);
38
+ }
39
+ }
40
+ }
41
+ }
42
+ if (res.next_offset_id) {
43
+ next_offset_id = res.next_offset_id;
44
+ saveOffset(next_offset_id);
45
+ }
46
+ }
47
+ catch (e) {
48
+ console.error('Error occurred while polling:', e);
49
+ }
50
+ }, 1000);
51
+ }
52
+ function saveOffset(offset) {
53
+ fs_1.default.writeFileSync('offset.json', JSON.stringify({ offset }));
54
+ }
55
+ function loadOffset() {
56
+ if (!fs_1.default.existsSync('offset.json'))
57
+ return undefined;
58
+ const data = fs_1.default.readFileSync('offset.json', 'utf8');
59
+ return JSON.parse(data).offset;
60
+ }
@@ -8,6 +8,9 @@ async function run(url, host, port = 3000, updates = [
8
8
  while (!this.initialize) {
9
9
  await this.network.delay(2000);
10
10
  }
11
- await this.setupWebhook(url, host, port, updates);
11
+ if (url)
12
+ await this.__setupWebhook(url, host, port, updates);
13
+ else
14
+ await this.__polling();
12
15
  }
13
16
  exports.default = run;
@@ -1,4 +1,4 @@
1
1
  import Bot from '../../bot';
2
2
  import { UpdateEndpointTypeEnum } from '../../types/models';
3
- declare function setupWebhook(this: Bot, url?: string, host?: string, port?: number, updates?: UpdateEndpointTypeEnum[]): Promise<void>;
3
+ declare function setupWebhook(this: Bot, url: string, host?: string, port?: number, updates?: UpdateEndpointTypeEnum[]): Promise<void>;
4
4
  export default setupWebhook;
@@ -5,7 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const json_bigint_1 = __importDefault(require("json-bigint"));
7
7
  const handleUpdates_1 = __importDefault(require("./handleUpdates"));
8
- const localtunnel_1 = __importDefault(require("localtunnel"));
9
8
  const JSONbig = (0, json_bigint_1.default)({ storeAsString: true });
10
9
  function handleParser(_, body, done) {
11
10
  try {
@@ -16,31 +15,6 @@ function handleParser(_, body, done) {
16
15
  done(err, undefined);
17
16
  }
18
17
  }
19
- async function manageTunnelWithWebhook(port, updates, url) {
20
- const setEndpoints = async (url) => {
21
- for (let update of updates) {
22
- const res = await this.updateBotEndpoints(url, update);
23
- if (res.status != 'Done')
24
- console.error(`[ manageTunnelWithWebhook ] status updateBotEndpoints is ${res.status} for update: ${update}`);
25
- }
26
- console.log('start robot...');
27
- };
28
- if (url) {
29
- return await setEndpoints(url);
30
- }
31
- while (true) {
32
- try {
33
- const tunnel = await (0, localtunnel_1.default)({ port });
34
- await setEndpoints(tunnel.url);
35
- await new Promise((resolve) => tunnel.on('close', resolve));
36
- console.warn('🚪 Tunnel closed. Reconnecting...');
37
- }
38
- catch (err) {
39
- console.error('❌ Tunnel failed:', err);
40
- await new Promise((r) => setTimeout(r, 3000));
41
- }
42
- }
43
- }
44
18
  function lowerFirstChar(str) {
45
19
  return str.charAt(0).toLowerCase() + str.slice(1);
46
20
  }
@@ -52,6 +26,11 @@ async function setupWebhook(url, host = '0.0.0.0', port = 3000, updates = []) {
52
26
  this.server.post(`/${lowerFirstChar(update)}`, handleUpdates_1.default.bind(this));
53
27
  }
54
28
  await this.server.listen({ port, host });
55
- await manageTunnelWithWebhook.bind(this)(port, updates, url);
29
+ for (let update of updates) {
30
+ const res = await this.updateBotEndpoints(url, update);
31
+ if (res.status != 'Done')
32
+ console.error(`[ setupWebhook ] status updateBotEndpoints is ${res.status} for update: ${update}`);
33
+ }
34
+ console.log('start robot...');
56
35
  }
57
36
  exports.default = setupWebhook;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rubjs",
3
- "version": "3.2.2",
3
+ "version": "3.2.3",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "keywords": [
@@ -34,6 +34,7 @@
34
34
  "fastify": "^5.4.0",
35
35
  "json-bigint": "^1.0.0",
36
36
  "localtunnel": "npm:@security-patched/localtunnel@^2.0.2-secpatched.5",
37
+ "ngrok": "^5.0.0-beta.2",
37
38
  "node-rsa": "^1.1.1",
38
39
  "optional-require": "^2.0.1",
39
40
  "undici": "^7.10.0",
@@ -1,17 +0,0 @@
1
- import { Bot } from '../../..';
2
- import { PaymentStatus, Update as UpdateMessage, UpdateTypeEnum, Message as MessageUpdate, Keypad, ChatKeypadTypeEnum, InlineKeypad } from '../types/models';
3
- declare class Update implements UpdateMessage {
4
- type: UpdateTypeEnum;
5
- chat_id: string;
6
- removed_message_id?: string;
7
- new_message?: MessageUpdate;
8
- updated_message?: MessageUpdate;
9
- updated_payment?: PaymentStatus;
10
- store: Record<string, any>;
11
- bot: Bot;
12
- constructor(bot: Bot, update: Update);
13
- reply(text: string, chat_keypad?: Keypad, inline_keypad?: InlineKeypad, disable_notification?: boolean, reply_to_message_id?: string, chat_keypad_type?: ChatKeypadTypeEnum): Promise<import("../types/models").SendMessage>;
14
- forward(to_chat_id: string, disable_notification?: boolean): Promise<import("../types/models").SendMessage | undefined>;
15
- delete(): Promise<import("../types/models").SendMessage | undefined>;
16
- }
17
- export default Update;
@@ -1,39 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- class Update {
4
- constructor(bot, update) {
5
- this.store = {};
6
- Object.defineProperty(this, 'bot', {
7
- value: bot,
8
- enumerable: false,
9
- writable: true,
10
- configurable: true,
11
- });
12
- this.type = update.type;
13
- this.chat_id = update.chat_id;
14
- if (update?.new_message)
15
- this.new_message = update?.new_message;
16
- if (update?.updated_message)
17
- this.updated_message = update?.updated_message;
18
- if (update?.updated_payment)
19
- this.updated_payment = update?.updated_payment;
20
- if (update.removed_message_id)
21
- this.removed_message_id = update.removed_message_id;
22
- }
23
- async reply(text, chat_keypad, inline_keypad, disable_notification = false, reply_to_message_id, chat_keypad_type) {
24
- return await this.bot.sendMessage(this.chat_id, text, chat_keypad, inline_keypad, disable_notification, reply_to_message_id, chat_keypad_type);
25
- }
26
- async forward(to_chat_id, disable_notification = false) {
27
- const message_id = this.new_message?.message_id || this.updated_message?.message_id;
28
- if (!message_id)
29
- return;
30
- return await this.bot.forwardMessage(this.chat_id, message_id, to_chat_id, disable_notification);
31
- }
32
- async delete() {
33
- const message_id = this.new_message?.message_id || this.updated_message?.message_id;
34
- if (!message_id)
35
- return;
36
- return await this.bot.deleteMessage(this.chat_id, message_id);
37
- }
38
- }
39
- exports.default = Update;
File without changes
@@ -1,44 +0,0 @@
1
- "use strict";
2
- // import fs from 'fs';
3
- // import Network from '.';
4
- // import Message from '../contexts/message.context';
5
- // export default async function polling(network: Network) {
6
- // console.log('start robot with polling mode...');
7
- // let next_offset_id: string | undefined = loadOffset();
8
- // setInterval(async () => {
9
- // try {
10
- // const res = await network.bot.getUpdates(next_offset_id);
11
- // for (let m of res.updates) {
12
- // if (isNaN(m.new_message?.time || m.updated_message?.time)) continue;
13
- // const messageTime =
14
- // Number(m.new_message?.time || m.updated_message?.time) | 0;
15
- // const nowTime = Math.floor(Date.now() / 1000);
16
- // if (nowTime - messageTime < 5) {
17
- // m = new Message(network.bot, m);
18
- // for (let handler of network.bot.handlers) {
19
- // if (
20
- // handler.filters.length === 0 ||
21
- // handler.filters.every((filter) => filter(m))
22
- // ) {
23
- // await handler.handler(m);
24
- // }
25
- // }
26
- // }
27
- // }
28
- // if (res.next_offset_id) {
29
- // next_offset_id = res.next_offset_id;
30
- // saveOffset(next_offset_id as string);
31
- // }
32
- // } catch (e) {
33
- // console.error('Error occurred while polling:', e);
34
- // }
35
- // }, 1000);
36
- // }
37
- // function saveOffset(offset: string) {
38
- // fs.writeFileSync('offset.json', JSON.stringify({ offset }));
39
- // }
40
- // function loadOffset(): string | undefined {
41
- // if (!fs.existsSync('offset.json')) return undefined;
42
- // const data = fs.readFileSync('offset.json', 'utf8');
43
- // return JSON.parse(data).offset;
44
- // }