rubika 1.0.5 → 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.
Files changed (67) hide show
  1. package/README.md +3 -3
  2. package/{src/bot.ts → bot.ts} +27 -12
  3. package/contexts/index.ts +4 -0
  4. package/contexts/inline.ts +302 -0
  5. package/contexts/update.ts +351 -0
  6. package/filters.ts +182 -0
  7. package/index.ts +9 -0
  8. package/{src/methods → methods}/advanced/builder.ts +10 -6
  9. package/methods/chat/index.ts +6 -0
  10. package/{src/methods → methods}/files/_sendFile.ts +8 -3
  11. package/{src/methods → methods}/files/uploadFile.ts +12 -8
  12. package/{src/methods → methods}/index.ts +14 -0
  13. package/methods/messages/sendContact.ts +45 -0
  14. package/{src/methods → methods}/messages/sendFile.ts +4 -2
  15. package/{src/methods → methods}/messages/sendGif.ts +2 -0
  16. package/{src/methods → methods}/messages/sendImage.ts +2 -0
  17. package/{src/methods → methods}/messages/sendLocation.ts +7 -2
  18. package/{src/methods → methods}/messages/sendMessage.ts +11 -8
  19. package/{src/methods → methods}/messages/sendMusic.ts +2 -0
  20. package/methods/messages/sendPoll.ts +17 -0
  21. package/{src/methods → methods}/messages/sendSticker.ts +6 -2
  22. package/{src/methods → methods}/messages/sendVideo.ts +2 -0
  23. package/{src/methods → methods}/messages/sendVoice.ts +2 -0
  24. package/{src/methods → methods}/utilities/handleUpdates.ts +9 -7
  25. package/{src/methods → methods}/utilities/polling.ts +5 -5
  26. package/{src/methods → methods}/utilities/run.ts +4 -1
  27. package/{src/methods → methods}/utilities/start.ts +2 -2
  28. package/{src/methods → methods}/utilities/webhook.ts +4 -3
  29. package/{src/network.ts → network.ts} +1 -1
  30. package/package.json +10 -7
  31. package/{src/types → types}/handlers.ts +4 -4
  32. package/{src/types → types}/interfaces.ts +0 -21
  33. package/{src/types → types}/utils.ts +10 -2
  34. package/utils/antiSpam.ts +47 -0
  35. package/utils/errors.ts +20 -0
  36. package/utils/index.ts +7 -0
  37. package/src/filters.ts +0 -92
  38. package/src/index.ts +0 -9
  39. package/src/methods/chat/index.ts +0 -3
  40. package/src/methods/messages/sendContact.ts +0 -40
  41. package/src/methods/messages/sendPoll.ts +0 -12
  42. /package/{src/methods → methods}/advanced/index.ts +0 -0
  43. /package/{src/methods → methods}/bot/getMe.ts +0 -0
  44. /package/{src/methods → methods}/bot/index.ts +0 -0
  45. /package/{src/methods → methods}/chat/banChatMember.ts +0 -0
  46. /package/{src/methods → methods}/chat/getChat.ts +0 -0
  47. /package/{src/methods → methods}/chat/unbanChatMember.ts +0 -0
  48. /package/{src/methods → methods}/files/getFile.ts +0 -0
  49. /package/{src/methods → methods}/files/index.ts +0 -0
  50. /package/{src/methods → methods}/files/requestSendFile.ts +0 -0
  51. /package/{src/methods → methods}/messages/deleteMessage.ts +0 -0
  52. /package/{src/methods → methods}/messages/editChatKeypad.ts +0 -0
  53. /package/{src/methods → methods}/messages/editMessageKeypad.ts +0 -0
  54. /package/{src/methods → methods}/messages/editMessageText.ts +0 -0
  55. /package/{src/methods → methods}/messages/forwardMessage.ts +0 -0
  56. /package/{src/methods → methods}/messages/index.ts +0 -0
  57. /package/{src/methods → methods}/settings/index.ts +0 -0
  58. /package/{src/methods → methods}/settings/setCommands.ts +0 -0
  59. /package/{src/methods → methods}/settings/updateBotEndpoints.ts +0 -0
  60. /package/{src/methods → methods}/utilities/getUpdates.ts +0 -0
  61. /package/{src/methods → methods}/utilities/index.ts +0 -0
  62. /package/{src/types → types}/enums.ts +0 -0
  63. /package/{src/types → types}/methods.ts +0 -0
  64. /package/{src/utils → utils}/checkFilter.ts +0 -0
  65. /package/{src/utils.ts → utils/formater.ts} +0 -0
  66. /package/{src/utils → utils}/parser.ts +0 -0
  67. /package/{src/utils → utils}/prompt.ts +0 -0
package/README.md CHANGED
@@ -24,8 +24,8 @@ bun add rubika
24
24
 
25
25
  ## › نمونه کد ساده
26
26
 
27
- ```js
28
- const { Bot, Filters } = require("rubika");
27
+ ```ts
28
+ import Bot, { Filters } from "rubika";
29
29
 
30
30
  const bot = new Bot("rubika");
31
31
 
@@ -33,7 +33,7 @@ bot.command("/start", async (ctx, bot) => {
33
33
  await bot.sendMessage(ctx.chat_id, "🤖 ربات استارت شد");
34
34
  });
35
35
 
36
- bot.on("message", [Filters.isText], async (ctx, bot) => {
36
+ bot.on("update", [Filters.isText], async (ctx, bot) => {
37
37
  await bot.sendMessage(ctx.chat_id, "سلام 😎");
38
38
  });
39
39
 
@@ -1,7 +1,9 @@
1
1
  import Network from "./network";
2
2
  import Methods from "./methods";
3
- import type { BotInfo, Update } from "./types/interfaces";
3
+ import type { BotInfo } from "./types/interfaces";
4
4
  import type { ContextMap, Handler, NestedFilter } from "./types/handlers";
5
+ import Update from "./contexts/update";
6
+ import Logger from "./utils/errors";
5
7
 
6
8
  class Bot extends Methods {
7
9
  protected initialize: boolean = false;
@@ -11,7 +13,9 @@ class Bot extends Methods {
11
13
 
12
14
  public handlers: {
13
15
  [K in keyof ContextMap]: Handler<ContextMap[K]>[];
14
- } = { inline: [], update: [] };
16
+ } = { inline: [], update: [], error: [] };
17
+
18
+ public logger = new Logger(this.handlers.error);
15
19
 
16
20
  /**
17
21
  * نمونه‌ای از کلاس ربات را ایجاد می‌کند.
@@ -27,7 +31,10 @@ class Bot extends Methods {
27
31
  * const bot = new Bot("your-token-here", 15000);
28
32
  * ```
29
33
  */
30
- constructor(public token: string, timeout: number = 10000) {
34
+ constructor(
35
+ public token: string,
36
+ timeout: number = 10000,
37
+ ) {
31
38
  super();
32
39
  this.BASE_URL = `https://botapi.rubika.ir/v3/${token}`;
33
40
  this.network = new Network(this.BASE_URL, timeout);
@@ -50,6 +57,10 @@ class Bot extends Methods {
50
57
  * console.log("پیام جدید دریافت شد:", ctx.text);
51
58
  * });
52
59
  *
60
+ * bot.on("error", (err) => {
61
+ * console.log("ارور دریافت شد", err);
62
+ * });
63
+ *
53
64
  * bot.on("update", [filter.isText], (ctx) => {
54
65
  * console.log("پیام شامل متن است:", ctx.text);
55
66
  * });
@@ -57,7 +68,7 @@ class Bot extends Methods {
57
68
  */
58
69
  on<T extends keyof typeof this.handlers>(
59
70
  type: T,
60
- handler: (ctx: ContextMap[T], bot: Bot) => Promise<void>
71
+ handler: (ctx: ContextMap[T]) => Promise<void>,
61
72
  ): void;
62
73
 
63
74
  /**
@@ -76,6 +87,10 @@ class Bot extends Methods {
76
87
  * console.log("پیام جدید دریافت شد:", ctx.text);
77
88
  * });
78
89
  *
90
+ * bot.on("error", (err) => {
91
+ * console.log("ارور دریافت شد", err);
92
+ * });
93
+ *
79
94
  * bot.on("update", [filter.isText], (ctx) => {
80
95
  * console.log("پیام شامل متن است:", ctx.text);
81
96
  * });
@@ -84,15 +99,15 @@ class Bot extends Methods {
84
99
  on<T extends keyof typeof this.handlers>(
85
100
  type: T,
86
101
  filters: NestedFilter<ContextMap[T]>,
87
- handler: (ctx: ContextMap[T], bot: Bot) => Promise<void>
102
+ handler: (ctx: ContextMap[T]) => Promise<void>,
88
103
  ): void;
89
104
 
90
105
  on<T extends keyof typeof this.handlers>(
91
106
  type: T,
92
107
  filtersOrHandler:
93
108
  | NestedFilter<ContextMap[T]>
94
- | ((ctx: ContextMap[T], bot: Bot) => Promise<void>),
95
- maybeHandler?: (ctx: ContextMap[T], bot: Bot) => Promise<void>
109
+ | ((ctx: ContextMap[T]) => Promise<void>),
110
+ maybeHandler?: (ctx: ContextMap[T]) => Promise<void>,
96
111
  ): void {
97
112
  if (typeof filtersOrHandler === "function") {
98
113
  this.handlers[type].push({
@@ -134,7 +149,7 @@ class Bot extends Methods {
134
149
  */
135
150
  command(
136
151
  prefix: string | RegExp,
137
- handler: (ctx: Update, bot: Bot) => Promise<void>
152
+ handler: (ctx: Update) => Promise<void>,
138
153
  ): void;
139
154
 
140
155
  /**
@@ -166,15 +181,15 @@ class Bot extends Methods {
166
181
  command(
167
182
  prefix: string | RegExp,
168
183
  filters: NestedFilter<ContextMap["update"]>,
169
- handler: (ctx: Update, bot: Bot) => Promise<void>
184
+ handler: (ctx: Update) => Promise<void>,
170
185
  ): void;
171
186
 
172
187
  command(
173
188
  prefix: string | RegExp,
174
189
  filtersOrHandler:
175
190
  | NestedFilter<ContextMap["update"]>
176
- | ((ctx: Update, bot: Bot) => Promise<void>),
177
- maybeHandler?: (ctx: Update, bot: Bot) => Promise<void>
191
+ | ((ctx: Update) => Promise<void>),
192
+ maybeHandler?: (ctx: Update) => Promise<void>,
178
193
  ) {
179
194
  if (typeof filtersOrHandler === "function") {
180
195
  this.handlers.update.push({
@@ -189,7 +204,7 @@ class Bot extends Methods {
189
204
  prefix,
190
205
  });
191
206
  } else {
192
- throw new Error("Invalid arguments for command()");
207
+ throw this.logger.error("Invalid arguments for command()", "warn");
193
208
  }
194
209
  }
195
210
  }
@@ -0,0 +1,4 @@
1
+ import Inline from "./inline";
2
+ import Update from "./update";
3
+
4
+ export { Inline, Update };
@@ -0,0 +1,302 @@
1
+ import Bot from "..";
2
+ import util from "util";
3
+ import { AuxData, File, InlineKeypad, Keypad, Location } from "../types/interfaces";
4
+ import { ChatKeypadTypeEnum } from "../types/enums";
5
+ import { FileSource } from "../types/methods";
6
+
7
+ class Inline {
8
+ sender_id: string;
9
+ text: string;
10
+ file?: File;
11
+ location?: Location;
12
+ aux_data?: AuxData;
13
+ message_id: string;
14
+ chat_id: string;
15
+ store: Record<string, any> = {};
16
+
17
+ declare bot: Bot;
18
+
19
+ constructor(ctx: any, bot: Bot) {
20
+ this.sender_id = ctx.sender_id;
21
+ this.text = ctx.text;
22
+ this.file = ctx.file;
23
+ this.location = ctx.location;
24
+ this.aux_data = ctx.aux_data;
25
+ this.message_id = ctx.message_id;
26
+ this.chat_id = ctx.chat_id;
27
+ this.bot = bot;
28
+ }
29
+
30
+ reply = async (
31
+ text: string,
32
+ chat_keypad?: Keypad,
33
+ inline_keypad?: InlineKeypad,
34
+ disable_notification?: boolean,
35
+ auto_delete: number | boolean = false,
36
+ chat_keypad_type?: ChatKeypadTypeEnum | undefined,
37
+ ) => {
38
+ return await this.bot.sendMessage(
39
+ this.chat_id,
40
+ text,
41
+ chat_keypad,
42
+ inline_keypad,
43
+ disable_notification,
44
+ this.message_id,
45
+ chat_keypad_type,
46
+ auto_delete,
47
+ );
48
+ };
49
+
50
+ replyImage = async (
51
+ text: string,
52
+ file: FileSource,
53
+ chat_keypad?: Keypad,
54
+ inline_keypad?: InlineKeypad,
55
+ disable_notification?: boolean,
56
+ auto_delete: number | boolean = false,
57
+ chat_keypad_type?: ChatKeypadTypeEnum | undefined,
58
+ ) => {
59
+ return await this.bot.sendImage(
60
+ this.chat_id,
61
+ file,
62
+ text,
63
+ chat_keypad,
64
+ inline_keypad,
65
+ disable_notification,
66
+ this.message_id,
67
+ chat_keypad_type,
68
+ auto_delete,
69
+ );
70
+ };
71
+
72
+ replyVideo = async (
73
+ text: string,
74
+ file: FileSource,
75
+ chat_keypad?: Keypad,
76
+ inline_keypad?: InlineKeypad,
77
+ disable_notification?: boolean,
78
+ auto_delete: number | boolean = false,
79
+ chat_keypad_type?: ChatKeypadTypeEnum | undefined,
80
+ ) => {
81
+ return await this.bot.sendVideo(
82
+ this.chat_id,
83
+ file,
84
+ text,
85
+ chat_keypad,
86
+ inline_keypad,
87
+ disable_notification,
88
+ this.message_id,
89
+ chat_keypad_type,
90
+ auto_delete,
91
+ );
92
+ };
93
+
94
+ replyGif = async (
95
+ text: string,
96
+ file: FileSource,
97
+ chat_keypad?: Keypad,
98
+ inline_keypad?: InlineKeypad,
99
+ disable_notification?: boolean,
100
+ auto_delete: number | boolean = false,
101
+ chat_keypad_type?: ChatKeypadTypeEnum | undefined,
102
+ ) => {
103
+ return await this.bot.sendGif(
104
+ this.chat_id,
105
+ file,
106
+ text,
107
+ chat_keypad,
108
+ inline_keypad,
109
+ disable_notification,
110
+ this.message_id,
111
+ chat_keypad_type,
112
+ auto_delete,
113
+ );
114
+ };
115
+
116
+ replySticker = async (
117
+ sticker_id: string,
118
+ chat_keypad?: Keypad,
119
+ inline_keypad?: InlineKeypad,
120
+ disable_notification?: boolean,
121
+ auto_delete: number | boolean = false,
122
+ chat_keypad_type?: ChatKeypadTypeEnum | undefined,
123
+ ) => {
124
+ return await this.bot.sendSticker(
125
+ this.chat_id,
126
+ sticker_id,
127
+ chat_keypad,
128
+ inline_keypad,
129
+ disable_notification,
130
+ this.message_id,
131
+ chat_keypad_type,
132
+ auto_delete,
133
+ );
134
+ };
135
+
136
+ replyMusic = async (
137
+ text: string,
138
+ file: FileSource,
139
+ chat_keypad?: Keypad,
140
+ inline_keypad?: InlineKeypad,
141
+ disable_notification?: boolean,
142
+ auto_delete: number | boolean = false,
143
+ chat_keypad_type?: ChatKeypadTypeEnum | undefined,
144
+ ) => {
145
+ return await this.bot.sendMusic(
146
+ this.chat_id,
147
+ file,
148
+ text,
149
+ chat_keypad,
150
+ inline_keypad,
151
+ disable_notification,
152
+ this.message_id,
153
+ chat_keypad_type,
154
+ auto_delete,
155
+ );
156
+ };
157
+
158
+ replyVoice = async (
159
+ text: string,
160
+ file: FileSource,
161
+ chat_keypad?: Keypad,
162
+ inline_keypad?: InlineKeypad,
163
+ disable_notification?: boolean,
164
+ auto_delete: number | boolean = false,
165
+ chat_keypad_type?: ChatKeypadTypeEnum | undefined,
166
+ ) => {
167
+ return await this.bot.sendVoice(
168
+ this.chat_id,
169
+ file,
170
+ text,
171
+ chat_keypad,
172
+ inline_keypad,
173
+ disable_notification,
174
+ this.message_id,
175
+ chat_keypad_type,
176
+ auto_delete,
177
+ );
178
+ };
179
+
180
+ replyFile = async (
181
+ text: string,
182
+ file: FileSource,
183
+ chat_keypad?: Keypad,
184
+ inline_keypad?: InlineKeypad,
185
+ disable_notification?: boolean,
186
+ auto_delete: number | boolean = false,
187
+ chat_keypad_type?: ChatKeypadTypeEnum | undefined,
188
+ ) => {
189
+ return await this.bot.sendFile(
190
+ this.chat_id,
191
+ file,
192
+ text,
193
+ chat_keypad,
194
+ inline_keypad,
195
+ disable_notification,
196
+ this.message_id,
197
+ chat_keypad_type,
198
+ auto_delete,
199
+ );
200
+ };
201
+
202
+ replyLocation = async (
203
+ latitude: string,
204
+ longitude: string,
205
+ chat_keypad?: Keypad,
206
+ inline_keypad?: InlineKeypad,
207
+ disable_notification?: boolean,
208
+ auto_delete: number | boolean = false,
209
+ chat_keypad_type?: ChatKeypadTypeEnum | undefined,
210
+ ) => {
211
+ return await this.bot.sendLocation(
212
+ this.chat_id,
213
+ latitude,
214
+ longitude,
215
+ chat_keypad,
216
+ inline_keypad,
217
+ disable_notification,
218
+ this.message_id,
219
+ chat_keypad_type,
220
+ auto_delete,
221
+ );
222
+ };
223
+
224
+ replyContact = async (
225
+ first_name: string,
226
+ last_name: string,
227
+ phone_number: string,
228
+ chat_keypad?: Keypad,
229
+ inline_keypad?: InlineKeypad,
230
+ disable_notification?: boolean,
231
+ auto_delete: number | boolean = false,
232
+ chat_keypad_type?: ChatKeypadTypeEnum | undefined,
233
+ ) => {
234
+ return await this.bot.sendContact(
235
+ this.chat_id,
236
+ first_name,
237
+ last_name,
238
+ phone_number,
239
+ chat_keypad,
240
+ inline_keypad,
241
+ disable_notification,
242
+ this.message_id,
243
+ chat_keypad_type,
244
+ auto_delete,
245
+ );
246
+ };
247
+
248
+ replyPoll = async (
249
+ question: string,
250
+ options: string[],
251
+ auto_delete: number | boolean = false,
252
+ ) => {
253
+ return await this.bot.sendPoll(
254
+ this.chat_id,
255
+ question,
256
+ options,
257
+ auto_delete,
258
+ );
259
+ };
260
+
261
+ forward = async (to_chat_id: string) => {
262
+ return await this.bot.forwardMessage(
263
+ this.chat_id,
264
+ this.message_id,
265
+ to_chat_id,
266
+ );
267
+ };
268
+
269
+ delete = async (messae_id?: string) => {
270
+ return await this.bot.deleteMessage(
271
+ this.chat_id,
272
+ messae_id || this.message_id,
273
+ );
274
+ };
275
+
276
+ editMessage = async (text?: string, inline_keypad?: InlineKeypad) => {
277
+ if (text)
278
+ await this.bot.editMessageText(this.chat_id, text, this.message_id);
279
+
280
+ if (inline_keypad)
281
+ await this.bot.editMessageKeypad(
282
+ this.chat_id,
283
+ this.message_id,
284
+ inline_keypad,
285
+ );
286
+ };
287
+
288
+ [util.inspect.custom]() {
289
+ return {
290
+ sender_id: this.sender_id,
291
+ text: this.text,
292
+ file: this.file,
293
+ location: this.location,
294
+ aux_data: this.aux_data,
295
+ message_id: this.message_id,
296
+ chat_id: this.chat_id,
297
+ store: this.store,
298
+ };
299
+ }
300
+ }
301
+
302
+ export default Inline;