gramio 0.0.8 → 0.0.10
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/README.md +6 -0
- package/dist/TelegramError.d.ts +8 -0
- package/dist/{apiErrors.js → TelegramError.js} +4 -6
- package/dist/bot.d.ts +3 -3
- package/dist/bot.js +5 -5
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/types.d.ts +14 -3
- package/dist/updates.d.ts +7 -5
- package/dist/updates.js +44 -14
- package/package.json +7 -6
- package/dist/apiErrors.d.ts +0 -12
package/README.md
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { APIMethodParams, APIMethods, TelegramAPIResponseError, TelegramResponseParameters } from "@gramio/types";
|
|
2
|
+
export declare class TelegramError<T extends keyof APIMethods> extends Error {
|
|
3
|
+
method: T;
|
|
4
|
+
params: APIMethodParams<T>;
|
|
5
|
+
code: number;
|
|
6
|
+
payload?: TelegramResponseParameters;
|
|
7
|
+
constructor(error: TelegramAPIResponseError, method: T, params: APIMethodParams<T>);
|
|
8
|
+
}
|
|
@@ -1,21 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
5
|
-
class APIError extends Error {
|
|
3
|
+
exports.TelegramError = void 0;
|
|
4
|
+
class TelegramError extends Error {
|
|
6
5
|
method;
|
|
7
6
|
params;
|
|
8
7
|
code;
|
|
9
8
|
payload;
|
|
10
|
-
constructor(
|
|
9
|
+
constructor(error, method, params) {
|
|
11
10
|
super(error.description);
|
|
12
11
|
this.name = method;
|
|
13
12
|
this.method = method;
|
|
14
13
|
this.params = params;
|
|
15
14
|
this.code = error.error_code;
|
|
16
|
-
//TODO: delete when undefined
|
|
17
15
|
if (error.parameters)
|
|
18
16
|
this.payload = error.parameters;
|
|
19
17
|
}
|
|
20
18
|
}
|
|
21
|
-
exports.
|
|
19
|
+
exports.TelegramError = TelegramError;
|
package/dist/bot.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { APIMethods } from "@gramio/types";
|
|
2
2
|
import "reflect-metadata";
|
|
3
3
|
import { BotOptions } from "./types";
|
|
4
4
|
import { Updates } from "./updates";
|
|
5
5
|
export declare class Bot {
|
|
6
6
|
readonly options: BotOptions;
|
|
7
|
-
readonly api:
|
|
7
|
+
readonly api: APIMethods;
|
|
8
8
|
updates: Updates;
|
|
9
|
-
private _callApi;
|
|
10
9
|
constructor(token: string, options?: Omit<BotOptions, "token">);
|
|
10
|
+
private _callApi;
|
|
11
11
|
}
|
package/dist/bot.js
CHANGED
|
@@ -13,7 +13,7 @@ const form_data_encoder_1 = require("form-data-encoder");
|
|
|
13
13
|
const inspectable_1 = require("inspectable");
|
|
14
14
|
require("reflect-metadata");
|
|
15
15
|
const undici_1 = require("undici");
|
|
16
|
-
const
|
|
16
|
+
const TelegramError_1 = require("./TelegramError");
|
|
17
17
|
const updates_1 = require("./updates");
|
|
18
18
|
let Bot = class Bot {
|
|
19
19
|
options = {};
|
|
@@ -21,6 +21,9 @@ let Bot = class Bot {
|
|
|
21
21
|
get: (_target, method) => (args) => this._callApi(method, args),
|
|
22
22
|
});
|
|
23
23
|
updates = new updates_1.Updates(this);
|
|
24
|
+
constructor(token, options) {
|
|
25
|
+
this.options = { ...options, token };
|
|
26
|
+
}
|
|
24
27
|
async _callApi(method, params = {}) {
|
|
25
28
|
const url = `https://api.telegram.org/bot${this.options.token}/${method}`;
|
|
26
29
|
const reqOptions = {
|
|
@@ -46,12 +49,9 @@ let Bot = class Bot {
|
|
|
46
49
|
const response = await (0, undici_1.fetch)(url, reqOptions);
|
|
47
50
|
const data = (await response.json());
|
|
48
51
|
if (!data.ok)
|
|
49
|
-
throw new
|
|
52
|
+
throw new TelegramError_1.TelegramError(data, method, params);
|
|
50
53
|
return data.result;
|
|
51
54
|
}
|
|
52
|
-
constructor(token, options) {
|
|
53
|
-
this.options = { ...options, token };
|
|
54
|
-
}
|
|
55
55
|
};
|
|
56
56
|
exports.Bot = Bot;
|
|
57
57
|
exports.Bot = Bot = __decorate([
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -15,7 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./bot"), exports);
|
|
18
|
-
__exportStar(require("./
|
|
18
|
+
__exportStar(require("./TelegramError"), exports);
|
|
19
19
|
__exportStar(require("./types"), exports);
|
|
20
20
|
__exportStar(require("@gramio/contexts"), exports);
|
|
21
21
|
__exportStar(require("@gramio/files"), exports);
|
package/dist/types.d.ts
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Context } from "@gramio/contexts";
|
|
2
|
+
import { APIMethods } from "@gramio/types";
|
|
2
3
|
import { NextMiddleware } from "middleware-io";
|
|
4
|
+
import { TelegramError } from "./TelegramError";
|
|
3
5
|
export interface BotOptions {
|
|
4
6
|
token?: string;
|
|
5
7
|
}
|
|
6
|
-
export type
|
|
7
|
-
|
|
8
|
+
export type Handler<T> = (context: T, next: NextMiddleware) => unknown;
|
|
9
|
+
interface ErrorHandlerParams<Kind extends string, Err extends Error> {
|
|
10
|
+
context: Context;
|
|
11
|
+
kind: Kind;
|
|
12
|
+
error: Err;
|
|
13
|
+
}
|
|
14
|
+
type AnyTelegramError = {
|
|
15
|
+
[APIMethod in keyof APIMethods]: TelegramError<APIMethod>;
|
|
16
|
+
}[keyof APIMethods];
|
|
17
|
+
export type ErrorHandler = (options: ErrorHandlerParams<"TELEGRAM", AnyTelegramError> | ErrorHandlerParams<"UNKNOWN", Error>) => unknown;
|
|
18
|
+
export {};
|
package/dist/updates.d.ts
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
import { Context, contextsMappings } from "@gramio/contexts";
|
|
1
|
+
import { Context, UpdateName, contextsMappings } from "@gramio/contexts";
|
|
2
2
|
import type { TelegramUpdate } from "@gramio/types";
|
|
3
3
|
import type { Bot } from "./bot";
|
|
4
|
-
import {
|
|
4
|
+
import { ErrorHandler, Handler } from "./types";
|
|
5
5
|
export declare class Updates {
|
|
6
6
|
private readonly bot;
|
|
7
7
|
private isStarted;
|
|
8
8
|
private offset;
|
|
9
9
|
private composer;
|
|
10
|
+
private errorHandler;
|
|
10
11
|
constructor(bot: Bot);
|
|
11
|
-
on<T extends
|
|
12
|
-
use(handler:
|
|
12
|
+
on<T extends UpdateName>(updateName: T, handler: Handler<InstanceType<(typeof contextsMappings)[T]>>): this;
|
|
13
|
+
use(handler: Handler<Context>): this;
|
|
13
14
|
handleUpdate(data: TelegramUpdate): Promise<void>;
|
|
14
|
-
startPolling(): Promise<
|
|
15
|
+
startPolling(): Promise<void>;
|
|
15
16
|
startFetchLoop(): Promise<void>;
|
|
16
17
|
stopPolling(): void;
|
|
18
|
+
onError(handler: ErrorHandler): void;
|
|
17
19
|
}
|
package/dist/updates.js
CHANGED
|
@@ -3,46 +3,73 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Updates = void 0;
|
|
4
4
|
const contexts_1 = require("@gramio/contexts");
|
|
5
5
|
const middleware_io_1 = require("middleware-io");
|
|
6
|
+
const _TelegramError_1 = require("#TelegramError");
|
|
6
7
|
class Updates {
|
|
7
8
|
bot;
|
|
8
9
|
isStarted = false;
|
|
9
10
|
offset = 0;
|
|
10
11
|
composer = middleware_io_1.Composer.builder();
|
|
12
|
+
errorHandler;
|
|
11
13
|
constructor(bot) {
|
|
12
14
|
this.bot = bot;
|
|
13
15
|
}
|
|
14
16
|
on(updateName, handler) {
|
|
15
|
-
return this.use((context, next) => {
|
|
17
|
+
return this.use(async (context, next) => {
|
|
16
18
|
//TODO: fix typings
|
|
17
19
|
if (context.is(updateName))
|
|
18
|
-
handler(context, next);
|
|
20
|
+
await handler(context, next);
|
|
19
21
|
else
|
|
20
|
-
next();
|
|
22
|
+
await next();
|
|
21
23
|
});
|
|
22
24
|
}
|
|
23
25
|
use(handler) {
|
|
24
|
-
this.composer
|
|
26
|
+
this.composer
|
|
27
|
+
.caught((ctx, error) => {
|
|
28
|
+
if (error instanceof _TelegramError_1.TelegramError)
|
|
29
|
+
return this.errorHandler?.({
|
|
30
|
+
context: ctx,
|
|
31
|
+
kind: "TELEGRAM",
|
|
32
|
+
error: error,
|
|
33
|
+
});
|
|
34
|
+
this.errorHandler?.({
|
|
35
|
+
context: ctx,
|
|
36
|
+
kind: "UNKNOWN",
|
|
37
|
+
error: error,
|
|
38
|
+
});
|
|
39
|
+
})
|
|
40
|
+
.use(handler);
|
|
25
41
|
return this;
|
|
26
42
|
}
|
|
27
43
|
async handleUpdate(data) {
|
|
28
44
|
const updateType = Object.keys(data).at(1);
|
|
29
45
|
this.offset = data.update_id + 1;
|
|
46
|
+
const UpdateContext = contexts_1.contextsMappings[updateType];
|
|
47
|
+
if (!UpdateContext)
|
|
48
|
+
return;
|
|
30
49
|
try {
|
|
31
|
-
|
|
32
|
-
|
|
50
|
+
let context = new UpdateContext({
|
|
51
|
+
//@ts-expect-error
|
|
33
52
|
bot: this.bot,
|
|
34
53
|
update: data,
|
|
35
|
-
|
|
36
|
-
//@ts-ignore
|
|
54
|
+
//@ts-expect-error
|
|
37
55
|
payload: data[updateType],
|
|
38
56
|
type: updateType,
|
|
39
57
|
updateId: data.update_id,
|
|
40
|
-
// raw: {
|
|
41
|
-
// update: data,
|
|
42
|
-
// updateId: data.update_id,
|
|
43
|
-
// updateType,
|
|
44
|
-
// },
|
|
45
58
|
});
|
|
59
|
+
if ("isEvent" in context && context.isEvent() && context.eventType) {
|
|
60
|
+
context = new contexts_1.contextsMappings[context.eventType]({
|
|
61
|
+
//@ts-expect-error
|
|
62
|
+
bot: this.bot,
|
|
63
|
+
update: data,
|
|
64
|
+
//@ts-expect-error
|
|
65
|
+
payload: data.message ??
|
|
66
|
+
data.edited_message ??
|
|
67
|
+
data.channel_post ??
|
|
68
|
+
data.edited_channel_post,
|
|
69
|
+
type: context.eventType,
|
|
70
|
+
updateId: data.update_id,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
46
73
|
this.composer.compose()(
|
|
47
74
|
//TODO: fix typings
|
|
48
75
|
context, middleware_io_1.noopNext);
|
|
@@ -56,7 +83,7 @@ class Updates {
|
|
|
56
83
|
throw new Error("[UPDATES] Polling already started!");
|
|
57
84
|
this.isStarted = true;
|
|
58
85
|
this.startFetchLoop();
|
|
59
|
-
return
|
|
86
|
+
return;
|
|
60
87
|
}
|
|
61
88
|
async startFetchLoop() {
|
|
62
89
|
while (this.isStarted) {
|
|
@@ -72,5 +99,8 @@ class Updates {
|
|
|
72
99
|
stopPolling() {
|
|
73
100
|
this.isStarted = false;
|
|
74
101
|
}
|
|
102
|
+
onError(handler) {
|
|
103
|
+
this.errorHandler = handler;
|
|
104
|
+
}
|
|
75
105
|
}
|
|
76
106
|
exports.Updates = Updates;
|
package/package.json
CHANGED
|
@@ -1,26 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gramio",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"description": "WIP",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
|
+
"type": "tsc --noEmit",
|
|
8
9
|
"lint": "bun check ./src",
|
|
9
|
-
"lint:fix": "bun lint --apply"
|
|
10
|
+
"lint:fix": "bun lint --apply",
|
|
11
|
+
"prepublishOnly": "tsc"
|
|
10
12
|
},
|
|
11
13
|
"author": "kravets",
|
|
12
14
|
"license": "ISC",
|
|
13
15
|
"devDependencies": {
|
|
14
16
|
"@biomejs/biome": "1.5.3",
|
|
15
|
-
"@gramio/types": "^7.
|
|
16
|
-
"@types/node": "^20.11.
|
|
17
|
+
"@gramio/types": "^7.1.1",
|
|
18
|
+
"@types/node": "^20.11.19"
|
|
17
19
|
},
|
|
18
20
|
"dependencies": {
|
|
19
|
-
"@gramio/contexts": "^0.0.
|
|
21
|
+
"@gramio/contexts": "^0.0.2",
|
|
20
22
|
"@gramio/files": "^0.0.3",
|
|
21
23
|
"@gramio/format": "^0.0.7",
|
|
22
24
|
"@gramio/keyboards": "^0.1.6",
|
|
23
|
-
"common-tags": "^1.8.2",
|
|
24
25
|
"form-data-encoder": "^4.0.2",
|
|
25
26
|
"inspectable": "^2.1.0",
|
|
26
27
|
"middleware-io": "^2.8.1",
|
package/dist/apiErrors.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { TelegramAPIResponseError } from "@gramio/types";
|
|
2
|
-
export interface APIErrorDetails {
|
|
3
|
-
method: string;
|
|
4
|
-
params: Record<string, any>;
|
|
5
|
-
}
|
|
6
|
-
export declare class APIError extends Error {
|
|
7
|
-
method: string;
|
|
8
|
-
params: Record<string, any>;
|
|
9
|
-
code: number;
|
|
10
|
-
payload?: TelegramAPIResponseError["parameters"];
|
|
11
|
-
constructor({ method, params }: APIErrorDetails, error: TelegramAPIResponseError);
|
|
12
|
-
}
|