gramio 0.0.18 → 0.0.19
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 +3 -2
- package/dist/bot.js +11 -8
- package/dist/webhook/adapters.d.ts +10 -1
- package/dist/webhook/adapters.js +8 -0
- package/dist/webhook/index.js +1 -1
- package/package.json +42 -43
package/dist/bot.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts";
|
|
2
2
|
import type { APIMethodParams, APIMethods, TelegramUser } from "@gramio/types";
|
|
3
3
|
import { Plugin } from "./plugin";
|
|
4
|
-
import { BotOptions, DeriveDefinitions, ErrorDefinitions, Handler, Hooks } from "./types";
|
|
4
|
+
import { BotOptions, DeriveDefinitions, ErrorDefinitions, Handler, Hooks, MaybePromise } from "./types";
|
|
5
5
|
import { Updates } from "./updates";
|
|
6
6
|
export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDefinitions = DeriveDefinitions> {
|
|
7
7
|
__Derives: Derives;
|
|
8
8
|
readonly options: BotOptions;
|
|
9
9
|
info: TelegramUser | undefined;
|
|
10
10
|
readonly api: APIMethods;
|
|
11
|
+
private lazyloadPlugins;
|
|
11
12
|
private dependencies;
|
|
12
13
|
private errorsDefinitions;
|
|
13
14
|
private errorHandler;
|
|
@@ -71,7 +72,7 @@ export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends D
|
|
|
71
72
|
onStart(handler: Hooks.OnStart): this;
|
|
72
73
|
on<T extends UpdateName>(updateName: MaybeArray<T>, handler: Handler<ContextType<typeof this, T> & Derives["global"] & Derives[T]>): this;
|
|
73
74
|
use(handler: Handler<Context<typeof this> & Derives["global"]>): this;
|
|
74
|
-
extend<NewPlugin extends Plugin>(plugin: NewPlugin): Bot<Errors & NewPlugin["Errors"], Derives & NewPlugin["Derives"]>;
|
|
75
|
+
extend<NewPlugin extends Plugin>(plugin: MaybePromise<NewPlugin>): Bot<Errors & NewPlugin["Errors"], Derives & NewPlugin["Derives"]>;
|
|
75
76
|
start({ webhook, dropPendingUpdates, allowedUpdates, }?: {
|
|
76
77
|
webhook?: Omit<APIMethodParams<"setWebhook">, "drop_pending_updates" | "allowed_updates">;
|
|
77
78
|
dropPendingUpdates?: boolean;
|
package/dist/bot.js
CHANGED
|
@@ -37,7 +37,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
37
37
|
exports.Bot = void 0;
|
|
38
38
|
const files_1 = require("@gramio/files");
|
|
39
39
|
const format_1 = require("@gramio/format");
|
|
40
|
-
const form_data_encoder_1 = require("form-data-encoder");
|
|
41
40
|
const inspectable_1 = require("inspectable");
|
|
42
41
|
const undici_1 = require("undici");
|
|
43
42
|
const errors_1 = require("./errors");
|
|
@@ -64,6 +63,7 @@ let Bot = (() => {
|
|
|
64
63
|
api = new Proxy({}, {
|
|
65
64
|
get: (_target, method) => (args) => this._callApi(method, args),
|
|
66
65
|
});
|
|
66
|
+
lazyloadPlugins = [];
|
|
67
67
|
dependencies = [];
|
|
68
68
|
errorsDefinitions = {
|
|
69
69
|
TELEGRAM: errors_1.TelegramError,
|
|
@@ -93,6 +93,8 @@ let Bot = (() => {
|
|
|
93
93
|
onStart: [],
|
|
94
94
|
};
|
|
95
95
|
constructor(token, options) {
|
|
96
|
+
if (!token || typeof token !== "string")
|
|
97
|
+
throw new Error(`Token is ${typeof token} but it should be a string!`);
|
|
96
98
|
this.options = { ...options, token };
|
|
97
99
|
}
|
|
98
100
|
async runHooks(type, context) {
|
|
@@ -113,7 +115,6 @@ let Bot = (() => {
|
|
|
113
115
|
const url = `https://api.telegram.org/bot${this.options.token}/${method}`;
|
|
114
116
|
const reqOptions = {
|
|
115
117
|
method: "POST",
|
|
116
|
-
duplex: "half",
|
|
117
118
|
};
|
|
118
119
|
const context = await this.runHooks("preRequest",
|
|
119
120
|
// TODO: fix type error
|
|
@@ -126,9 +127,7 @@ let Bot = (() => {
|
|
|
126
127
|
params = context.params;
|
|
127
128
|
if (params && (0, files_1.isMediaUpload)(method, params)) {
|
|
128
129
|
const formData = await (0, files_1.convertJsonToFormData)(method, params);
|
|
129
|
-
|
|
130
|
-
reqOptions.body = encoder.encode();
|
|
131
|
-
reqOptions.headers = encoder.headers;
|
|
130
|
+
reqOptions.body = formData;
|
|
132
131
|
}
|
|
133
132
|
else {
|
|
134
133
|
reqOptions.headers = {
|
|
@@ -136,8 +135,8 @@ let Bot = (() => {
|
|
|
136
135
|
};
|
|
137
136
|
reqOptions.body = JSON.stringify(params);
|
|
138
137
|
}
|
|
139
|
-
const response = await (0, undici_1.
|
|
140
|
-
const data = (await response.json());
|
|
138
|
+
const response = await (0, undici_1.request)(url, reqOptions);
|
|
139
|
+
const data = (await response.body.json());
|
|
141
140
|
if (!data.ok)
|
|
142
141
|
throw new errors_1.TelegramError(data, method, params);
|
|
143
142
|
return data.result;
|
|
@@ -223,6 +222,10 @@ let Bot = (() => {
|
|
|
223
222
|
return this;
|
|
224
223
|
}
|
|
225
224
|
extend(plugin) {
|
|
225
|
+
if (plugin instanceof Promise) {
|
|
226
|
+
this.lazyloadPlugins.push(plugin);
|
|
227
|
+
return this;
|
|
228
|
+
}
|
|
226
229
|
if (plugin.dependencies.some((dep) => !this.dependencies.includes(dep)))
|
|
227
230
|
throw new Error(`The «${plugin.name}» plugin needs dependencies registered before: ${plugin.dependencies
|
|
228
231
|
.filter((dep) => !this.dependencies.includes(dep))
|
|
@@ -242,7 +245,7 @@ let Bot = (() => {
|
|
|
242
245
|
return this;
|
|
243
246
|
}
|
|
244
247
|
async start({ webhook, dropPendingUpdates, allowedUpdates, } = {}) {
|
|
245
|
-
|
|
248
|
+
await Promise.all(this.lazyloadPlugins.map(async (plugin) => this.extend(await plugin)));
|
|
246
249
|
this.info = await this.api.getMe();
|
|
247
250
|
if (!webhook) {
|
|
248
251
|
await this.api.deleteWebhook({
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { TelegramUpdate } from "@gramio/types";
|
|
2
|
+
import { MaybePromise } from "../types";
|
|
2
3
|
export interface FrameworkHandler {
|
|
3
|
-
update: TelegramUpdate
|
|
4
|
+
update: MaybePromise<TelegramUpdate>;
|
|
4
5
|
header?: string;
|
|
5
6
|
}
|
|
6
7
|
export type FrameworkAdapter = (...args: any[]) => FrameworkHandler;
|
|
@@ -13,4 +14,12 @@ export declare const frameworks: {
|
|
|
13
14
|
update: any;
|
|
14
15
|
header: any;
|
|
15
16
|
};
|
|
17
|
+
hono: (c: any) => {
|
|
18
|
+
update: any;
|
|
19
|
+
header: any;
|
|
20
|
+
};
|
|
21
|
+
express: (req: any) => {
|
|
22
|
+
update: any;
|
|
23
|
+
header: any;
|
|
24
|
+
};
|
|
16
25
|
};
|
package/dist/webhook/adapters.js
CHANGED
|
@@ -11,4 +11,12 @@ exports.frameworks = {
|
|
|
11
11
|
update: request.body,
|
|
12
12
|
header: request.headers[SECRET_TOKEN_HEADER],
|
|
13
13
|
}),
|
|
14
|
+
hono: (c) => ({
|
|
15
|
+
update: c.req.json(),
|
|
16
|
+
header: c.req.header(SECRET_TOKEN_HEADER)
|
|
17
|
+
}),
|
|
18
|
+
express: (req) => ({
|
|
19
|
+
update: req.body,
|
|
20
|
+
header: req.header(SECRET_TOKEN_HEADER)
|
|
21
|
+
})
|
|
14
22
|
};
|
package/dist/webhook/index.js
CHANGED
|
@@ -6,7 +6,7 @@ function webhookHandler(bot, framework) {
|
|
|
6
6
|
const frameworkAdapter = adapters_1.frameworks[framework];
|
|
7
7
|
return async (...args) => {
|
|
8
8
|
const { update } = frameworkAdapter(...args);
|
|
9
|
-
await bot.updates.handleUpdate(update);
|
|
9
|
+
await bot.updates.handleUpdate(await update);
|
|
10
10
|
};
|
|
11
11
|
}
|
|
12
12
|
exports.webhookHandler = webhookHandler;
|
package/package.json
CHANGED
|
@@ -1,45 +1,44 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
"
|
|
43
|
-
|
|
44
|
-
]
|
|
2
|
+
"name": "gramio",
|
|
3
|
+
"version": "0.0.19",
|
|
4
|
+
"description": "Powerful Telegram Bot API framework",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"telegram",
|
|
9
|
+
"telegram-bot",
|
|
10
|
+
"telegram-bot-api",
|
|
11
|
+
"bot",
|
|
12
|
+
"framework",
|
|
13
|
+
"types",
|
|
14
|
+
"client",
|
|
15
|
+
"webhook",
|
|
16
|
+
"long-polling"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"type": "tsc --noEmit",
|
|
20
|
+
"lint": "bun check ./src",
|
|
21
|
+
"lint:fix": "bun lint --apply",
|
|
22
|
+
"prepublishOnly": "tsc && bunx tsc-alias"
|
|
23
|
+
},
|
|
24
|
+
"author": "kravets",
|
|
25
|
+
"license": "ISC",
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@biomejs/biome": "1.5.3",
|
|
28
|
+
"@gramio/types": "^7.1.6",
|
|
29
|
+
"@types/node": "^20.11.24",
|
|
30
|
+
"typescript": "^5.3.3"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@gramio/contexts": "^0.0.7",
|
|
34
|
+
"@gramio/files": "^0.0.3",
|
|
35
|
+
"@gramio/format": "^0.0.8",
|
|
36
|
+
"@gramio/keyboards": "^0.2.0",
|
|
37
|
+
"inspectable": "^3.0.0",
|
|
38
|
+
"middleware-io": "^2.8.1",
|
|
39
|
+
"undici": "^6.6.2"
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"dist"
|
|
43
|
+
]
|
|
45
44
|
}
|