rubika 1.2.1 → 1.2.4

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.
Files changed (77) hide show
  1. package/bot/bot.ts +34 -28
  2. package/bot/contexts/inline.ts +15 -14
  3. package/bot/contexts/update.ts +20 -21
  4. package/bot/filters.ts +32 -30
  5. package/bot/index.ts +4 -2
  6. package/bot/methods/advanced/builder.ts +8 -13
  7. package/bot/methods/files/_sendFile.ts +5 -2
  8. package/bot/methods/files/uploadFile.ts +5 -2
  9. package/bot/methods/index.ts +10 -10
  10. package/bot/methods/messages/deleteMessage.ts +6 -10
  11. package/bot/methods/messages/sendContact.ts +5 -3
  12. package/bot/methods/messages/sendFile.ts +1 -1
  13. package/bot/methods/messages/sendGif.ts +23 -23
  14. package/bot/methods/messages/sendImage.ts +23 -23
  15. package/bot/methods/messages/sendLocation.ts +6 -3
  16. package/bot/methods/messages/sendMessage.ts +6 -2
  17. package/bot/methods/messages/sendMusic.ts +23 -23
  18. package/bot/methods/messages/sendPoll.ts +6 -3
  19. package/bot/methods/messages/sendSticker.ts +8 -4
  20. package/bot/methods/messages/sendVideo.ts +23 -23
  21. package/bot/methods/messages/sendVoice.ts +23 -23
  22. package/bot/methods/utilities/getUpdates.ts +1 -1
  23. package/bot/methods/utilities/handleUpdates.ts +14 -14
  24. package/bot/methods/utilities/polling.ts +57 -32
  25. package/bot/methods/utilities/run.ts +2 -2
  26. package/bot/methods/utilities/start.ts +5 -2
  27. package/bot/methods/utilities/webhook.ts +4 -3
  28. package/bot/network.ts +43 -15
  29. package/bot/types/handlers.ts +3 -3
  30. package/bot/types/interfaces.ts +44 -4
  31. package/bot/types/utils.ts +6 -1
  32. package/bot/utils/antiSpam.ts +1 -1
  33. package/client/client.ts +35 -33
  34. package/client/contexts/activities.type.ts +2 -2
  35. package/client/contexts/chat.type.ts +2 -2
  36. package/client/contexts/message.type.ts +10 -3
  37. package/client/contexts/notifications.type.ts +2 -2
  38. package/client/crypto.ts +2 -2
  39. package/client/filters.ts +31 -31
  40. package/client/index.ts +5 -1
  41. package/client/methods/advanced/builder.ts +3 -1
  42. package/client/methods/auth/registerDevice.ts +8 -6
  43. package/client/methods/channels/addChannel.ts +1 -1
  44. package/client/methods/channels/editChannelInfo.ts +3 -3
  45. package/client/methods/channels/setChannelVoiceChatSetting.ts +1 -1
  46. package/client/methods/chats/editJoinLink.ts +1 -1
  47. package/client/methods/chats/seenChats.ts +1 -1
  48. package/client/methods/extras/deleteMessagebyCount.ts +40 -38
  49. package/client/methods/extras/onEditMessages.ts +1 -1
  50. package/client/methods/extras/userIsAdmin.ts +2 -2
  51. package/client/methods/groups/editGroupInfo.ts +13 -1
  52. package/client/methods/groups/setGroupVoiceChatSetting.ts +1 -1
  53. package/client/methods/messages/sendDocument.ts +3 -2
  54. package/client/methods/messages/sendFileInline.ts +1 -1
  55. package/client/methods/messages/sendGif.ts +4 -3
  56. package/client/methods/messages/sendMessage.ts +8 -6
  57. package/client/methods/messages/sendMessageAPICall.ts +18 -0
  58. package/client/methods/messages/sendMusic.ts +3 -2
  59. package/client/methods/messages/sendPhoto.ts +3 -2
  60. package/client/methods/messages/sendText.ts +4 -2
  61. package/client/methods/messages/sendVideo.ts +3 -2
  62. package/client/methods/messages/sendVideoMessage.ts +3 -2
  63. package/client/methods/messages/sendVoice.ts +3 -2
  64. package/client/methods/settings/updateProfile.ts +1 -1
  65. package/client/methods/utilities/start.ts +7 -4
  66. package/client/network/api.ts +1 -1
  67. package/client/network/file.ts +13 -7
  68. package/client/network/websocket.ts +32 -15
  69. package/client/types/client.type.ts +10 -10
  70. package/client/utils/session.ts +1 -1
  71. package/index.ts +1 -0
  72. package/package.json +1 -7
  73. package/utils/formater.ts +1 -1
  74. package/utils/index.ts +2 -1
  75. package/utils/logger.ts +28 -0
  76. package/utils/parser.ts +120 -156
  77. package/utils/errors.ts +0 -26
package/bot/bot.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import Network from "./network";
2
2
  import Methods from "./methods";
3
+ import { Logger } from "../utils";
4
+ import Update from "./contexts/update";
3
5
  import type { BotInfo } from "./types/interfaces";
4
6
  import type { ContextMap, Handler, NestedFilter } from "./types/handlers";
5
- import Update from "./contexts/update";
6
- import { Logger } from "../utils";
7
+ import { BotConfig } from "./types/utils";
7
8
 
8
9
  class Bot extends Methods {
9
10
  protected initialize: boolean = false;
@@ -12,7 +13,7 @@ class Bot extends Methods {
12
13
  public bot?: BotInfo;
13
14
 
14
15
  public handlers: {
15
- [K in keyof ContextMap]: Handler<ContextMap[K]>[];
16
+ [K in keyof ContextMap<unknown>]: Handler<ContextMap<unknown>[K]>[];
16
17
  } = { inline: [], update: [], error: [] };
17
18
 
18
19
  public logger = new Logger<Bot>(this.handlers.error, this);
@@ -33,11 +34,16 @@ class Bot extends Methods {
33
34
  */
34
35
  constructor(
35
36
  public token: string,
36
- timeout: number = 10000,
37
+ config: BotConfig = { retryCount: 3, timeout: 10000 },
37
38
  ) {
38
39
  super();
39
40
  this.BASE_URL = `https://botapi.rubika.ir/v3/${token}`;
40
- this.network = new Network(this.BASE_URL, timeout);
41
+ this.network = new Network(
42
+ this.BASE_URL,
43
+ this.logger,
44
+ config.timeout,
45
+ config.retryCount,
46
+ );
41
47
 
42
48
  this.start();
43
49
  }
@@ -66,9 +72,9 @@ class Bot extends Methods {
66
72
  * });
67
73
  * ```
68
74
  */
69
- on<T extends keyof typeof this.handlers>(
70
- type: T,
71
- handler: (ctx: ContextMap[T]) => Promise<void>,
75
+ on<T, K extends keyof ContextMap<T>>(
76
+ type: K,
77
+ handler: (ctx: ContextMap<T>[K]) => Promise<void>,
72
78
  ): void;
73
79
 
74
80
  /**
@@ -96,18 +102,18 @@ class Bot extends Methods {
96
102
  * });
97
103
  * ```
98
104
  */
99
- on<T extends keyof typeof this.handlers>(
100
- type: T,
101
- filters: NestedFilter<ContextMap[T]>,
102
- handler: (ctx: ContextMap[T]) => Promise<void>,
105
+ on<T, K extends keyof ContextMap<T>>(
106
+ type: K,
107
+ filters: NestedFilter<ContextMap<T>[K]>,
108
+ handler: (ctx: ContextMap<T>[K]) => Promise<void>,
103
109
  ): void;
104
110
 
105
- on<T extends keyof typeof this.handlers>(
106
- type: T,
107
- filtersOrHandler:
108
- | NestedFilter<ContextMap[T]>
109
- | ((ctx: ContextMap[T]) => Promise<void>),
110
- maybeHandler?: (ctx: ContextMap[T]) => Promise<void>,
111
+ on<T, K extends keyof ContextMap<T>>(
112
+ type: K,
113
+ filtersOrHandler?:
114
+ | NestedFilter<ContextMap<T>[K]>
115
+ | ((ctx: ContextMap<T>[K]) => Promise<void>),
116
+ maybeHandler?: (ctx: ContextMap<T>[K]) => Promise<void>,
111
117
  ): void {
112
118
  if (typeof filtersOrHandler === "function") {
113
119
  this.handlers[type].push({
@@ -147,9 +153,9 @@ class Bot extends Methods {
147
153
  * });
148
154
  * ```
149
155
  */
150
- command(
156
+ command<T>(
151
157
  prefix: string | RegExp,
152
- handler: (ctx: Update) => Promise<void>,
158
+ handler: (ctx: Update<T>) => Promise<void>,
153
159
  ): void;
154
160
 
155
161
  /**
@@ -178,18 +184,18 @@ class Bot extends Methods {
178
184
  * });
179
185
  * ```
180
186
  */
181
- command(
187
+ command<T>(
182
188
  prefix: string | RegExp,
183
- filters: NestedFilter<ContextMap["update"]>,
184
- handler: (ctx: Update) => Promise<void>,
189
+ filters: NestedFilter<ContextMap<T>["update"]>,
190
+ handler: (ctx: Update<T>) => Promise<void>,
185
191
  ): void;
186
192
 
187
- command(
193
+ command<T>(
188
194
  prefix: string | RegExp,
189
195
  filtersOrHandler:
190
- | NestedFilter<ContextMap["update"]>
191
- | ((ctx: Update) => Promise<void>),
192
- maybeHandler?: (ctx: Update) => Promise<void>,
196
+ | NestedFilter<ContextMap<T>["update"]>
197
+ | ((ctx: Update<T>) => Promise<void>),
198
+ maybeHandler?: (ctx: Update<T>) => Promise<void>,
193
199
  ) {
194
200
  if (typeof filtersOrHandler === "function") {
195
201
  this.handlers.update.push({
@@ -204,7 +210,7 @@ class Bot extends Methods {
204
210
  prefix,
205
211
  });
206
212
  } else {
207
- throw this.logger.error("Invalid arguments for command()", "warn");
213
+ this.logger.error("Invalid arguments for command()", "warn");
208
214
  }
209
215
  }
210
216
  }
@@ -4,13 +4,14 @@ import {
4
4
  AuxData,
5
5
  File,
6
6
  InlineKeypad,
7
+ InlineMessage,
7
8
  Keypad,
8
9
  Location,
9
10
  } from "../types/interfaces";
10
11
  import { ChatKeypadTypeEnum } from "../types/enums";
11
12
  import { FileSource } from "../types/methods";
12
13
 
13
- class Inline {
14
+ class Inline<T> {
14
15
  sender_id: string;
15
16
  text: string;
16
17
  file?: File;
@@ -18,12 +19,12 @@ class Inline {
18
19
  aux_data?: AuxData;
19
20
  message_id: string;
20
21
  chat_id: string;
21
- store: Record<string, any> = {};
22
+ store: Partial<T> = {};
22
23
 
23
24
  declare bot: Bot;
24
25
 
25
26
  constructor(
26
- private ctx: any,
27
+ private ctx: InlineMessage,
27
28
  bot: Bot,
28
29
  ) {
29
30
  this.sender_id = ctx.sender_id;
@@ -41,7 +42,7 @@ class Inline {
41
42
  chat_keypad?: Keypad,
42
43
  inline_keypad?: InlineKeypad,
43
44
  disable_notification?: boolean,
44
- auto_delete: number | boolean = false,
45
+ auto_delete: number | false = false,
45
46
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
46
47
  ) => {
47
48
  return await this.bot.sendMessage(
@@ -62,7 +63,7 @@ class Inline {
62
63
  chat_keypad?: Keypad,
63
64
  inline_keypad?: InlineKeypad,
64
65
  disable_notification?: boolean,
65
- auto_delete: number | boolean = false,
66
+ auto_delete: number | false = false,
66
67
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
67
68
  ) => {
68
69
  return await this.bot.sendImage(
@@ -84,7 +85,7 @@ class Inline {
84
85
  chat_keypad?: Keypad,
85
86
  inline_keypad?: InlineKeypad,
86
87
  disable_notification?: boolean,
87
- auto_delete: number | boolean = false,
88
+ auto_delete: number | false = false,
88
89
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
89
90
  ) => {
90
91
  return await this.bot.sendVideo(
@@ -106,7 +107,7 @@ class Inline {
106
107
  chat_keypad?: Keypad,
107
108
  inline_keypad?: InlineKeypad,
108
109
  disable_notification?: boolean,
109
- auto_delete: number | boolean = false,
110
+ auto_delete: number | false = false,
110
111
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
111
112
  ) => {
112
113
  return await this.bot.sendGif(
@@ -127,7 +128,7 @@ class Inline {
127
128
  chat_keypad?: Keypad,
128
129
  inline_keypad?: InlineKeypad,
129
130
  disable_notification?: boolean,
130
- auto_delete: number | boolean = false,
131
+ auto_delete: number | false = false,
131
132
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
132
133
  ) => {
133
134
  return await this.bot.sendSticker(
@@ -148,7 +149,7 @@ class Inline {
148
149
  chat_keypad?: Keypad,
149
150
  inline_keypad?: InlineKeypad,
150
151
  disable_notification?: boolean,
151
- auto_delete: number | boolean = false,
152
+ auto_delete: number | false = false,
152
153
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
153
154
  ) => {
154
155
  return await this.bot.sendMusic(
@@ -170,7 +171,7 @@ class Inline {
170
171
  chat_keypad?: Keypad,
171
172
  inline_keypad?: InlineKeypad,
172
173
  disable_notification?: boolean,
173
- auto_delete: number | boolean = false,
174
+ auto_delete: number | false = false,
174
175
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
175
176
  ) => {
176
177
  return await this.bot.sendVoice(
@@ -192,7 +193,7 @@ class Inline {
192
193
  chat_keypad?: Keypad,
193
194
  inline_keypad?: InlineKeypad,
194
195
  disable_notification?: boolean,
195
- auto_delete: number | boolean = false,
196
+ auto_delete: number | false = false,
196
197
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
197
198
  ) => {
198
199
  return await this.bot.sendFile(
@@ -214,7 +215,7 @@ class Inline {
214
215
  chat_keypad?: Keypad,
215
216
  inline_keypad?: InlineKeypad,
216
217
  disable_notification?: boolean,
217
- auto_delete: number | boolean = false,
218
+ auto_delete: number | false = false,
218
219
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
219
220
  ) => {
220
221
  return await this.bot.sendLocation(
@@ -237,7 +238,7 @@ class Inline {
237
238
  chat_keypad?: Keypad,
238
239
  inline_keypad?: InlineKeypad,
239
240
  disable_notification?: boolean,
240
- auto_delete: number | boolean = false,
241
+ auto_delete: number | false = false,
241
242
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
242
243
  ) => {
243
244
  return await this.bot.sendContact(
@@ -257,7 +258,7 @@ class Inline {
257
258
  replyPoll = async (
258
259
  question: string,
259
260
  options: string[],
260
- auto_delete: number | boolean = false,
261
+ auto_delete: number | false = false,
261
262
  ) => {
262
263
  return await this.bot.sendPoll(
263
264
  this.chat_id,
@@ -7,20 +7,22 @@ import {
7
7
  Keypad,
8
8
  Message,
9
9
  PaymentStatus,
10
+ UpdateMessage,
10
11
  } from "../types/interfaces";
11
12
 
12
- class Update {
13
+ class Update<T> {
13
14
  type: UpdateTypeEnum;
14
15
  chat_id: string;
16
+ update_time: number;
15
17
  removed_message_id?: string;
16
18
  new_message?: Message;
17
19
  updated_message?: Message;
18
20
  updated_payment?: PaymentStatus;
19
- store: Record<string, any> = {};
21
+ store: Partial<T> = {};
20
22
  declare bot: Bot;
21
23
 
22
24
  constructor(
23
- private ctx: any,
25
+ private ctx: UpdateMessage,
24
26
  bot: Bot,
25
27
  ) {
26
28
  this.type = ctx.type;
@@ -29,6 +31,7 @@ class Update {
29
31
  this.new_message = ctx.new_message;
30
32
  this.updated_message = ctx.updated_message;
31
33
  this.updated_payment = ctx.updated_payment;
34
+ this.update_time = ctx.update_time;
32
35
  this.bot = bot;
33
36
  }
34
37
 
@@ -37,7 +40,7 @@ class Update {
37
40
  chat_keypad?: Keypad,
38
41
  inline_keypad?: InlineKeypad,
39
42
  disable_notification?: boolean,
40
- auto_delete: number | boolean = false,
43
+ auto_delete: number | false = false,
41
44
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
42
45
  ) => {
43
46
  const replyId = this.ensureReply();
@@ -60,7 +63,7 @@ class Update {
60
63
  chat_keypad?: Keypad,
61
64
  inline_keypad?: InlineKeypad,
62
65
  disable_notification?: boolean,
63
- auto_delete: number | boolean = false,
66
+ auto_delete: number | false = false,
64
67
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
65
68
  ) => {
66
69
  const replyId = this.ensureReply();
@@ -84,7 +87,7 @@ class Update {
84
87
  chat_keypad?: Keypad,
85
88
  inline_keypad?: InlineKeypad,
86
89
  disable_notification?: boolean,
87
- auto_delete: number | boolean = false,
90
+ auto_delete: number | false = false,
88
91
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
89
92
  ) => {
90
93
  const replyId = this.ensureReply();
@@ -108,7 +111,7 @@ class Update {
108
111
  chat_keypad?: Keypad,
109
112
  inline_keypad?: InlineKeypad,
110
113
  disable_notification?: boolean,
111
- auto_delete: number | boolean = false,
114
+ auto_delete: number | false = false,
112
115
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
113
116
  ) => {
114
117
  const replyId = this.ensureReply();
@@ -131,7 +134,7 @@ class Update {
131
134
  chat_keypad?: Keypad,
132
135
  inline_keypad?: InlineKeypad,
133
136
  disable_notification?: boolean,
134
- auto_delete: number | boolean = false,
137
+ auto_delete: number | false = false,
135
138
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
136
139
  ) => {
137
140
  const replyId = this.ensureReply();
@@ -154,7 +157,7 @@ class Update {
154
157
  chat_keypad?: Keypad,
155
158
  inline_keypad?: InlineKeypad,
156
159
  disable_notification?: boolean,
157
- auto_delete: number | boolean = false,
160
+ auto_delete: number | false = false,
158
161
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
159
162
  ) => {
160
163
  const replyId = this.ensureReply();
@@ -178,7 +181,7 @@ class Update {
178
181
  chat_keypad?: Keypad,
179
182
  inline_keypad?: InlineKeypad,
180
183
  disable_notification?: boolean,
181
- auto_delete: number | boolean = false,
184
+ auto_delete: number | false = false,
182
185
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
183
186
  ) => {
184
187
  const replyId = this.ensureReply();
@@ -202,7 +205,7 @@ class Update {
202
205
  chat_keypad?: Keypad,
203
206
  inline_keypad?: InlineKeypad,
204
207
  disable_notification?: boolean,
205
- auto_delete: number | boolean = false,
208
+ auto_delete: number | false = false,
206
209
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
207
210
  ) => {
208
211
  const replyId = this.ensureReply();
@@ -226,7 +229,7 @@ class Update {
226
229
  chat_keypad?: Keypad,
227
230
  inline_keypad?: InlineKeypad,
228
231
  disable_notification?: boolean,
229
- auto_delete: number | boolean = false,
232
+ auto_delete: number | false = false,
230
233
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
231
234
  ) => {
232
235
  const replyId = this.ensureReply();
@@ -251,7 +254,7 @@ class Update {
251
254
  chat_keypad?: Keypad,
252
255
  inline_keypad?: InlineKeypad,
253
256
  disable_notification?: boolean,
254
- auto_delete: number | boolean = false,
257
+ auto_delete: number | false = false,
255
258
  chat_keypad_type?: ChatKeypadTypeEnum | undefined,
256
259
  ) => {
257
260
  const replyId = this.ensureReply();
@@ -273,7 +276,7 @@ class Update {
273
276
  replyPoll = async (
274
277
  question: string,
275
278
  options: string[],
276
- auto_delete: number | boolean = false,
279
+ auto_delete: number | false = false,
277
280
  ) => {
278
281
  return await this.bot.sendPoll(
279
282
  this.chat_id,
@@ -304,8 +307,6 @@ class Update {
304
307
 
305
308
  if (!user && !userId)
306
309
  throw this.bot.logger.error("No data to ban user", "warn");
307
-
308
- //@ts-ignore
309
310
  return await this.bot.banChatMember(this.chat_id, userId || user);
310
311
  };
311
312
 
@@ -313,8 +314,6 @@ class Update {
313
314
  const user = this.ensureReply(true);
314
315
  if (!user && !userId)
315
316
  throw this.bot.logger.error("No data to unban user", "warn");
316
-
317
- //@ts-ignore
318
317
  return await this.bot.banChatMember(this.chat_id, userId || user);
319
318
  };
320
319
 
@@ -322,12 +321,12 @@ class Update {
322
321
  const message_id = this.ensureReply();
323
322
  if (!text && !inline_keypad && !message_id)
324
323
  throw this.bot.logger.error("No data to edit message", "warn");
325
- //@ts-ignore
326
- if (text) await this.bot.editMessageText(this.chat_id, text, message_id);
324
+
327
325
  if (inline_keypad) {
328
- //@ts-ignore
329
326
  await this.bot.editMessageKeypad(this.chat_id, message_id, inline_keypad);
330
327
  }
328
+
329
+ if (text) await this.bot.editMessageText(this.chat_id, text, message_id);
331
330
  };
332
331
 
333
332
  private ensureReply(isId = false) {
package/bot/filters.ts CHANGED
@@ -11,7 +11,7 @@ export default class Filters {
11
11
 
12
12
  static USERNAME_PATTERN = /@([a-zA-Z0-9_]{3,32})/;
13
13
 
14
- static findKey(message: any, key: string): any {
14
+ static findKey(message: Record<string, any>, key: string){
15
15
  if (!message || typeof message !== "object") {
16
16
  return undefined;
17
17
  }
@@ -42,11 +42,11 @@ export default class Filters {
42
42
  return undefined;
43
43
  }
44
44
 
45
- static isText(message: Update): boolean {
45
+ static isText(message: Update<unknown>): boolean {
46
46
  return !!Filters.findKey(message, "text");
47
47
  }
48
48
 
49
- static isPersian(message: Update): boolean {
49
+ static isPersian(message: Update<unknown>): boolean {
50
50
  const text = Filters.findKey(message, "text");
51
51
  if (!text) return false;
52
52
 
@@ -66,115 +66,117 @@ export default class Filters {
66
66
  return false;
67
67
  }
68
68
 
69
- static isLocation(message: Update): boolean {
69
+ static isLocation(message: Update<unknown>): boolean {
70
70
  return !!Filters.findKey(message, "location");
71
71
  }
72
72
 
73
- static isTag(message: Update): boolean {
73
+ static isTag(message: Update<unknown>): boolean {
74
74
  const text = Filters.findKey(message, "text");
75
75
  return text ? text.includes("#") : false;
76
76
  }
77
77
 
78
- static isSpam(message: Update): boolean {
78
+ static isSpam(message: Update<unknown>): boolean {
79
79
  const text = Filters.findKey(message, "text");
80
80
  return text ? text.length > 1000 : false;
81
81
  }
82
82
 
83
- static isSticker(message: Update): boolean {
83
+ static isSticker(message: Update<unknown>): boolean {
84
84
  return !!Filters.findKey(message, "sticker");
85
85
  }
86
86
 
87
- static isLink(message: Update): boolean {
87
+ static isLink(message: Update<unknown>): boolean {
88
88
  const text = Filters.findKey(message, "text");
89
89
  return text ? Filters.linkify.test(text) : false;
90
90
  }
91
91
 
92
- static isUsername(message: Update): boolean {
92
+ static isUsername(message: Update<unknown>): boolean {
93
93
  const text = Filters.findKey(message, "text");
94
94
  return text ? Filters.USERNAME_PATTERN.test(text) : false;
95
95
  }
96
96
 
97
- static isForward(message: Update): boolean {
97
+ static isForward(message: Update<unknown>): boolean {
98
98
  return !!Filters.findKey(message, "forwarded_from");
99
99
  }
100
100
 
101
- static isReply(message: Update): boolean {
101
+ static isReply(message: Update<unknown>): boolean {
102
102
  return !!Filters.findKey(message, "reply_to_message_id");
103
103
  }
104
104
 
105
- static isContact(message: Update): boolean {
105
+ static isContact(message: Update<unknown>): boolean {
106
106
  return !!Filters.findKey(message, "contact_message");
107
107
  }
108
108
 
109
- static isPoll(message: Update): boolean {
109
+ static isPoll(message: Update<unknown>): boolean {
110
110
  return !!Filters.findKey(message, "poll");
111
111
  }
112
112
 
113
- static isLiveLocation(message: Update): boolean {
113
+ static isLiveLocation(message: Update<unknown>): boolean {
114
114
  return !!Filters.findKey(message, "live_location");
115
115
  }
116
116
 
117
- static isFile(message: Update): boolean {
117
+ static isFile(message: Update<unknown>): boolean {
118
118
  return !!Filters.findKey(message, "file");
119
119
  }
120
120
 
121
- static isMention(message: Update): boolean {
121
+ static isMention(message: Update<unknown>): boolean {
122
122
  return !!Filters.findKey(
123
123
  Filters.findKey(message, "meta_data_parts") || {},
124
124
  "link",
125
125
  );
126
126
  }
127
127
 
128
- static isMarkdown(message: Update): boolean {
128
+ static isMarkdown(message: Update<unknown>): boolean {
129
129
  return !!Filters.findKey(message, "metadata");
130
130
  }
131
131
 
132
- static isDelete(message: Update): boolean {
132
+ static isDelete(message: Update<unknown>): boolean {
133
133
  return !!Filters.findKey(message, "removed_message_id");
134
134
  }
135
135
 
136
- static isPayment(message: Update): boolean {
136
+ static isPayment(message: Update<unknown>): boolean {
137
137
  return !!Filters.findKey(message, "updated_payment");
138
138
  }
139
139
 
140
- static isPrivate(message: Update | Inline): boolean {
140
+ static isPrivate(message: Update<unknown> | Inline<unknown>): boolean {
141
141
  return message.chat_id.startsWith("b0");
142
142
  }
143
143
 
144
- static isGroup(message: Update | Inline): boolean {
144
+ static isGroup(message: Update<unknown> | Inline<unknown>): boolean {
145
145
  return message.chat_id.startsWith("g0");
146
146
  }
147
147
 
148
- static isChannel(message: Update | Inline): boolean {
148
+ static isChannel(message: Update<unknown> | Inline<unknown>): boolean {
149
149
  return message.chat_id.startsWith("c0");
150
150
  }
151
151
 
152
- static isNewMessage(message: Update): boolean {
152
+ static isNewMessage(message: Update<unknown>): boolean {
153
153
  return message.type === "NewMessage";
154
154
  }
155
155
 
156
- static isUpdatedMessage(message: Update): boolean {
156
+ static isUpdatedMessage(message: Update<unknown>): boolean {
157
157
  return message.type === "UpdatedMessage";
158
158
  }
159
159
 
160
- static isRemovedMessage(message: Update): boolean {
160
+ static isRemovedMessage(message: Update<unknown>): boolean {
161
161
  return message.type === "RemovedMessage";
162
162
  }
163
163
 
164
- static isStartedBot(message: Update): boolean {
164
+ static isStartedBot(message: Update<unknown>): boolean {
165
165
  return message.type === "StartedBot";
166
166
  }
167
167
 
168
- static isStoppedBot(message: Update): boolean {
168
+ static isStoppedBot(message: Update<unknown>): boolean {
169
169
  return message.type === "StoppedBot";
170
170
  }
171
171
 
172
- static isUpdatedPayment(message: Update): boolean {
172
+ static isUpdatedPayment(message: Update<unknown>): boolean {
173
173
  return message.type === "UpdatedPayment";
174
174
  }
175
175
 
176
- static kypadID(button_id: string): (message: Update | Inline) => boolean {
177
- return (message: Update | Inline) => {
176
+ static kypadID(
177
+ button_id: string,
178
+ ): (message: Update<unknown> | Inline<unknown>) => boolean {
179
+ return (message: Update<unknown> | Inline<unknown>) => {
178
180
  const res = Filters.findKey(message, "button_id");
179
181
  return res === button_id;
180
182
  };
package/bot/index.ts CHANGED
@@ -3,8 +3,10 @@ import { AntiSpam } from "./utils";
3
3
  import Filters from "./filters";
4
4
  import Utils from "../utils/formater";
5
5
  import type * as Enums from "./types/enums";
6
- import { Update, Inline } from "./contexts/index";
6
+ export * as Contexts from "./contexts";
7
+ export * from "../utils";
8
+
7
9
 
8
10
  export default Bot;
9
11
  export { Bot, Filters, Utils, AntiSpam };
10
- export type { Enums, Update, Inline };
12
+ export type { Enums };
@@ -5,23 +5,18 @@ async function builder(
5
5
  method: string,
6
6
  input: object = {},
7
7
  ): Promise<any> {
8
- if (!this.token) {
9
- throw this.logger.error(
10
- "[builder] Bot token is not set. Please provide a valid token.",
11
- "error",
12
- );
13
- }
14
-
15
8
  const response: any = await this.network.request(method, input);
9
+ if (response === false) return { status_message: "NETWORK_ERROR" };
10
+
11
+ if (response?.status === "OK") {
12
+ return { ...response.data, status_message: "OK" };
13
+ }
16
14
 
17
- if (response?.status !== "OK") {
18
- throw this.logger.error(
19
- `[builder] error in request ${method}:\n ${JSON.stringify(response, null, 2)}`,
20
- "error",
21
- );
15
+ if (method === "getMe" && response.status === "INVALID_ACCESS") {
16
+ return { status_message: "INVALID_TOKEN" };
22
17
  }
23
18
 
24
- return response.data;
19
+ return { status_message: response.status };
25
20
  }
26
21
 
27
22
  export default builder;
@@ -15,7 +15,7 @@ async function _sendFile(
15
15
  disable_notification = false,
16
16
  reply_to_message_id?: string,
17
17
  chat_keypad_type?: ChatKeypadTypeEnum,
18
- auto_delete: number | boolean = false,
18
+ auto_delete: number | false = false,
19
19
  ) {
20
20
  const { upload_url } = await this.requestSendFile(type);
21
21
  const {
@@ -44,7 +44,10 @@ async function _sendFile(
44
44
 
45
45
  const res = await this.builder("sendFile", data);
46
46
 
47
- if (auto_delete !== false) await this.deleteMessage(chat_id, res.message_id);
47
+ if (auto_delete !== false)
48
+ setTimeout(async () => {
49
+ await this.deleteMessage(chat_id, res.message_id);
50
+ }, auto_delete);
48
51
 
49
52
  return res;
50
53
  }
@@ -15,7 +15,10 @@ async function uploadFile(
15
15
  if (typeof source === "string") {
16
16
  if (source.startsWith("http://") || source.startsWith("https://")) {
17
17
  // download file
18
- console.log(`[ uploadFile ] Downloading from URL: ${source}`);
18
+ this.logger.error(
19
+ `[ uploadFile ] Downloading from URL: ${source}`,
20
+ "warn",
21
+ );
19
22
  const res = await fetch(source);
20
23
  if (!res.ok) {
21
24
  throw this.logger.error(
@@ -67,7 +70,7 @@ async function uploadFile(
67
70
  throw this.logger.error(`HTTP ${res.status}: ${text}`, "warn");
68
71
  }
69
72
 
70
- const response = await res.json();
73
+ const response: any = await res.json();
71
74
 
72
75
  if (response.status !== "OK") {
73
76
  throw this.logger.error(