grammy-tools 1.36.2
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/LICENSE +21 -0
- package/README.md +339 -0
- package/out/bot.d.ts +314 -0
- package/out/bot.js +669 -0
- package/out/composer.d.ts +764 -0
- package/out/composer.js +704 -0
- package/out/context.d.ts +1618 -0
- package/out/context.js +1976 -0
- package/out/convenience/constants.d.ts +98 -0
- package/out/convenience/constants.js +37 -0
- package/out/convenience/frameworks.d.ts +216 -0
- package/out/convenience/frameworks.js +370 -0
- package/out/convenience/inline_query.d.ts +401 -0
- package/out/convenience/inline_query.js +461 -0
- package/out/convenience/input_media.d.ts +72 -0
- package/out/convenience/input_media.js +82 -0
- package/out/convenience/keyboard.d.ts +763 -0
- package/out/convenience/keyboard.js +935 -0
- package/out/convenience/session.d.ts +345 -0
- package/out/convenience/session.js +455 -0
- package/out/convenience/webhook.d.ts +59 -0
- package/out/convenience/webhook.js +80 -0
- package/out/core/api.d.ts +1628 -0
- package/out/core/api.js +1907 -0
- package/out/core/client.d.ts +180 -0
- package/out/core/client.js +226 -0
- package/out/core/error.d.ts +53 -0
- package/out/core/error.js +87 -0
- package/out/core/payload.d.ts +39 -0
- package/out/core/payload.js +199 -0
- package/out/filter.d.ts +959 -0
- package/out/filter.js +378 -0
- package/out/mod.d.ts +14 -0
- package/out/mod.js +43 -0
- package/out/platform.node.d.ts +16 -0
- package/out/platform.node.js +43 -0
- package/out/shim.node.d.ts +2 -0
- package/out/shim.node.js +7 -0
- package/out/types.d.ts +1 -0
- package/out/types.js +17 -0
- package/out/types.node.d.ts +78 -0
- package/out/types.node.js +107 -0
- package/out/web.d.ts +1 -0
- package/out/web.mjs +5172 -0
- package/package.json +71 -0
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import { type MiddlewareFn } from "../composer.js";
|
|
2
|
+
import { type Context } from "../context.js";
|
|
3
|
+
type MaybePromise<T> = Promise<T> | T;
|
|
4
|
+
/**
|
|
5
|
+
* A session flavor is a context flavor that holds session data under
|
|
6
|
+
* `ctx.session`.
|
|
7
|
+
*
|
|
8
|
+
* Session middleware will load the session data of a specific chat from your
|
|
9
|
+
* storage solution, and make it available to you on the context object. Check
|
|
10
|
+
* out the [documentation](https://grammy.dev/ref/core/session) on session
|
|
11
|
+
* middleware to know more, and read the section about sessions on the
|
|
12
|
+
* [website](https://grammy.dev/plugins/session).
|
|
13
|
+
*/
|
|
14
|
+
export interface SessionFlavor<S> {
|
|
15
|
+
/**
|
|
16
|
+
* Session data on the context object.
|
|
17
|
+
*
|
|
18
|
+
* **WARNING:** You have to make sure that your session data is not
|
|
19
|
+
* undefined by _providing an initial value to the session middleware_, or
|
|
20
|
+
* by making sure that `ctx.session` is assigned if it is empty! The type
|
|
21
|
+
* system does not include `| undefined` because this is really annoying to
|
|
22
|
+
* work with.
|
|
23
|
+
*
|
|
24
|
+
* Accessing `ctx.session` by reading or writing will throw if
|
|
25
|
+
* `getSessionKey(ctx) === undefined` for the respective context object
|
|
26
|
+
* `ctx`.
|
|
27
|
+
*/
|
|
28
|
+
get session(): S;
|
|
29
|
+
set session(session: S | null | undefined);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* A lazy session flavor is a context flavor that holds a promise of some
|
|
33
|
+
* session data under `ctx.session`.
|
|
34
|
+
*
|
|
35
|
+
* Lazy session middleware will provide this promise lazily on the context
|
|
36
|
+
* object. Once you access `ctx.session`, the storage will be queried and the
|
|
37
|
+
* session data becomes available. If you access `ctx.session` again for the
|
|
38
|
+
* same context object, the cached value will be used. Check out the
|
|
39
|
+
* [documentation](https://grammy.dev/ref/core/lazysession) on lazy session
|
|
40
|
+
* middleware to know more, and read the section about lazy sessions on the
|
|
41
|
+
* [website](https://grammy.dev/plugins/session#lazy-sessions).
|
|
42
|
+
*/
|
|
43
|
+
export interface LazySessionFlavor<S> {
|
|
44
|
+
/**
|
|
45
|
+
* Session data on the context object, potentially a promise.
|
|
46
|
+
*
|
|
47
|
+
* **WARNING:** You have to make sure that your session data is not
|
|
48
|
+
* undefined by _providing a default value to the session middleware_, or by
|
|
49
|
+
* making sure that `ctx.session` is assigned if it is empty! The type
|
|
50
|
+
* system does not include `| undefined` because this is really annoying to
|
|
51
|
+
* work with.
|
|
52
|
+
*
|
|
53
|
+
* Accessing `ctx.session` by reading or writing will throw iff
|
|
54
|
+
* `getSessionKey(ctx) === undefined` holds for the respective context
|
|
55
|
+
* object `ctx`.
|
|
56
|
+
*/
|
|
57
|
+
get session(): MaybePromise<S>;
|
|
58
|
+
set session(session: MaybePromise<S | null | undefined>);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* A storage adapter is an abstraction that provides read, write, and delete
|
|
62
|
+
* access to a storage solution of any kind. Storage adapters are used to keep
|
|
63
|
+
* session middleware independent of your database provider, and they allow you
|
|
64
|
+
* to pass your own storage solution.
|
|
65
|
+
*/
|
|
66
|
+
export interface StorageAdapter<T> {
|
|
67
|
+
/**
|
|
68
|
+
* Reads a value for the given key from the storage. May return the value or
|
|
69
|
+
* undefined, or a promise of either.
|
|
70
|
+
*/
|
|
71
|
+
read: (key: string) => MaybePromise<T | undefined>;
|
|
72
|
+
/**
|
|
73
|
+
* Writes a value for the given key to the storage.
|
|
74
|
+
*/
|
|
75
|
+
write: (key: string, value: T) => MaybePromise<void>;
|
|
76
|
+
/**
|
|
77
|
+
* Deletes a value for the given key from the storage.
|
|
78
|
+
*/
|
|
79
|
+
delete: (key: string) => MaybePromise<void>;
|
|
80
|
+
/**
|
|
81
|
+
* Checks whether a key exists in the storage.
|
|
82
|
+
*/
|
|
83
|
+
has?: (key: string) => MaybePromise<boolean>;
|
|
84
|
+
/**
|
|
85
|
+
* Lists all keys.
|
|
86
|
+
*/
|
|
87
|
+
readAllKeys?: () => Iterable<string> | AsyncIterable<string>;
|
|
88
|
+
/**
|
|
89
|
+
* Lists all values.
|
|
90
|
+
*/
|
|
91
|
+
readAllValues?: () => Iterable<T> | AsyncIterable<T>;
|
|
92
|
+
/**
|
|
93
|
+
* Lists all keys with their values.
|
|
94
|
+
*/
|
|
95
|
+
readAllEntries?: () => Iterable<[key: string, value: T]> | AsyncIterable<[key: string, value: T]>;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Options for session middleware.
|
|
99
|
+
*/
|
|
100
|
+
export interface SessionOptions<S, C extends Context = Context> {
|
|
101
|
+
type?: "single";
|
|
102
|
+
/**
|
|
103
|
+
* **Recommended to use.**
|
|
104
|
+
*
|
|
105
|
+
* A function that produces an initial value for `ctx.session`. This
|
|
106
|
+
* function will be called every time the storage solution returns undefined
|
|
107
|
+
* for a given session key. Make sure to create a new value every time, such
|
|
108
|
+
* that different context objects do that accidentally share the same
|
|
109
|
+
* session data.
|
|
110
|
+
*/
|
|
111
|
+
initial?: () => S;
|
|
112
|
+
/**
|
|
113
|
+
* An optional prefix to prepend to the session key after it was generated.
|
|
114
|
+
*
|
|
115
|
+
* This makes it easier to store session data under a namespace. You can
|
|
116
|
+
* technically achieve the same functionality by returning an already
|
|
117
|
+
* prefixed key from `getSessionKey`. This option is merely more convenient,
|
|
118
|
+
* as it does not require you to think about session key generation.
|
|
119
|
+
*/
|
|
120
|
+
prefix?: string;
|
|
121
|
+
/**
|
|
122
|
+
* This option lets you generate your own session keys per context object.
|
|
123
|
+
* The session key determines how to map the different session objects to
|
|
124
|
+
* your chats and users. Check out the
|
|
125
|
+
* [documentation](https://grammy.dev/plugins/session#how-to-use-sessions)
|
|
126
|
+
* on the website about how to use session middleware to know how session
|
|
127
|
+
* keys are used.
|
|
128
|
+
*
|
|
129
|
+
* The default implementation will store sessions per chat, as determined by
|
|
130
|
+
* `ctx.chatId`.
|
|
131
|
+
*/
|
|
132
|
+
getSessionKey?: (ctx: Omit<C, "session">) => MaybePromise<string | undefined>;
|
|
133
|
+
/**
|
|
134
|
+
* A storage adapter to your storage solution. Provides read, write, and
|
|
135
|
+
* delete access to the session middleware.
|
|
136
|
+
*
|
|
137
|
+
* Consider using a [known storage
|
|
138
|
+
* adapter](https://grammy.dev/plugins/session#known-storage-adapters)
|
|
139
|
+
* instead of rolling your own implementation of this.
|
|
140
|
+
*
|
|
141
|
+
* The default implementation will store session in memory. The data will be
|
|
142
|
+
* lost whenever your bot restarts.
|
|
143
|
+
*/
|
|
144
|
+
storage?: StorageAdapter<S>;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Options for session middleware if multi sessions are used. Specify `"type":
|
|
148
|
+
* "multi"` in the options to use multi sessions.
|
|
149
|
+
*/
|
|
150
|
+
export type MultiSessionOptions<S, C extends Context> = S extends Record<string, any> ? {
|
|
151
|
+
type: "multi";
|
|
152
|
+
} & MultiSessionOptionsRecord<S, C> : never;
|
|
153
|
+
type MultiSessionOptionsRecord<S extends Record<string, unknown>, C extends Context> = {
|
|
154
|
+
[K in keyof S]: SessionOptions<S[K], C>;
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Session middleware provides a persistent data storage for your bot. You can
|
|
158
|
+
* use it to let your bot remember any data you want, for example the messages
|
|
159
|
+
* it sent or received in the past. This is done by attaching _session data_ to
|
|
160
|
+
* every chat. The stored data is then provided on the context object under
|
|
161
|
+
* `ctx.session`.
|
|
162
|
+
*
|
|
163
|
+
* > **What is a session?** Simply put, the session of a chat is a little
|
|
164
|
+
* > persistent storage that is attached to it. As an example, your bot can send
|
|
165
|
+
* > a message to a chat and store the identifier of that message in the
|
|
166
|
+
* > corresponding session. The next time your bot receives an update from that
|
|
167
|
+
* > chat, the session will still contain that ID.
|
|
168
|
+
* >
|
|
169
|
+
* > Session data can be stored in a database, in a file, or simply in memory.
|
|
170
|
+
* > grammY only supports memory sessions out of the box, but you can use
|
|
171
|
+
* > third-party session middleware to connect to other storage solutions. Note
|
|
172
|
+
* > that memory sessions will be lost when you stop your bot and the process
|
|
173
|
+
* > exits, so they are usually not useful in production.
|
|
174
|
+
*
|
|
175
|
+
* Whenever your bot receives an update, the first thing the session middleware
|
|
176
|
+
* will do is to load the correct session from your storage solution. This
|
|
177
|
+
* object is then provided on `ctx.session` while your other middleware is
|
|
178
|
+
* running. As soon as your bot is done handling the update, the middleware
|
|
179
|
+
* takes over again and writes back the session object to your storage. This
|
|
180
|
+
* allows you to modify the session object arbitrarily in your middleware, and
|
|
181
|
+
* to stop worrying about the database.
|
|
182
|
+
*
|
|
183
|
+
* ```ts
|
|
184
|
+
* bot.use(session())
|
|
185
|
+
*
|
|
186
|
+
* bot.on('message', ctx => {
|
|
187
|
+
* // The session object is persisted across updates!
|
|
188
|
+
* const session = ctx.session
|
|
189
|
+
* })
|
|
190
|
+
* ```
|
|
191
|
+
*
|
|
192
|
+
* It is recommended to make use of the `initial` option in the configuration
|
|
193
|
+
* object, which correctly initializes session objects for new chats.
|
|
194
|
+
*
|
|
195
|
+
* You can delete the session data by setting `ctx.session` to `null` or
|
|
196
|
+
* `undefined`.
|
|
197
|
+
*
|
|
198
|
+
* Check out the [documentation](https://grammy.dev/plugins/session) on the
|
|
199
|
+
* website to know more about how sessions work in grammY.
|
|
200
|
+
*
|
|
201
|
+
* @param options Optional configuration to pass to the session middleware
|
|
202
|
+
*/
|
|
203
|
+
export declare function session<S, C extends Context>(options?: SessionOptions<S, C> | MultiSessionOptions<S, C>): MiddlewareFn<C & SessionFlavor<S>>;
|
|
204
|
+
/**
|
|
205
|
+
* > This is an advanced function of grammY.
|
|
206
|
+
*
|
|
207
|
+
* Generally speaking, lazy sessions work just like normal sessions—just they
|
|
208
|
+
* are loaded on demand. Except for a few `async`s and `await`s here and there,
|
|
209
|
+
* their usage looks 100 % identical.
|
|
210
|
+
*
|
|
211
|
+
* Instead of directly querying the storage every time an update arrives, lazy
|
|
212
|
+
* sessions quickly do this _once you access_ `ctx.session`. This can
|
|
213
|
+
* significantly reduce the database traffic (especially when your bot is added
|
|
214
|
+
* to group chats), because it skips a read and a wrote operation for all
|
|
215
|
+
* updates that the bot does not react to.
|
|
216
|
+
*
|
|
217
|
+
* ```ts
|
|
218
|
+
* // The options are identical
|
|
219
|
+
* bot.use(lazySession({ storage: ... }))
|
|
220
|
+
*
|
|
221
|
+
* bot.on('message', async ctx => {
|
|
222
|
+
* // The session object is persisted across updates!
|
|
223
|
+
* const session = await ctx.session
|
|
224
|
+
* // ^
|
|
225
|
+
* // |
|
|
226
|
+
* // This plain property access (no function call) will trigger the database query!
|
|
227
|
+
* })
|
|
228
|
+
* ```
|
|
229
|
+
*
|
|
230
|
+
* Check out the
|
|
231
|
+
* [documentation](https://grammy.dev/plugins/session#lazy-sessions) on the
|
|
232
|
+
* website to know more about how lazy sessions work in grammY.
|
|
233
|
+
*
|
|
234
|
+
* @param options Optional configuration to pass to the session middleware
|
|
235
|
+
*/
|
|
236
|
+
export declare function lazySession<S, C extends Context>(options?: SessionOptions<S, C>): MiddlewareFn<C & LazySessionFlavor<S>>;
|
|
237
|
+
/**
|
|
238
|
+
* When enhancing a storage adapter, it needs to be able to store additional
|
|
239
|
+
* information. It does this by wrapping the actual data inside an object, and
|
|
240
|
+
* adding more properties to this wrapper.
|
|
241
|
+
*
|
|
242
|
+
* This interface defines the additional properties that need to be stored by a
|
|
243
|
+
* storage adapter that supports enhanced sessions.
|
|
244
|
+
*/
|
|
245
|
+
export interface Enhance<T> {
|
|
246
|
+
/** Version */
|
|
247
|
+
v?: number;
|
|
248
|
+
/** Data */
|
|
249
|
+
__d: T;
|
|
250
|
+
/** Expiry date */
|
|
251
|
+
e?: number;
|
|
252
|
+
}
|
|
253
|
+
/** Options for enhanced sessions */
|
|
254
|
+
export interface MigrationOptions<T> {
|
|
255
|
+
/** The original storage adapter that will be enhanced */
|
|
256
|
+
storage: StorageAdapter<Enhance<T>>;
|
|
257
|
+
/**
|
|
258
|
+
* A set of session migrations, defined as an object mapping from version
|
|
259
|
+
* numbers to migration functions that transform data to the respective
|
|
260
|
+
* version.
|
|
261
|
+
*/
|
|
262
|
+
migrations?: Migrations;
|
|
263
|
+
/**
|
|
264
|
+
* Number of milliseconds after the last write operation until the session
|
|
265
|
+
* data expires.
|
|
266
|
+
*/
|
|
267
|
+
millisecondsToLive?: number;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* A mapping from version numbers to session migration functions. Each entry in
|
|
271
|
+
* this object has a version number as a key, and a function as a value.
|
|
272
|
+
*
|
|
273
|
+
* For a key `n`, the respective value should be a function that takes the
|
|
274
|
+
* previous session data and migrates it to conform with the data that is used
|
|
275
|
+
* by version `n`. The previous session data is defined by the next key less
|
|
276
|
+
* than `n`, such as `n-1`. Versions don't have to be integers, nor do all
|
|
277
|
+
* versions have to be adjacent. For example, you can use `[1, 1.5, 4]` as
|
|
278
|
+
* versions. If `n` is the lowest value in the set of keys, the function stored
|
|
279
|
+
* for `n` can be used to migrate session data that was stored before migrations
|
|
280
|
+
* were used.
|
|
281
|
+
*/
|
|
282
|
+
export interface Migrations {
|
|
283
|
+
[version: number]: (old: any) => any;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* You can use this function to transform an existing storage adapter, and add
|
|
287
|
+
* more features to it. Currently, you can add session migrations and expiry
|
|
288
|
+
* dates.
|
|
289
|
+
*
|
|
290
|
+
* You can use this function like so:
|
|
291
|
+
* ```ts
|
|
292
|
+
* const storage = ... // define your storage adapter
|
|
293
|
+
* const enhanced = enhanceStorage({ storage, millisecondsToLive: 500 })
|
|
294
|
+
* bot.use(session({ storage: enhanced }))
|
|
295
|
+
* ```
|
|
296
|
+
*
|
|
297
|
+
* @param options Session enhancing options
|
|
298
|
+
* @returns The enhanced storage adapter
|
|
299
|
+
*/
|
|
300
|
+
export declare function enhanceStorage<T>(options: MigrationOptions<T>): StorageAdapter<T>;
|
|
301
|
+
/**
|
|
302
|
+
* The memory session storage is a built-in storage adapter that saves your
|
|
303
|
+
* session data in RAM using a regular JavaScript `Map` object. If you use this
|
|
304
|
+
* storage adapter, all sessions will be lost when your process terminates or
|
|
305
|
+
* restarts. Hence, you should only use it for short-lived data that is not
|
|
306
|
+
* important to persist.
|
|
307
|
+
*
|
|
308
|
+
* This class is used as default if you do not provide a storage adapter, e.g.
|
|
309
|
+
* to your database.
|
|
310
|
+
*
|
|
311
|
+
* This storage adapter features expiring sessions. When instantiating this
|
|
312
|
+
* class yourself, you can pass a time to live in milliseconds that will be used
|
|
313
|
+
* for each session object. If a session for a user expired, the session data
|
|
314
|
+
* will be discarded on its first read, and a fresh session object as returned
|
|
315
|
+
* by the `initial` option (or undefined) will be put into place.
|
|
316
|
+
*/
|
|
317
|
+
export declare class MemorySessionStorage<S> implements StorageAdapter<S> {
|
|
318
|
+
private readonly timeToLive?;
|
|
319
|
+
/**
|
|
320
|
+
* Internally used `Map` instance that stores the session data
|
|
321
|
+
*/
|
|
322
|
+
protected readonly storage: Map<string, {
|
|
323
|
+
session: S;
|
|
324
|
+
expires?: number;
|
|
325
|
+
}>;
|
|
326
|
+
/**
|
|
327
|
+
* Constructs a new memory session storage with the given time to live. Note
|
|
328
|
+
* that this storage adapter will not store your data permanently.
|
|
329
|
+
*
|
|
330
|
+
* @param timeToLive TTL in milliseconds, default is `Infinity`
|
|
331
|
+
*/
|
|
332
|
+
constructor(timeToLive?: number | undefined);
|
|
333
|
+
read(key: string): S | undefined;
|
|
334
|
+
/**
|
|
335
|
+
* @deprecated Use {@link readAllValues} instead
|
|
336
|
+
*/
|
|
337
|
+
readAll(): S[];
|
|
338
|
+
readAllKeys(): string[];
|
|
339
|
+
readAllValues(): S[];
|
|
340
|
+
readAllEntries(): [string, S][];
|
|
341
|
+
has(key: string): boolean;
|
|
342
|
+
write(key: string, value: S): void;
|
|
343
|
+
delete(key: string): void;
|
|
344
|
+
}
|
|
345
|
+
export {};
|