gramio 0.0.16 → 0.0.17
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/dist/bot.d.ts +9 -1
- package/dist/bot.js +45 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/plugin.d.ts +4 -1
- package/dist/plugin.js +4 -1
- package/dist/types.d.ts +7 -1
- package/dist/updates.d.ts +5 -4
- package/dist/updates.js +5 -3
- package/dist/webhook/adapters.d.ts +16 -0
- package/dist/webhook/adapters.js +14 -0
- package/dist/webhook/index.d.ts +3 -0
- package/dist/webhook/index.js +12 -0
- package/package.json +2 -2
package/dist/bot.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts";
|
|
2
|
-
import type { APIMethods } from "@gramio/types";
|
|
2
|
+
import type { APIMethodParams, APIMethods, TelegramUser } from "@gramio/types";
|
|
3
3
|
import "reflect-metadata";
|
|
4
4
|
import { Plugin } from "./plugin";
|
|
5
5
|
import { BotOptions, DeriveDefinitions, ErrorDefinitions, Handler, Hooks } from "./types";
|
|
6
6
|
import { Updates } from "./updates";
|
|
7
7
|
export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDefinitions = DeriveDefinitions> {
|
|
8
8
|
readonly options: BotOptions;
|
|
9
|
+
info: TelegramUser | undefined;
|
|
9
10
|
readonly api: APIMethods;
|
|
11
|
+
private dependencies;
|
|
10
12
|
private errorsDefinitions;
|
|
11
13
|
private errorHandler;
|
|
12
14
|
updates: Updates;
|
|
@@ -66,7 +68,13 @@ export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends D
|
|
|
66
68
|
derive<Update extends UpdateName, Handler extends Hooks.Derive<ContextType<typeof this, Update>>>(updateName: MaybeArray<Update>, handler: Handler): Bot<Errors, Derives & {
|
|
67
69
|
[K in Update]: Awaited<ReturnType<Handler>>;
|
|
68
70
|
}>;
|
|
71
|
+
onStart(handler: Hooks.OnStart): this;
|
|
69
72
|
on<T extends UpdateName>(updateName: MaybeArray<T>, handler: Handler<ContextType<typeof this, T> & Derives["global"] & Derives[T]>): this;
|
|
70
73
|
use(handler: Handler<Context<typeof this> & Derives["global"]>): this;
|
|
71
74
|
extend<NewPlugin extends Plugin>(plugin: NewPlugin): Bot<Errors & NewPlugin["Errors"], Derives & NewPlugin["Derives"]>;
|
|
75
|
+
start({ webhook, dropPendingUpdates, allowedUpdates, }?: {
|
|
76
|
+
webhook?: Omit<APIMethodParams<"setWebhook">, "drop_pending_updates" | "allowed_updates">;
|
|
77
|
+
dropPendingUpdates?: boolean;
|
|
78
|
+
allowedUpdates?: NonNullable<APIMethodParams<"getUpdates">>["allowed_updates"];
|
|
79
|
+
}): Promise<TelegramUser>;
|
|
72
80
|
}
|
package/dist/bot.js
CHANGED
|
@@ -17,9 +17,11 @@ const errors_1 = require("./errors");
|
|
|
17
17
|
const updates_1 = require("./updates");
|
|
18
18
|
let Bot = class Bot {
|
|
19
19
|
options = {};
|
|
20
|
+
info;
|
|
20
21
|
api = new Proxy({}, {
|
|
21
22
|
get: (_target, method) => (args) => this._callApi(method, args),
|
|
22
23
|
});
|
|
24
|
+
dependencies = [];
|
|
23
25
|
errorsDefinitions = {
|
|
24
26
|
TELEGRAM: errors_1.TelegramError,
|
|
25
27
|
};
|
|
@@ -45,6 +47,7 @@ let Bot = class Bot {
|
|
|
45
47
|
},
|
|
46
48
|
],
|
|
47
49
|
onError: [],
|
|
50
|
+
onStart: [],
|
|
48
51
|
};
|
|
49
52
|
constructor(token, options) {
|
|
50
53
|
this.options = { ...options, token };
|
|
@@ -58,6 +61,8 @@ let Bot = class Bot {
|
|
|
58
61
|
}
|
|
59
62
|
async runImmutableHooks(type, context) {
|
|
60
63
|
for await (const hook of this.hooks[type]) {
|
|
64
|
+
//TODO: solve that later
|
|
65
|
+
//@ts-expect-error
|
|
61
66
|
await hook(context);
|
|
62
67
|
}
|
|
63
68
|
}
|
|
@@ -162,6 +167,10 @@ let Bot = class Bot {
|
|
|
162
167
|
});
|
|
163
168
|
return this;
|
|
164
169
|
}
|
|
170
|
+
onStart(handler) {
|
|
171
|
+
this.hooks.onStart.push(handler);
|
|
172
|
+
return this;
|
|
173
|
+
}
|
|
165
174
|
on(updateName, handler) {
|
|
166
175
|
this.updates.on(updateName, handler);
|
|
167
176
|
return this;
|
|
@@ -171,6 +180,10 @@ let Bot = class Bot {
|
|
|
171
180
|
return this;
|
|
172
181
|
}
|
|
173
182
|
extend(plugin) {
|
|
183
|
+
if (plugin.dependencies.some((dep) => !this.dependencies.includes(dep)))
|
|
184
|
+
throw new Error(`The «${plugin.name}» plugin needs dependencies registered before: ${plugin.dependencies
|
|
185
|
+
.filter((dep) => !this.dependencies.includes(dep))
|
|
186
|
+
.join(", ")}`);
|
|
174
187
|
for (const [key, value] of Object.entries(plugin.errorsDefinitions)) {
|
|
175
188
|
if (this.errorsDefinitions[key])
|
|
176
189
|
this.errorsDefinitions[key] = value;
|
|
@@ -182,8 +195,40 @@ let Bot = class Bot {
|
|
|
182
195
|
else
|
|
183
196
|
this.derive(updateName, derive);
|
|
184
197
|
}
|
|
198
|
+
this.dependencies.push(plugin.name);
|
|
185
199
|
return this;
|
|
186
200
|
}
|
|
201
|
+
async start({ webhook, dropPendingUpdates, allowedUpdates, } = {}) {
|
|
202
|
+
//TODO: maybe it useless??
|
|
203
|
+
this.info = await this.api.getMe();
|
|
204
|
+
if (!webhook) {
|
|
205
|
+
await this.api.deleteWebhook({
|
|
206
|
+
drop_pending_updates: dropPendingUpdates,
|
|
207
|
+
});
|
|
208
|
+
await this.updates.startPolling({
|
|
209
|
+
allowed_updates: allowedUpdates,
|
|
210
|
+
});
|
|
211
|
+
this.runImmutableHooks("onStart", {
|
|
212
|
+
plugins: this.dependencies,
|
|
213
|
+
info: this.info,
|
|
214
|
+
updatesFrom: "long-polling",
|
|
215
|
+
});
|
|
216
|
+
return this.info;
|
|
217
|
+
}
|
|
218
|
+
if (this.updates.isStarted)
|
|
219
|
+
this.updates.stopPolling();
|
|
220
|
+
await this.api.setWebhook({
|
|
221
|
+
...webhook,
|
|
222
|
+
drop_pending_updates: dropPendingUpdates,
|
|
223
|
+
allowed_updates: allowedUpdates,
|
|
224
|
+
});
|
|
225
|
+
this.runImmutableHooks("onStart", {
|
|
226
|
+
plugins: this.dependencies,
|
|
227
|
+
info: this.info,
|
|
228
|
+
updatesFrom: "long-polling",
|
|
229
|
+
});
|
|
230
|
+
return this.info;
|
|
231
|
+
}
|
|
187
232
|
};
|
|
188
233
|
exports.Bot = Bot;
|
|
189
234
|
exports.Bot = Bot = __decorate([
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -18,6 +18,7 @@ __exportStar(require("./bot"), exports);
|
|
|
18
18
|
__exportStar(require("./errors"), exports);
|
|
19
19
|
__exportStar(require("./types"), exports);
|
|
20
20
|
__exportStar(require("./plugin"), exports);
|
|
21
|
+
__exportStar(require("./webhook"), exports);
|
|
21
22
|
__exportStar(require("@gramio/contexts"), exports);
|
|
22
23
|
__exportStar(require("@gramio/files"), exports);
|
|
23
24
|
__exportStar(require("@gramio/keyboards"), exports);
|
package/dist/plugin.d.ts
CHANGED
|
@@ -9,7 +9,10 @@ export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extend
|
|
|
9
9
|
new (...args: any): any;
|
|
10
10
|
prototype: Error;
|
|
11
11
|
}>;
|
|
12
|
-
|
|
12
|
+
dependencies: string[];
|
|
13
|
+
constructor(name: string, { dependencies }?: {
|
|
14
|
+
dependencies?: string[];
|
|
15
|
+
});
|
|
13
16
|
/**
|
|
14
17
|
* Register custom class-error in plugin
|
|
15
18
|
**/
|
package/dist/plugin.js
CHANGED
|
@@ -9,8 +9,11 @@ class Plugin {
|
|
|
9
9
|
derives = [];
|
|
10
10
|
name;
|
|
11
11
|
errorsDefinitions = {};
|
|
12
|
-
|
|
12
|
+
dependencies = [];
|
|
13
|
+
constructor(name, { dependencies } = {}) {
|
|
13
14
|
this.name = name;
|
|
15
|
+
if (dependencies)
|
|
16
|
+
this.dependencies = dependencies;
|
|
14
17
|
}
|
|
15
18
|
/**
|
|
16
19
|
* Register custom class-error in plugin
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BotLike, Context, UpdateName } from "@gramio/contexts";
|
|
2
|
-
import { APIMethodParams, APIMethods } from "@gramio/types";
|
|
2
|
+
import { APIMethodParams, APIMethods, TelegramUser } from "@gramio/types";
|
|
3
3
|
import { NextMiddleware } from "middleware-io";
|
|
4
4
|
import { TelegramError } from "./errors";
|
|
5
5
|
export interface BotOptions {
|
|
@@ -29,9 +29,15 @@ export declare namespace Hooks {
|
|
|
29
29
|
[K in keyof T]: ErrorHandlerParams<Ctx, K & string, T[K & string]>;
|
|
30
30
|
}[keyof T];
|
|
31
31
|
type OnError<T extends ErrorDefinitions, Ctx extends Context<BotLike> = Context<BotLike>> = (options: OnErrorContext<Ctx, T>) => unknown;
|
|
32
|
+
type OnStart = (context: {
|
|
33
|
+
plugins: string[];
|
|
34
|
+
info: TelegramUser;
|
|
35
|
+
updatesFrom: "webhook" | "long-polling";
|
|
36
|
+
}) => unknown;
|
|
32
37
|
interface Store<T extends ErrorDefinitions> {
|
|
33
38
|
preRequest: PreRequest[];
|
|
34
39
|
onError: OnError<T>[];
|
|
40
|
+
onStart: OnStart[];
|
|
35
41
|
}
|
|
36
42
|
}
|
|
37
43
|
export type ErrorDefinitions = Record<string, Error>;
|
package/dist/updates.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Context, MaybeArray, UpdateName } from "@gramio/contexts";
|
|
2
|
-
import type { TelegramUpdate } from "@gramio/types";
|
|
2
|
+
import type { APIMethodParams, TelegramUpdate } from "@gramio/types";
|
|
3
3
|
import { CaughtMiddlewareHandler } from "middleware-io";
|
|
4
4
|
import type { Bot } from "./bot";
|
|
5
5
|
import { Handler } from "./types";
|
|
6
6
|
export declare class Updates {
|
|
7
7
|
private readonly bot;
|
|
8
|
-
|
|
8
|
+
isStarted: boolean;
|
|
9
9
|
private offset;
|
|
10
10
|
private composer;
|
|
11
11
|
private onError;
|
|
@@ -13,7 +13,8 @@ export declare class Updates {
|
|
|
13
13
|
on<T extends UpdateName>(updateName: MaybeArray<T>, handler: Handler<any>): this;
|
|
14
14
|
use(handler: Handler<any>): this;
|
|
15
15
|
handleUpdate(data: TelegramUpdate): Promise<void>;
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
/**@deprecated use bot.start instead */
|
|
17
|
+
startPolling(params?: APIMethodParams<"getUpdates">): Promise<void>;
|
|
18
|
+
startFetchLoop(params?: APIMethodParams<"getUpdates">): Promise<void>;
|
|
18
19
|
stopPolling(): void;
|
|
19
20
|
}
|
package/dist/updates.js
CHANGED
|
@@ -63,17 +63,19 @@ class Updates {
|
|
|
63
63
|
throw new Error(`[UPDATES] Update type ${updateType} not supported.`);
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
|
-
|
|
66
|
+
/**@deprecated use bot.start instead */
|
|
67
|
+
async startPolling(params = {}) {
|
|
67
68
|
if (this.isStarted)
|
|
68
69
|
throw new Error("[UPDATES] Polling already started!");
|
|
69
70
|
this.isStarted = true;
|
|
70
|
-
this.startFetchLoop();
|
|
71
|
+
this.startFetchLoop(params);
|
|
71
72
|
return;
|
|
72
73
|
}
|
|
73
|
-
async startFetchLoop() {
|
|
74
|
+
async startFetchLoop(params = {}) {
|
|
74
75
|
while (this.isStarted) {
|
|
75
76
|
const updates = await this.bot.api.getUpdates({
|
|
76
77
|
offset: this.offset,
|
|
78
|
+
...params,
|
|
77
79
|
});
|
|
78
80
|
for await (const update of updates) {
|
|
79
81
|
//TODO: update errors
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { TelegramUpdate } from "@gramio/types";
|
|
2
|
+
export interface FrameworkHandler {
|
|
3
|
+
update: TelegramUpdate;
|
|
4
|
+
header?: string;
|
|
5
|
+
}
|
|
6
|
+
export type FrameworkAdapter = (...args: any[]) => FrameworkHandler;
|
|
7
|
+
export declare const frameworks: {
|
|
8
|
+
elysia: ({ body, headers }: any) => {
|
|
9
|
+
update: any;
|
|
10
|
+
header: any;
|
|
11
|
+
};
|
|
12
|
+
fastify: (request: any) => {
|
|
13
|
+
update: any;
|
|
14
|
+
header: any;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.frameworks = void 0;
|
|
4
|
+
const SECRET_TOKEN_HEADER = "X-Telegram-Bot-Api-Secret-Token";
|
|
5
|
+
exports.frameworks = {
|
|
6
|
+
elysia: ({ body, headers }) => ({
|
|
7
|
+
update: body,
|
|
8
|
+
header: headers[SECRET_TOKEN_HEADER],
|
|
9
|
+
}),
|
|
10
|
+
fastify: (request) => ({
|
|
11
|
+
update: request.body,
|
|
12
|
+
header: request.headers[SECRET_TOKEN_HEADER],
|
|
13
|
+
}),
|
|
14
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.webhookHandler = void 0;
|
|
4
|
+
const adapters_1 = require("./adapters");
|
|
5
|
+
function webhookHandler(bot, framework) {
|
|
6
|
+
const frameworkAdapter = adapters_1.frameworks[framework];
|
|
7
|
+
return async (...args) => {
|
|
8
|
+
const { update } = frameworkAdapter(...args);
|
|
9
|
+
await bot.updates.handleUpdate(update);
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
exports.webhookHandler = webhookHandler;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gramio",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.17",
|
|
4
4
|
"description": "Powerful Telegram Bot API framework",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"license": "ISC",
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@biomejs/biome": "1.5.3",
|
|
28
|
-
"@gramio/types": "^7.1.
|
|
28
|
+
"@gramio/types": "^7.1.4",
|
|
29
29
|
"@types/node": "^20.11.20",
|
|
30
30
|
"typescript": "^5.3.3"
|
|
31
31
|
},
|