gramio 0.4.4 → 0.4.5
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/index.cjs +48 -18
- package/dist/index.d.cts +24 -2
- package/dist/index.d.ts +24 -2
- package/dist/index.js +48 -18
- package/dist/{utils-CJlBu70R.js → utils-CJfJNxc_.js} +15 -1
- package/dist/{utils-CnG66NLH.cjs → utils-CYIya32k.cjs} +15 -0
- package/dist/utils.cjs +1 -1
- package/dist/utils.js +1 -1
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -7,7 +7,7 @@ var contexts = require('@gramio/contexts');
|
|
|
7
7
|
var files = require('@gramio/files');
|
|
8
8
|
var format = require('@gramio/format');
|
|
9
9
|
var debug = require('debug');
|
|
10
|
-
var utils = require('./utils-
|
|
10
|
+
var utils = require('./utils-CYIya32k.cjs');
|
|
11
11
|
var middlewareIo = require('middleware-io');
|
|
12
12
|
var keyboards = require('@gramio/keyboards');
|
|
13
13
|
|
|
@@ -1125,8 +1125,9 @@ class Bot {
|
|
|
1125
1125
|
|
|
1126
1126
|
const SECRET_TOKEN_HEADER = "X-Telegram-Bot-Api-Secret-Token";
|
|
1127
1127
|
const WRONG_TOKEN_ERROR = "secret token is invalid";
|
|
1128
|
-
const
|
|
1129
|
-
const
|
|
1128
|
+
const RESPONSE_OK = "ok!";
|
|
1129
|
+
const responseOK = () => new Response(RESPONSE_OK);
|
|
1130
|
+
const responseUnauthorized = () => new Response(WRONG_TOKEN_ERROR, {
|
|
1130
1131
|
status: 401
|
|
1131
1132
|
// @ts-ignore
|
|
1132
1133
|
});
|
|
@@ -1134,22 +1135,26 @@ const frameworks = {
|
|
|
1134
1135
|
elysia: ({ body, headers }) => ({
|
|
1135
1136
|
update: body,
|
|
1136
1137
|
header: headers[SECRET_TOKEN_HEADER],
|
|
1137
|
-
unauthorized:
|
|
1138
|
+
unauthorized: responseUnauthorized,
|
|
1139
|
+
response: responseOK
|
|
1138
1140
|
}),
|
|
1139
1141
|
fastify: (request, reply) => ({
|
|
1140
1142
|
update: request.body,
|
|
1141
1143
|
header: request.headers[SECRET_TOKEN_HEADER],
|
|
1142
|
-
unauthorized: () => reply.code(401).send(WRONG_TOKEN_ERROR)
|
|
1144
|
+
unauthorized: () => reply.code(401).send(WRONG_TOKEN_ERROR),
|
|
1145
|
+
response: () => reply.send(RESPONSE_OK)
|
|
1143
1146
|
}),
|
|
1144
1147
|
hono: (c) => ({
|
|
1145
1148
|
update: c.req.json(),
|
|
1146
1149
|
header: c.req.header(SECRET_TOKEN_HEADER),
|
|
1147
|
-
unauthorized: () => c.text(WRONG_TOKEN_ERROR, 401)
|
|
1150
|
+
unauthorized: () => c.text(WRONG_TOKEN_ERROR, 401),
|
|
1151
|
+
response: responseOK
|
|
1148
1152
|
}),
|
|
1149
1153
|
express: (req, res) => ({
|
|
1150
1154
|
update: req.body,
|
|
1151
1155
|
header: req.header(SECRET_TOKEN_HEADER),
|
|
1152
|
-
unauthorized: () => res.status(401).send(WRONG_TOKEN_ERROR)
|
|
1156
|
+
unauthorized: () => res.status(401).send(WRONG_TOKEN_ERROR),
|
|
1157
|
+
response: () => res.send(RESPONSE_OK)
|
|
1153
1158
|
}),
|
|
1154
1159
|
koa: (ctx) => ({
|
|
1155
1160
|
update: ctx.request.body,
|
|
@@ -1157,6 +1162,10 @@ const frameworks = {
|
|
|
1157
1162
|
unauthorized: () => {
|
|
1158
1163
|
ctx.status === 401;
|
|
1159
1164
|
ctx.body = WRONG_TOKEN_ERROR;
|
|
1165
|
+
},
|
|
1166
|
+
response: () => {
|
|
1167
|
+
ctx.status = 200;
|
|
1168
|
+
ctx.body = RESPONSE_OK;
|
|
1160
1169
|
}
|
|
1161
1170
|
}),
|
|
1162
1171
|
http: (req, res) => ({
|
|
@@ -1168,38 +1177,59 @@ const frameworks = {
|
|
|
1168
1177
|
req.on("end", () => resolve(JSON.parse(body)));
|
|
1169
1178
|
}),
|
|
1170
1179
|
header: req.headers[SECRET_TOKEN_HEADER.toLowerCase()],
|
|
1171
|
-
unauthorized: () => res.writeHead(401).end(WRONG_TOKEN_ERROR)
|
|
1180
|
+
unauthorized: () => res.writeHead(401).end(WRONG_TOKEN_ERROR),
|
|
1181
|
+
response: () => res.writeHead(200).end(RESPONSE_OK)
|
|
1172
1182
|
}),
|
|
1173
|
-
// TODO: Maybe unify it by 'WebRequest'
|
|
1174
1183
|
"std/http": (req) => ({
|
|
1175
1184
|
update: req.json(),
|
|
1176
1185
|
header: req.headers.get(SECRET_TOKEN_HEADER),
|
|
1177
|
-
response:
|
|
1178
|
-
unauthorized:
|
|
1186
|
+
response: responseOK,
|
|
1187
|
+
unauthorized: responseUnauthorized
|
|
1179
1188
|
}),
|
|
1180
1189
|
"Bun.serve": (req) => ({
|
|
1181
1190
|
update: req.json(),
|
|
1182
1191
|
header: req.headers.get(SECRET_TOKEN_HEADER),
|
|
1183
|
-
response:
|
|
1184
|
-
unauthorized:
|
|
1192
|
+
response: responseOK,
|
|
1193
|
+
unauthorized: responseUnauthorized
|
|
1194
|
+
}),
|
|
1195
|
+
cloudflare: (req) => ({
|
|
1196
|
+
update: req.json(),
|
|
1197
|
+
header: req.headers.get(SECRET_TOKEN_HEADER),
|
|
1198
|
+
response: responseOK,
|
|
1199
|
+
unauthorized: responseUnauthorized
|
|
1185
1200
|
}),
|
|
1186
1201
|
Request: (req) => ({
|
|
1187
1202
|
update: req.json(),
|
|
1188
1203
|
header: req.headers.get(SECRET_TOKEN_HEADER),
|
|
1189
|
-
response:
|
|
1190
|
-
unauthorized:
|
|
1204
|
+
response: responseOK,
|
|
1205
|
+
unauthorized: responseUnauthorized
|
|
1191
1206
|
})
|
|
1192
1207
|
};
|
|
1193
1208
|
|
|
1194
|
-
function webhookHandler(bot, framework,
|
|
1209
|
+
function webhookHandler(bot, framework, secretTokenOrOptions) {
|
|
1195
1210
|
const frameworkAdapter = frameworks[framework];
|
|
1211
|
+
const secretToken = typeof secretTokenOrOptions === "string" ? secretTokenOrOptions : secretTokenOrOptions?.secretToken;
|
|
1212
|
+
const shouldWaitOptions = typeof secretTokenOrOptions === "string" ? false : secretTokenOrOptions?.shouldWait;
|
|
1213
|
+
const isShouldWait = shouldWaitOptions && (typeof shouldWaitOptions === "object" || typeof shouldWaitOptions === "boolean");
|
|
1196
1214
|
return async (...args) => {
|
|
1197
1215
|
const { update, response, header, unauthorized } = frameworkAdapter(
|
|
1198
1216
|
...args
|
|
1199
1217
|
);
|
|
1200
1218
|
if (secretToken && header !== secretToken) return unauthorized();
|
|
1201
|
-
|
|
1202
|
-
|
|
1219
|
+
if (!isShouldWait) {
|
|
1220
|
+
bot.updates.queue.add(await update);
|
|
1221
|
+
if (response) return response();
|
|
1222
|
+
} else {
|
|
1223
|
+
const timeoutOptions = typeof shouldWaitOptions === "object" ? shouldWaitOptions : void 0;
|
|
1224
|
+
const timeout = timeoutOptions?.timeout ?? 3e4;
|
|
1225
|
+
const onTimeout = timeoutOptions?.onTimeout ?? "throw";
|
|
1226
|
+
await utils.timeoutWebhook(
|
|
1227
|
+
bot.updates.handleUpdate(await update, "wait"),
|
|
1228
|
+
timeout,
|
|
1229
|
+
onTimeout
|
|
1230
|
+
);
|
|
1231
|
+
if (response) return response();
|
|
1232
|
+
}
|
|
1203
1233
|
};
|
|
1204
1234
|
}
|
|
1205
1235
|
|
package/dist/index.d.cts
CHANGED
|
@@ -1012,31 +1012,37 @@ declare const frameworks: {
|
|
|
1012
1012
|
update: any;
|
|
1013
1013
|
header: any;
|
|
1014
1014
|
unauthorized: () => Response;
|
|
1015
|
+
response: () => Response;
|
|
1015
1016
|
};
|
|
1016
1017
|
fastify: (request: any, reply: any) => {
|
|
1017
1018
|
update: any;
|
|
1018
1019
|
header: any;
|
|
1019
1020
|
unauthorized: () => any;
|
|
1021
|
+
response: () => any;
|
|
1020
1022
|
};
|
|
1021
1023
|
hono: (c: any) => {
|
|
1022
1024
|
update: any;
|
|
1023
1025
|
header: any;
|
|
1024
1026
|
unauthorized: () => any;
|
|
1027
|
+
response: () => Response;
|
|
1025
1028
|
};
|
|
1026
1029
|
express: (req: any, res: any) => {
|
|
1027
1030
|
update: any;
|
|
1028
1031
|
header: any;
|
|
1029
1032
|
unauthorized: () => any;
|
|
1033
|
+
response: () => any;
|
|
1030
1034
|
};
|
|
1031
1035
|
koa: (ctx: any) => {
|
|
1032
1036
|
update: any;
|
|
1033
1037
|
header: any;
|
|
1034
1038
|
unauthorized: () => void;
|
|
1039
|
+
response: () => void;
|
|
1035
1040
|
};
|
|
1036
1041
|
http: (req: any, res: any) => {
|
|
1037
1042
|
update: Promise<TelegramUpdate>;
|
|
1038
1043
|
header: any;
|
|
1039
1044
|
unauthorized: () => any;
|
|
1045
|
+
response: () => any;
|
|
1040
1046
|
};
|
|
1041
1047
|
"std/http": (req: any) => {
|
|
1042
1048
|
update: any;
|
|
@@ -1050,6 +1056,12 @@ declare const frameworks: {
|
|
|
1050
1056
|
response: () => Response;
|
|
1051
1057
|
unauthorized: () => Response;
|
|
1052
1058
|
};
|
|
1059
|
+
cloudflare: (req: any) => {
|
|
1060
|
+
update: any;
|
|
1061
|
+
header: any;
|
|
1062
|
+
response: () => Response;
|
|
1063
|
+
unauthorized: () => Response;
|
|
1064
|
+
};
|
|
1053
1065
|
Request: (req: any) => {
|
|
1054
1066
|
update: any;
|
|
1055
1067
|
header: any;
|
|
@@ -1060,6 +1072,16 @@ declare const frameworks: {
|
|
|
1060
1072
|
|
|
1061
1073
|
/** Union type of webhook handlers name */
|
|
1062
1074
|
type WebhookHandlers = keyof typeof frameworks;
|
|
1075
|
+
interface WebhookHandlerOptionsShouldWait {
|
|
1076
|
+
/** Action to take when timeout occurs. @default "throw" */
|
|
1077
|
+
onTimeout?: "throw" | "return";
|
|
1078
|
+
/** Timeout in milliseconds. @default 10_000 */
|
|
1079
|
+
timeout?: number;
|
|
1080
|
+
}
|
|
1081
|
+
interface WebhookHandlerOptions {
|
|
1082
|
+
secretToken?: string;
|
|
1083
|
+
shouldWait?: boolean | WebhookHandlerOptionsShouldWait;
|
|
1084
|
+
}
|
|
1063
1085
|
/**
|
|
1064
1086
|
* Setup handler with yours web-framework to receive updates via webhook
|
|
1065
1087
|
*
|
|
@@ -1088,8 +1110,8 @@ type WebhookHandlers = keyof typeof frameworks;
|
|
|
1088
1110
|
* });
|
|
1089
1111
|
* ```
|
|
1090
1112
|
*/
|
|
1091
|
-
declare function webhookHandler<Framework extends keyof typeof frameworks>(bot: Bot, framework: Framework,
|
|
1113
|
+
declare function webhookHandler<Framework extends keyof typeof frameworks>(bot: Bot, framework: Framework, secretTokenOrOptions?: string | WebhookHandlerOptions): ReturnType<(typeof frameworks)[Framework]> extends {
|
|
1092
1114
|
response: () => any;
|
|
1093
1115
|
} ? (...args: Parameters<(typeof frameworks)[Framework]>) => ReturnType<ReturnType<(typeof frameworks)[Framework]>["response"]> : (...args: Parameters<(typeof frameworks)[Framework]>) => void;
|
|
1094
1116
|
|
|
1095
|
-
export { type AllowedUpdates, type AnyBot, type AnyPlugin, Bot, type BotOptions, type BotStartOptions, type BotStartOptionsLongPolling, type BotStartOptionsWebhook, type CallbackQueryShorthandContext, Composer, type DeriveDefinitions, type ErrorDefinitions, ErrorKind, type FilterDefinitions, type Handler, Hooks, type MaybePromise, type MaybeSuppressedParams, type MaybeSuppressedReturn, Plugin, type PollingStartOptions, type Suppress, type SuppressedAPIMethodParams, type SuppressedAPIMethodReturn, type SuppressedAPIMethods, TelegramError, Updates, type WebhookHandlers, webhookHandler };
|
|
1117
|
+
export { type AllowedUpdates, type AnyBot, type AnyPlugin, Bot, type BotOptions, type BotStartOptions, type BotStartOptionsLongPolling, type BotStartOptionsWebhook, type CallbackQueryShorthandContext, Composer, type DeriveDefinitions, type ErrorDefinitions, ErrorKind, type FilterDefinitions, type Handler, Hooks, type MaybePromise, type MaybeSuppressedParams, type MaybeSuppressedReturn, Plugin, type PollingStartOptions, type Suppress, type SuppressedAPIMethodParams, type SuppressedAPIMethodReturn, type SuppressedAPIMethods, TelegramError, Updates, type WebhookHandlerOptions, type WebhookHandlerOptionsShouldWait, type WebhookHandlers, webhookHandler };
|
package/dist/index.d.ts
CHANGED
|
@@ -1012,31 +1012,37 @@ declare const frameworks: {
|
|
|
1012
1012
|
update: any;
|
|
1013
1013
|
header: any;
|
|
1014
1014
|
unauthorized: () => Response;
|
|
1015
|
+
response: () => Response;
|
|
1015
1016
|
};
|
|
1016
1017
|
fastify: (request: any, reply: any) => {
|
|
1017
1018
|
update: any;
|
|
1018
1019
|
header: any;
|
|
1019
1020
|
unauthorized: () => any;
|
|
1021
|
+
response: () => any;
|
|
1020
1022
|
};
|
|
1021
1023
|
hono: (c: any) => {
|
|
1022
1024
|
update: any;
|
|
1023
1025
|
header: any;
|
|
1024
1026
|
unauthorized: () => any;
|
|
1027
|
+
response: () => Response;
|
|
1025
1028
|
};
|
|
1026
1029
|
express: (req: any, res: any) => {
|
|
1027
1030
|
update: any;
|
|
1028
1031
|
header: any;
|
|
1029
1032
|
unauthorized: () => any;
|
|
1033
|
+
response: () => any;
|
|
1030
1034
|
};
|
|
1031
1035
|
koa: (ctx: any) => {
|
|
1032
1036
|
update: any;
|
|
1033
1037
|
header: any;
|
|
1034
1038
|
unauthorized: () => void;
|
|
1039
|
+
response: () => void;
|
|
1035
1040
|
};
|
|
1036
1041
|
http: (req: any, res: any) => {
|
|
1037
1042
|
update: Promise<TelegramUpdate>;
|
|
1038
1043
|
header: any;
|
|
1039
1044
|
unauthorized: () => any;
|
|
1045
|
+
response: () => any;
|
|
1040
1046
|
};
|
|
1041
1047
|
"std/http": (req: any) => {
|
|
1042
1048
|
update: any;
|
|
@@ -1050,6 +1056,12 @@ declare const frameworks: {
|
|
|
1050
1056
|
response: () => Response;
|
|
1051
1057
|
unauthorized: () => Response;
|
|
1052
1058
|
};
|
|
1059
|
+
cloudflare: (req: any) => {
|
|
1060
|
+
update: any;
|
|
1061
|
+
header: any;
|
|
1062
|
+
response: () => Response;
|
|
1063
|
+
unauthorized: () => Response;
|
|
1064
|
+
};
|
|
1053
1065
|
Request: (req: any) => {
|
|
1054
1066
|
update: any;
|
|
1055
1067
|
header: any;
|
|
@@ -1060,6 +1072,16 @@ declare const frameworks: {
|
|
|
1060
1072
|
|
|
1061
1073
|
/** Union type of webhook handlers name */
|
|
1062
1074
|
type WebhookHandlers = keyof typeof frameworks;
|
|
1075
|
+
interface WebhookHandlerOptionsShouldWait {
|
|
1076
|
+
/** Action to take when timeout occurs. @default "throw" */
|
|
1077
|
+
onTimeout?: "throw" | "return";
|
|
1078
|
+
/** Timeout in milliseconds. @default 10_000 */
|
|
1079
|
+
timeout?: number;
|
|
1080
|
+
}
|
|
1081
|
+
interface WebhookHandlerOptions {
|
|
1082
|
+
secretToken?: string;
|
|
1083
|
+
shouldWait?: boolean | WebhookHandlerOptionsShouldWait;
|
|
1084
|
+
}
|
|
1063
1085
|
/**
|
|
1064
1086
|
* Setup handler with yours web-framework to receive updates via webhook
|
|
1065
1087
|
*
|
|
@@ -1088,8 +1110,8 @@ type WebhookHandlers = keyof typeof frameworks;
|
|
|
1088
1110
|
* });
|
|
1089
1111
|
* ```
|
|
1090
1112
|
*/
|
|
1091
|
-
declare function webhookHandler<Framework extends keyof typeof frameworks>(bot: Bot, framework: Framework,
|
|
1113
|
+
declare function webhookHandler<Framework extends keyof typeof frameworks>(bot: Bot, framework: Framework, secretTokenOrOptions?: string | WebhookHandlerOptions): ReturnType<(typeof frameworks)[Framework]> extends {
|
|
1092
1114
|
response: () => any;
|
|
1093
1115
|
} ? (...args: Parameters<(typeof frameworks)[Framework]>) => ReturnType<ReturnType<(typeof frameworks)[Framework]>["response"]> : (...args: Parameters<(typeof frameworks)[Framework]>) => void;
|
|
1094
1116
|
|
|
1095
|
-
export { type AllowedUpdates, type AnyBot, type AnyPlugin, Bot, type BotOptions, type BotStartOptions, type BotStartOptionsLongPolling, type BotStartOptionsWebhook, type CallbackQueryShorthandContext, Composer, type DeriveDefinitions, type ErrorDefinitions, ErrorKind, type FilterDefinitions, type Handler, Hooks, type MaybePromise, type MaybeSuppressedParams, type MaybeSuppressedReturn, Plugin, type PollingStartOptions, type Suppress, type SuppressedAPIMethodParams, type SuppressedAPIMethodReturn, type SuppressedAPIMethods, TelegramError, Updates, type WebhookHandlers, webhookHandler };
|
|
1117
|
+
export { type AllowedUpdates, type AnyBot, type AnyPlugin, Bot, type BotOptions, type BotStartOptions, type BotStartOptionsLongPolling, type BotStartOptionsWebhook, type CallbackQueryShorthandContext, Composer, type DeriveDefinitions, type ErrorDefinitions, ErrorKind, type FilterDefinitions, type Handler, Hooks, type MaybePromise, type MaybeSuppressedParams, type MaybeSuppressedReturn, Plugin, type PollingStartOptions, type Suppress, type SuppressedAPIMethodParams, type SuppressedAPIMethodReturn, type SuppressedAPIMethods, TelegramError, Updates, type WebhookHandlerOptions, type WebhookHandlerOptionsShouldWait, type WebhookHandlers, webhookHandler };
|
package/dist/index.js
CHANGED
|
@@ -9,7 +9,7 @@ export * from '@gramio/files';
|
|
|
9
9
|
import { FormattableMap } from '@gramio/format';
|
|
10
10
|
export * from '@gramio/format';
|
|
11
11
|
import debug from 'debug';
|
|
12
|
-
import { E as ErrorKind, s as sleep, w as withRetries, T as TelegramError, d as debug$updates, I as IS_BUN, a as simplifyObject } from './utils-
|
|
12
|
+
import { E as ErrorKind, s as sleep, w as withRetries, T as TelegramError, d as debug$updates, I as IS_BUN, a as simplifyObject, t as timeoutWebhook } from './utils-CJfJNxc_.js';
|
|
13
13
|
import { Composer as Composer$1, noopNext } from 'middleware-io';
|
|
14
14
|
export * from '@gramio/keyboards';
|
|
15
15
|
|
|
@@ -1127,8 +1127,9 @@ class Bot {
|
|
|
1127
1127
|
|
|
1128
1128
|
const SECRET_TOKEN_HEADER = "X-Telegram-Bot-Api-Secret-Token";
|
|
1129
1129
|
const WRONG_TOKEN_ERROR = "secret token is invalid";
|
|
1130
|
-
const
|
|
1131
|
-
const
|
|
1130
|
+
const RESPONSE_OK = "ok!";
|
|
1131
|
+
const responseOK = () => new Response(RESPONSE_OK);
|
|
1132
|
+
const responseUnauthorized = () => new Response(WRONG_TOKEN_ERROR, {
|
|
1132
1133
|
status: 401
|
|
1133
1134
|
// @ts-ignore
|
|
1134
1135
|
});
|
|
@@ -1136,22 +1137,26 @@ const frameworks = {
|
|
|
1136
1137
|
elysia: ({ body, headers }) => ({
|
|
1137
1138
|
update: body,
|
|
1138
1139
|
header: headers[SECRET_TOKEN_HEADER],
|
|
1139
|
-
unauthorized:
|
|
1140
|
+
unauthorized: responseUnauthorized,
|
|
1141
|
+
response: responseOK
|
|
1140
1142
|
}),
|
|
1141
1143
|
fastify: (request, reply) => ({
|
|
1142
1144
|
update: request.body,
|
|
1143
1145
|
header: request.headers[SECRET_TOKEN_HEADER],
|
|
1144
|
-
unauthorized: () => reply.code(401).send(WRONG_TOKEN_ERROR)
|
|
1146
|
+
unauthorized: () => reply.code(401).send(WRONG_TOKEN_ERROR),
|
|
1147
|
+
response: () => reply.send(RESPONSE_OK)
|
|
1145
1148
|
}),
|
|
1146
1149
|
hono: (c) => ({
|
|
1147
1150
|
update: c.req.json(),
|
|
1148
1151
|
header: c.req.header(SECRET_TOKEN_HEADER),
|
|
1149
|
-
unauthorized: () => c.text(WRONG_TOKEN_ERROR, 401)
|
|
1152
|
+
unauthorized: () => c.text(WRONG_TOKEN_ERROR, 401),
|
|
1153
|
+
response: responseOK
|
|
1150
1154
|
}),
|
|
1151
1155
|
express: (req, res) => ({
|
|
1152
1156
|
update: req.body,
|
|
1153
1157
|
header: req.header(SECRET_TOKEN_HEADER),
|
|
1154
|
-
unauthorized: () => res.status(401).send(WRONG_TOKEN_ERROR)
|
|
1158
|
+
unauthorized: () => res.status(401).send(WRONG_TOKEN_ERROR),
|
|
1159
|
+
response: () => res.send(RESPONSE_OK)
|
|
1155
1160
|
}),
|
|
1156
1161
|
koa: (ctx) => ({
|
|
1157
1162
|
update: ctx.request.body,
|
|
@@ -1159,6 +1164,10 @@ const frameworks = {
|
|
|
1159
1164
|
unauthorized: () => {
|
|
1160
1165
|
ctx.status === 401;
|
|
1161
1166
|
ctx.body = WRONG_TOKEN_ERROR;
|
|
1167
|
+
},
|
|
1168
|
+
response: () => {
|
|
1169
|
+
ctx.status = 200;
|
|
1170
|
+
ctx.body = RESPONSE_OK;
|
|
1162
1171
|
}
|
|
1163
1172
|
}),
|
|
1164
1173
|
http: (req, res) => ({
|
|
@@ -1170,38 +1179,59 @@ const frameworks = {
|
|
|
1170
1179
|
req.on("end", () => resolve(JSON.parse(body)));
|
|
1171
1180
|
}),
|
|
1172
1181
|
header: req.headers[SECRET_TOKEN_HEADER.toLowerCase()],
|
|
1173
|
-
unauthorized: () => res.writeHead(401).end(WRONG_TOKEN_ERROR)
|
|
1182
|
+
unauthorized: () => res.writeHead(401).end(WRONG_TOKEN_ERROR),
|
|
1183
|
+
response: () => res.writeHead(200).end(RESPONSE_OK)
|
|
1174
1184
|
}),
|
|
1175
|
-
// TODO: Maybe unify it by 'WebRequest'
|
|
1176
1185
|
"std/http": (req) => ({
|
|
1177
1186
|
update: req.json(),
|
|
1178
1187
|
header: req.headers.get(SECRET_TOKEN_HEADER),
|
|
1179
|
-
response:
|
|
1180
|
-
unauthorized:
|
|
1188
|
+
response: responseOK,
|
|
1189
|
+
unauthorized: responseUnauthorized
|
|
1181
1190
|
}),
|
|
1182
1191
|
"Bun.serve": (req) => ({
|
|
1183
1192
|
update: req.json(),
|
|
1184
1193
|
header: req.headers.get(SECRET_TOKEN_HEADER),
|
|
1185
|
-
response:
|
|
1186
|
-
unauthorized:
|
|
1194
|
+
response: responseOK,
|
|
1195
|
+
unauthorized: responseUnauthorized
|
|
1196
|
+
}),
|
|
1197
|
+
cloudflare: (req) => ({
|
|
1198
|
+
update: req.json(),
|
|
1199
|
+
header: req.headers.get(SECRET_TOKEN_HEADER),
|
|
1200
|
+
response: responseOK,
|
|
1201
|
+
unauthorized: responseUnauthorized
|
|
1187
1202
|
}),
|
|
1188
1203
|
Request: (req) => ({
|
|
1189
1204
|
update: req.json(),
|
|
1190
1205
|
header: req.headers.get(SECRET_TOKEN_HEADER),
|
|
1191
|
-
response:
|
|
1192
|
-
unauthorized:
|
|
1206
|
+
response: responseOK,
|
|
1207
|
+
unauthorized: responseUnauthorized
|
|
1193
1208
|
})
|
|
1194
1209
|
};
|
|
1195
1210
|
|
|
1196
|
-
function webhookHandler(bot, framework,
|
|
1211
|
+
function webhookHandler(bot, framework, secretTokenOrOptions) {
|
|
1197
1212
|
const frameworkAdapter = frameworks[framework];
|
|
1213
|
+
const secretToken = typeof secretTokenOrOptions === "string" ? secretTokenOrOptions : secretTokenOrOptions?.secretToken;
|
|
1214
|
+
const shouldWaitOptions = typeof secretTokenOrOptions === "string" ? false : secretTokenOrOptions?.shouldWait;
|
|
1215
|
+
const isShouldWait = shouldWaitOptions && (typeof shouldWaitOptions === "object" || typeof shouldWaitOptions === "boolean");
|
|
1198
1216
|
return async (...args) => {
|
|
1199
1217
|
const { update, response, header, unauthorized } = frameworkAdapter(
|
|
1200
1218
|
...args
|
|
1201
1219
|
);
|
|
1202
1220
|
if (secretToken && header !== secretToken) return unauthorized();
|
|
1203
|
-
|
|
1204
|
-
|
|
1221
|
+
if (!isShouldWait) {
|
|
1222
|
+
bot.updates.queue.add(await update);
|
|
1223
|
+
if (response) return response();
|
|
1224
|
+
} else {
|
|
1225
|
+
const timeoutOptions = typeof shouldWaitOptions === "object" ? shouldWaitOptions : void 0;
|
|
1226
|
+
const timeout = timeoutOptions?.timeout ?? 3e4;
|
|
1227
|
+
const onTimeout = timeoutOptions?.onTimeout ?? "throw";
|
|
1228
|
+
await timeoutWebhook(
|
|
1229
|
+
bot.updates.handleUpdate(await update, "wait"),
|
|
1230
|
+
timeout,
|
|
1231
|
+
onTimeout
|
|
1232
|
+
);
|
|
1233
|
+
if (response) return response();
|
|
1234
|
+
}
|
|
1205
1235
|
};
|
|
1206
1236
|
}
|
|
1207
1237
|
|
|
@@ -42,6 +42,20 @@ function simplifyObject(obj) {
|
|
|
42
42
|
const IS_BUN = typeof Bun !== "undefined";
|
|
43
43
|
debug("gramio:api");
|
|
44
44
|
const debug$updates = debug("gramio:updates");
|
|
45
|
+
function timeoutWebhook(task, timeout, mode) {
|
|
46
|
+
return new Promise((resolve, reject) => {
|
|
47
|
+
const timeoutTask = setTimeout(() => {
|
|
48
|
+
if (mode === "throw") {
|
|
49
|
+
reject(
|
|
50
|
+
new Error(`Webhook handler execution timed out after ${timeout}ms`)
|
|
51
|
+
);
|
|
52
|
+
} else {
|
|
53
|
+
resolve(void 0);
|
|
54
|
+
}
|
|
55
|
+
}, timeout);
|
|
56
|
+
task.then(resolve).catch(reject).finally(() => clearTimeout(timeoutTask));
|
|
57
|
+
});
|
|
58
|
+
}
|
|
45
59
|
|
|
46
60
|
async function suppressError(fn) {
|
|
47
61
|
try {
|
|
@@ -66,4 +80,4 @@ async function withRetries(resultPromise) {
|
|
|
66
80
|
return result;
|
|
67
81
|
}
|
|
68
82
|
|
|
69
|
-
export { ErrorKind as E, IS_BUN as I, TelegramError as T, simplifyObject as a, debug$updates as d, sleep as s, withRetries as w };
|
|
83
|
+
export { ErrorKind as E, IS_BUN as I, TelegramError as T, simplifyObject as a, debug$updates as d, sleep as s, timeoutWebhook as t, withRetries as w };
|
|
@@ -44,6 +44,20 @@ function simplifyObject(obj) {
|
|
|
44
44
|
const IS_BUN = typeof Bun !== "undefined";
|
|
45
45
|
debug("gramio:api");
|
|
46
46
|
const debug$updates = debug("gramio:updates");
|
|
47
|
+
function timeoutWebhook(task, timeout, mode) {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const timeoutTask = setTimeout(() => {
|
|
50
|
+
if (mode === "throw") {
|
|
51
|
+
reject(
|
|
52
|
+
new Error(`Webhook handler execution timed out after ${timeout}ms`)
|
|
53
|
+
);
|
|
54
|
+
} else {
|
|
55
|
+
resolve(void 0);
|
|
56
|
+
}
|
|
57
|
+
}, timeout);
|
|
58
|
+
task.then(resolve).catch(reject).finally(() => clearTimeout(timeoutTask));
|
|
59
|
+
});
|
|
60
|
+
}
|
|
47
61
|
|
|
48
62
|
async function suppressError(fn) {
|
|
49
63
|
try {
|
|
@@ -74,4 +88,5 @@ exports.TelegramError = TelegramError;
|
|
|
74
88
|
exports.debug$updates = debug$updates;
|
|
75
89
|
exports.simplifyObject = simplifyObject;
|
|
76
90
|
exports.sleep = sleep;
|
|
91
|
+
exports.timeoutWebhook = timeoutWebhook;
|
|
77
92
|
exports.withRetries = withRetries;
|
package/dist/utils.cjs
CHANGED
package/dist/utils.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { w as withRetries } from './utils-
|
|
1
|
+
export { w as withRetries } from './utils-CJfJNxc_.js';
|
|
2
2
|
import 'debug';
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gramio",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.4.
|
|
4
|
+
"version": "0.4.5",
|
|
5
5
|
"description": "Powerful, extensible and really type-safe Telegram Bot API framework",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
7
7
|
"module": "dist/index.js",
|
|
@@ -56,20 +56,20 @@
|
|
|
56
56
|
"license": "MIT",
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"@biomejs/biome": "1.9.4",
|
|
59
|
-
"@types/bun": "^1.2.
|
|
59
|
+
"@types/bun": "^1.2.14",
|
|
60
60
|
"@types/debug": "^4.1.12",
|
|
61
61
|
"expect-type": "^1.2.1",
|
|
62
|
-
"pkgroll": "^2.12.
|
|
62
|
+
"pkgroll": "^2.12.2",
|
|
63
63
|
"typescript": "^5.8.3"
|
|
64
64
|
},
|
|
65
65
|
"dependencies": {
|
|
66
66
|
"@gramio/callback-data": "^0.0.3",
|
|
67
67
|
"@gramio/contexts": "^0.2.5",
|
|
68
68
|
"@gramio/files": "^0.3.0",
|
|
69
|
-
"@gramio/format": "^0.2.
|
|
69
|
+
"@gramio/format": "^0.2.1",
|
|
70
70
|
"@gramio/keyboards": "^1.2.1",
|
|
71
71
|
"@gramio/types": "^9.0.2",
|
|
72
|
-
"debug": "^4.4.
|
|
72
|
+
"debug": "^4.4.1",
|
|
73
73
|
"middleware-io": "^2.8.1"
|
|
74
74
|
},
|
|
75
75
|
"files": ["dist"]
|