gramio 0.0.50 → 0.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.
package/package.json CHANGED
@@ -1,10 +1,23 @@
1
1
  {
2
2
  "name": "gramio",
3
- "type": "commonjs",
4
- "version": "0.0.50",
3
+ "type": "module",
4
+ "version": "0.1.0",
5
5
  "description": "Powerful, extensible and really type-safe Telegram Bot API framework",
6
- "main": "./dist/index.js",
7
- "types": "./dist/index.d.ts",
6
+ "main": "dist/index.cjs",
7
+ "module": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "require": {
16
+ "types": "./dist/index.d.cts",
17
+ "default": "./dist/index.cjs"
18
+ }
19
+ }
20
+ },
8
21
  "keywords": [
9
22
  "telegram",
10
23
  "telegram-bot",
@@ -20,26 +33,26 @@
20
33
  "type": "tsc --noEmit",
21
34
  "lint": "bunx @biomejs/biome check ./src",
22
35
  "lint:fix": "bun lint --apply",
23
- "prepublishOnly": "tsc",
36
+ "prepublishOnly": "bunx pkgroll",
24
37
  "jsr": "bun scripts/release-jsr.ts",
25
38
  "try-deno": "deno publish --unstable-sloppy-imports --dry-run --allow-slow-types --allow-dirty"
26
39
  },
27
40
  "author": "kravets",
28
41
  "license": "MIT",
29
42
  "devDependencies": {
30
- "@biomejs/biome": "1.9.1",
31
- "@types/bun": "^1.1.9",
43
+ "@biomejs/biome": "1.9.4",
44
+ "@types/bun": "^1.1.12",
32
45
  "@types/debug": "^4.1.12",
33
46
  "pkgroll": "^2.5.0",
34
- "typescript": "^5.6.2"
47
+ "typescript": "^5.6.3"
35
48
  },
36
49
  "dependencies": {
37
50
  "@gramio/callback-data": "^0.0.3",
38
51
  "@gramio/contexts": "^0.0.23",
39
- "@gramio/files": "^0.0.12",
40
- "@gramio/format": "^0.1.3",
52
+ "@gramio/files": "^0.1.0",
53
+ "@gramio/format": "^0.1.4",
41
54
  "@gramio/keyboards": "^0.3.3",
42
- "@gramio/types": "^7.10.1",
55
+ "@gramio/types": "^7.10.2",
43
56
  "debug": "^4.3.7",
44
57
  "inspectable": "^3.0.2",
45
58
  "middleware-io": "^2.8.1"
package/dist/bot.d.ts DELETED
@@ -1,404 +0,0 @@
1
- import { CallbackData } from "@gramio/callback-data";
2
- import { type Attachment, type Context, type ContextType, type MaybeArray, type UpdateName } from "@gramio/contexts";
3
- import type { APIMethodParams, APIMethods, SetMyCommandsParams, TelegramBotCommand, TelegramReactionTypeEmojiEmoji, TelegramUser } from "@gramio/types";
4
- import type { AnyBot, AnyPlugin, BotOptions, DeriveDefinitions, ErrorDefinitions, Handler, Hooks, MaybePromise, SuppressedAPIMethods } from "./types";
5
- import { Updates } from "./updates";
6
- /** Bot instance
7
- *
8
- * @example
9
- * ```ts
10
- * import { Bot } from "gramio";
11
- *
12
- * const bot = new Bot("") // put you token here
13
- * .command("start", (context) => context.send("Hi!"))
14
- * .onStart(console.log);
15
- *
16
- * bot.start();
17
- * ```
18
- */
19
- export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDefinitions = DeriveDefinitions> {
20
- _: {
21
- /** @internal. Remap generic */
22
- derives: Derives;
23
- };
24
- /** @internal. Remap generic */
25
- __Derives: Derives;
26
- private filters;
27
- /** Options provided to instance */
28
- readonly options: BotOptions;
29
- /** Bot data (filled in when calling bot.init/bot.start) */
30
- info: TelegramUser | undefined;
31
- /**
32
- * Send API Request to Telegram Bot API
33
- *
34
- * @example
35
- * ```ts
36
- * const response = await bot.api.sendMessage({
37
- * chat_id: "@gramio_forum",
38
- * text: "some text",
39
- * });
40
- * ```
41
- *
42
- * [Documentation](https://gramio.dev/bot-api.html)
43
- */
44
- readonly api: SuppressedAPIMethods;
45
- private lazyloadPlugins;
46
- private dependencies;
47
- private errorsDefinitions;
48
- private errorHandler;
49
- /** This instance handle updates */
50
- updates: Updates;
51
- private hooks;
52
- constructor(token: string, options?: Omit<BotOptions, "token" | "api"> & {
53
- api?: Partial<BotOptions["api"]>;
54
- });
55
- constructor(options: Omit<BotOptions, "api"> & {
56
- api?: Partial<BotOptions["api"]>;
57
- });
58
- private runHooks;
59
- private runImmutableHooks;
60
- private _callApi;
61
- /**
62
- * Download file
63
- *
64
- * @example
65
- * ```ts
66
- * bot.on("message", async (context) => {
67
- * if (!context.document) return;
68
- * // download to ./file-name
69
- * await context.download(context.document.fileName || "file-name");
70
- * // get ArrayBuffer
71
- * const buffer = await context.download();
72
- *
73
- * return context.send("Thank you!");
74
- * });
75
- * ```
76
- * [Documentation](https://gramio.dev/files/download.html)
77
- */
78
- downloadFile(attachment: Attachment | {
79
- file_id: string;
80
- } | string): Promise<ArrayBuffer>;
81
- downloadFile(attachment: Attachment | {
82
- file_id: string;
83
- } | string, path: string): Promise<string>;
84
- /**
85
- * Register custom class-error for type-safe catch in `onError` hook
86
- *
87
- * @example
88
- * ```ts
89
- * export class NoRights extends Error {
90
- * needRole: "admin" | "moderator";
91
- *
92
- * constructor(role: "admin" | "moderator") {
93
- * super();
94
- * this.needRole = role;
95
- * }
96
- * }
97
- *
98
- * const bot = new Bot(process.env.TOKEN!)
99
- * .error("NO_RIGHTS", NoRights)
100
- * .onError(({ context, kind, error }) => {
101
- * if (context.is("message") && kind === "NO_RIGHTS")
102
- * return context.send(
103
- * format`You don't have enough rights! You need to have an «${bold(
104
- * error.needRole
105
- * )}» role.`
106
- * );
107
- * });
108
- *
109
- * bot.updates.on("message", (context) => {
110
- * if (context.text === "bun") throw new NoRights("admin");
111
- * });
112
- * ```
113
- */
114
- error<Name extends string, NewError extends {
115
- new (...args: any): any;
116
- prototype: Error;
117
- }>(kind: Name, error: NewError): Bot<Errors & { [name in Name]: InstanceType<NewError>; }, Derives>;
118
- /**
119
- * Set error handler.
120
- * @example
121
- * ```ts
122
- * bot.onError("message", ({ context, kind, error }) => {
123
- * return context.send(`${kind}: ${error.message}`);
124
- * })
125
- * ```
126
- */
127
- onError<T extends UpdateName>(updateName: MaybeArray<T>, handler: Hooks.OnError<Errors, ContextType<typeof this, T> & Derives["global"] & Derives[T]>): this;
128
- onError(handler: Hooks.OnError<Errors, Context<typeof this> & Derives["global"]>): this;
129
- /**
130
- * Derive some data to handlers
131
- *
132
- * @example
133
- * ```ts
134
- * new Bot("token").derive((context) => {
135
- * return {
136
- * superSend: () => context.send("Derived method")
137
- * }
138
- * })
139
- * ```
140
- */
141
- derive<Handler extends Hooks.Derive<Context<typeof this>>>(handler: Handler): Bot<Errors, Derives & {
142
- global: Awaited<ReturnType<Handler>>;
143
- }>;
144
- derive<Update extends UpdateName, Handler extends Hooks.Derive<ContextType<typeof this, Update>>>(updateName: MaybeArray<Update>, handler: Handler): Bot<Errors, Derives & {
145
- [K in Update]: Awaited<ReturnType<Handler>>;
146
- }>;
147
- decorate<Value extends Record<string, any>>(value: Value): Bot<Errors, Derives & {
148
- global: {
149
- [K in keyof Value]: Value[K];
150
- };
151
- }>;
152
- decorate<Name extends string, Value>(name: Name, value: Value): Bot<Errors, Derives & {
153
- global: {
154
- [K in Name]: Value;
155
- };
156
- }>;
157
- /**
158
- * This hook called when the bot is `started`.
159
- *
160
- * @example
161
- * ```typescript
162
- * import { Bot } from "gramio";
163
- *
164
- * const bot = new Bot(process.env.TOKEN!).onStart(
165
- * ({ plugins, info, updatesFrom }) => {
166
- * console.log(`plugin list - ${plugins.join(", ")}`);
167
- * console.log(`bot username is @${info.username}`);
168
- * console.log(`updates from ${updatesFrom}`);
169
- * }
170
- * );
171
- *
172
- * bot.start();
173
- * ```
174
- *
175
- * [Documentation](https://gramio.dev/hooks/on-start.html)
176
- * */
177
- onStart(handler: Hooks.OnStart): this;
178
- /**
179
- * This hook called when the bot stops.
180
- *
181
- * @example
182
- * ```typescript
183
- * import { Bot } from "gramio";
184
- *
185
- * const bot = new Bot(process.env.TOKEN!).onStop(
186
- * ({ plugins, info, updatesFrom }) => {
187
- * console.log(`plugin list - ${plugins.join(", ")}`);
188
- * console.log(`bot username is @${info.username}`);
189
- * }
190
- * );
191
- *
192
- * bot.start();
193
- * bot.stop();
194
- * ```
195
- *
196
- * [Documentation](https://gramio.dev/hooks/on-stop.html)
197
- * */
198
- onStop(handler: Hooks.OnStop): this;
199
- /**
200
- * This hook called before sending a request to Telegram Bot API (allows us to impact the sent parameters).
201
- *
202
- * @example
203
- * ```typescript
204
- * import { Bot } from "gramio";
205
- *
206
- * const bot = new Bot(process.env.TOKEN!).preRequest((context) => {
207
- * if (context.method === "sendMessage") {
208
- * context.params.text = "mutate params";
209
- * }
210
- *
211
- * return context;
212
- * });
213
- *
214
- * bot.start();
215
- * ```
216
- *
217
- * [Documentation](https://gramio.dev/hooks/pre-request.html)
218
- * */
219
- preRequest<Methods extends keyof APIMethods, Handler extends Hooks.PreRequest<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
220
- preRequest(handler: Hooks.PreRequest): this;
221
- /**
222
- * This hook called when API return successful response
223
- *
224
- * [Documentation](https://gramio.dev/hooks/on-response.html)
225
- * */
226
- onResponse<Methods extends keyof APIMethods, Handler extends Hooks.OnResponse<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
227
- onResponse(handler: Hooks.OnResponse): this;
228
- /**
229
- * This hook called when API return an error
230
- *
231
- * [Documentation](https://gramio.dev/hooks/on-response-error.html)
232
- * */
233
- onResponseError<Methods extends keyof APIMethods, Handler extends Hooks.OnResponseError<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
234
- onResponseError(handler: Hooks.OnResponseError): this;
235
- /** Register handler to one or many Updates */
236
- on<T extends UpdateName>(updateName: MaybeArray<T>, handler: Handler<ContextType<typeof this, T> & Derives["global"] & Derives[T]>): this;
237
- /** Register handler to any Updates */
238
- use(handler: Handler<Context<typeof this> & Derives["global"]>): this;
239
- /**
240
- * Extend {@link Plugin} logic and types
241
- *
242
- * @example
243
- * ```ts
244
- * import { Plugin, Bot } from "gramio";
245
- *
246
- * export class PluginError extends Error {
247
- * wow: "type" | "safe" = "type";
248
- * }
249
- *
250
- * const plugin = new Plugin("gramio-example")
251
- * .error("PLUGIN", PluginError)
252
- * .derive(() => {
253
- * return {
254
- * some: ["derived", "props"] as const,
255
- * };
256
- * });
257
- *
258
- * const bot = new Bot(process.env.TOKEN!)
259
- * .extend(plugin)
260
- * .onError(({ context, kind, error }) => {
261
- * if (context.is("message") && kind === "PLUGIN") {
262
- * console.log(error.wow);
263
- * }
264
- * })
265
- * .use((context) => {
266
- * console.log(context.some);
267
- * });
268
- * ```
269
- */
270
- extend<NewPlugin extends AnyPlugin>(plugin: MaybePromise<NewPlugin>): Bot<Errors & NewPlugin["_"]["Errors"], Derives & NewPlugin["_"]["Derives"]>;
271
- /**
272
- * Register handler to reaction (`message_reaction` update)
273
- *
274
- * @example
275
- * ```ts
276
- * new Bot().reaction("👍", async (context) => {
277
- * await context.reply(`Thank you!`);
278
- * });
279
- * ```
280
- * */
281
- reaction(trigger: MaybeArray<TelegramReactionTypeEmojiEmoji>, handler: (context: ContextType<typeof this, "message_reaction"> & Derives["global"] & Derives["message_reaction"]) => unknown): this;
282
- /**
283
- * Register handler to `callback_query` event
284
- *
285
- * @example
286
- * ```ts
287
- * const someData = new CallbackData("example").number("id");
288
- *
289
- * new Bot()
290
- * .command("start", (context) =>
291
- * context.send("some", {
292
- * reply_markup: new InlineKeyboard().text(
293
- * "example",
294
- * someData.pack({
295
- * id: 1,
296
- * })
297
- * ),
298
- * })
299
- * )
300
- * .callbackQuery(someData, (context) => {
301
- * context.queryData; // is type-safe
302
- * });
303
- * ```
304
- */
305
- callbackQuery<Trigger extends CallbackData | string | RegExp>(trigger: Trigger, handler: (context: Omit<ContextType<typeof this, "callback_query">, "data"> & Derives["global"] & Derives["callback_query"] & {
306
- queryData: Trigger extends CallbackData ? ReturnType<Trigger["unpack"]> : RegExpMatchArray | null;
307
- }) => unknown): this;
308
- /** Register handler to `chosen_inline_result` update */
309
- chosenInlineResult<Ctx = ContextType<typeof this, "chosen_inline_result"> & Derives["global"] & Derives["chosen_inline_result"]>(trigger: RegExp | string | ((context: Ctx) => boolean), handler: (context: Ctx & {
310
- args: RegExpMatchArray | null;
311
- }) => unknown): this;
312
- /**
313
- * Register handler to `inline_query` update
314
- *
315
- * @example
316
- * ```ts
317
- * new Bot().inlineQuery(
318
- * /regular expression with (.*)/i,
319
- * async (context) => {
320
- * if (context.args) {
321
- * await context.answer(
322
- * [
323
- * InlineQueryResult.article(
324
- * "id-1",
325
- * context.args[1],
326
- * InputMessageContent.text("some"),
327
- * {
328
- * reply_markup: new InlineKeyboard().text(
329
- * "some",
330
- * "callback-data"
331
- * ),
332
- * }
333
- * ),
334
- * ],
335
- * {
336
- * cache_time: 0,
337
- * }
338
- * );
339
- * }
340
- * },
341
- * {
342
- * onResult: (context) => context.editText("Message edited!"),
343
- * }
344
- * );
345
- * ```
346
- * */
347
- inlineQuery<Ctx = ContextType<typeof this, "inline_query"> & Derives["global"] & Derives["inline_query"]>(trigger: RegExp | string | ((context: Ctx) => boolean), handler: (context: Ctx & {
348
- args: RegExpMatchArray | null;
349
- }) => unknown, options?: {
350
- onResult?: (context: ContextType<Bot, "chosen_inline_result"> & Derives["global"] & Derives["chosen_inline_result"] & {
351
- args: RegExpMatchArray | null;
352
- }) => unknown;
353
- }): this;
354
- /**
355
- * Register handler to `message` and `business_message` event
356
- *
357
- * new Bot().hears(/regular expression with (.*)/i, async (context) => {
358
- * if (context.args) await context.send(`Params ${context.args[1]}`);
359
- * });
360
- */
361
- hears<Ctx = ContextType<typeof this, "message"> & Derives["global"] & Derives["message"]>(trigger: RegExp | string | ((context: Ctx) => boolean), handler: (context: Ctx & {
362
- args: RegExpMatchArray | null;
363
- }) => unknown): this;
364
- /**
365
- * Register handler to `message` and `business_message` event when entities contains a command
366
- *
367
- * new Bot().command("start", async (context) => {
368
- * return context.send(`You message is /start ${context.args}`);
369
- * });
370
- */
371
- command(command: string, handler: (context: ContextType<typeof this, "message"> & Derives["global"] & Derives["message"] & {
372
- args: string | null;
373
- }) => unknown, options?: Omit<SetMyCommandsParams, "commands"> & Omit<TelegramBotCommand, "command">): this;
374
- /** Currently not isolated!!! */
375
- group(grouped: (bot: typeof this) => AnyBot): typeof this;
376
- /**
377
- * Init bot. Call it manually only if you doesn't use {@link Bot.start}
378
- */
379
- init(): Promise<void>;
380
- /**
381
- * Start receive updates via long-polling or webhook
382
- *
383
- * @example
384
- * ```ts
385
- * import { Bot } from "gramio";
386
- *
387
- * const bot = new Bot("") // put you token here
388
- * .command("start", (context) => context.send("Hi!"))
389
- * .onStart(console.log);
390
- *
391
- * bot.start();
392
- * ```
393
- */
394
- start({ webhook, dropPendingUpdates, allowedUpdates, }?: {
395
- webhook?: Omit<APIMethodParams<"setWebhook">, "drop_pending_updates" | "allowed_updates">;
396
- dropPendingUpdates?: boolean;
397
- allowedUpdates?: NonNullable<APIMethodParams<"getUpdates">>["allowed_updates"];
398
- }): Promise<TelegramUser | undefined>;
399
- /**
400
- * Stops receiving events via long-polling or webhook
401
- * Currently does not implement graceful shutdown
402
- * */
403
- stop(): Promise<void>;
404
- }