@tgify/tgify 0.1.0 → 0.1.4

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.
Files changed (42) hide show
  1. package/LICENSE +23 -23
  2. package/README.md +356 -356
  3. package/lib/cli.mjs +9 -9
  4. package/package.json +1 -1
  5. package/src/button.ts +182 -182
  6. package/src/composer.ts +1008 -1008
  7. package/src/context.ts +1661 -1661
  8. package/src/core/helpers/args.ts +63 -63
  9. package/src/core/helpers/check.ts +71 -71
  10. package/src/core/helpers/compact.ts +18 -18
  11. package/src/core/helpers/deunionize.ts +26 -26
  12. package/src/core/helpers/formatting.ts +119 -119
  13. package/src/core/helpers/util.ts +96 -96
  14. package/src/core/network/client.ts +396 -396
  15. package/src/core/network/error.ts +29 -29
  16. package/src/core/network/multipart-stream.ts +45 -45
  17. package/src/core/network/polling.ts +94 -94
  18. package/src/core/network/webhook.ts +58 -58
  19. package/src/core/types/typegram.ts +54 -54
  20. package/src/filters.ts +109 -109
  21. package/src/format.ts +110 -110
  22. package/src/future.ts +213 -213
  23. package/src/index.ts +17 -17
  24. package/src/input.ts +59 -59
  25. package/src/markup.ts +142 -142
  26. package/src/middleware.ts +24 -24
  27. package/src/reactions.ts +118 -118
  28. package/src/router.ts +55 -55
  29. package/src/scenes/base.ts +52 -52
  30. package/src/scenes/context.ts +136 -136
  31. package/src/scenes/index.ts +21 -21
  32. package/src/scenes/stage.ts +71 -71
  33. package/src/scenes/wizard/context.ts +58 -58
  34. package/src/scenes/wizard/index.ts +63 -63
  35. package/src/scenes.ts +1 -1
  36. package/src/session.ts +204 -204
  37. package/src/telegraf.ts +354 -354
  38. package/src/telegram-types.ts +219 -219
  39. package/src/telegram.ts +1635 -1635
  40. package/src/types.ts +2 -2
  41. package/src/utils.ts +1 -1
  42. package/typings/telegraf.d.ts.map +1 -1
@@ -1,63 +1,63 @@
1
- import BaseScene, { SceneOptions } from '../base'
2
- import { Middleware, MiddlewareObj } from '../../middleware'
3
- import WizardContextWizard, { WizardSessionData } from './context'
4
- import Composer from '../../composer'
5
- import Context from '../../context'
6
- import SceneContextScene from '../context'
7
-
8
- export class WizardScene<
9
- C extends Context & {
10
- scene: SceneContextScene<C, WizardSessionData>
11
- wizard: WizardContextWizard<C>
12
- },
13
- >
14
- extends BaseScene<C>
15
- implements MiddlewareObj<C>
16
- {
17
- steps: Array<Middleware<C>>
18
-
19
- constructor(id: string, ...steps: Array<Middleware<C>>)
20
- constructor(
21
- id: string,
22
- options: SceneOptions<C>,
23
- ...steps: Array<Middleware<C>>
24
- )
25
- constructor(
26
- id: string,
27
- options: SceneOptions<C> | Middleware<C>,
28
- ...steps: Array<Middleware<C>>
29
- ) {
30
- let opts: SceneOptions<C> | undefined
31
- let s: Array<Middleware<C>>
32
- if (typeof options === 'function' || 'middleware' in options) {
33
- opts = undefined
34
- s = [options, ...steps]
35
- } else {
36
- opts = options
37
- s = steps
38
- }
39
- super(id, opts)
40
- this.steps = s
41
- }
42
-
43
- middleware() {
44
- return Composer.compose<C>([
45
- (ctx, next) => {
46
- ctx.wizard = new WizardContextWizard<C>(ctx, this.steps)
47
- return next()
48
- },
49
- super.middleware(),
50
- (ctx, next) => {
51
- if (ctx.wizard.step === undefined) {
52
- ctx.wizard.selectStep(0)
53
- return ctx.scene.leave()
54
- }
55
- return Composer.unwrap(ctx.wizard.step)(ctx, next)
56
- },
57
- ])
58
- }
59
-
60
- enterMiddleware() {
61
- return Composer.compose([this.enterHandler, this.middleware()])
62
- }
63
- }
1
+ import BaseScene, { SceneOptions } from '../base'
2
+ import { Middleware, MiddlewareObj } from '../../middleware'
3
+ import WizardContextWizard, { WizardSessionData } from './context'
4
+ import Composer from '../../composer'
5
+ import Context from '../../context'
6
+ import SceneContextScene from '../context'
7
+
8
+ export class WizardScene<
9
+ C extends Context & {
10
+ scene: SceneContextScene<C, WizardSessionData>
11
+ wizard: WizardContextWizard<C>
12
+ },
13
+ >
14
+ extends BaseScene<C>
15
+ implements MiddlewareObj<C>
16
+ {
17
+ steps: Array<Middleware<C>>
18
+
19
+ constructor(id: string, ...steps: Array<Middleware<C>>)
20
+ constructor(
21
+ id: string,
22
+ options: SceneOptions<C>,
23
+ ...steps: Array<Middleware<C>>
24
+ )
25
+ constructor(
26
+ id: string,
27
+ options: SceneOptions<C> | Middleware<C>,
28
+ ...steps: Array<Middleware<C>>
29
+ ) {
30
+ let opts: SceneOptions<C> | undefined
31
+ let s: Array<Middleware<C>>
32
+ if (typeof options === 'function' || 'middleware' in options) {
33
+ opts = undefined
34
+ s = [options, ...steps]
35
+ } else {
36
+ opts = options
37
+ s = steps
38
+ }
39
+ super(id, opts)
40
+ this.steps = s
41
+ }
42
+
43
+ middleware() {
44
+ return Composer.compose<C>([
45
+ (ctx, next) => {
46
+ ctx.wizard = new WizardContextWizard<C>(ctx, this.steps)
47
+ return next()
48
+ },
49
+ super.middleware(),
50
+ (ctx, next) => {
51
+ if (ctx.wizard.step === undefined) {
52
+ ctx.wizard.selectStep(0)
53
+ return ctx.scene.leave()
54
+ }
55
+ return Composer.unwrap(ctx.wizard.step)(ctx, next)
56
+ },
57
+ ])
58
+ }
59
+
60
+ enterMiddleware() {
61
+ return Composer.compose([this.enterHandler, this.middleware()])
62
+ }
63
+ }
package/src/scenes.ts CHANGED
@@ -1 +1 @@
1
- export * from './scenes/index.js'
1
+ export * from './scenes/index.js'
package/src/session.ts CHANGED
@@ -1,204 +1,204 @@
1
- import { Context } from './context'
2
- import { ExclusiveKeys, MaybePromise } from './core/helpers/util'
3
- import { MiddlewareFn } from './middleware'
4
- import d from 'debug'
5
- const debug = d('telegraf:session')
6
-
7
- export interface SyncSessionStore<T> {
8
- get: (name: string) => T | undefined
9
- set: (name: string, value: T) => void
10
- delete: (name: string) => void
11
- }
12
-
13
- export interface AsyncSessionStore<T> {
14
- get: (name: string) => Promise<T | undefined>
15
- set: (name: string, value: T) => Promise<unknown>
16
- delete: (name: string) => Promise<unknown>
17
- }
18
-
19
- export type SessionStore<T> = SyncSessionStore<T> | AsyncSessionStore<T>
20
-
21
- interface SessionOptions<S, C extends Context, P extends string> {
22
- /** Customise the session prop. Defaults to "session" and is available as ctx.session. */
23
- property?: P
24
- getSessionKey?: (ctx: C) => MaybePromise<string | undefined>
25
- store?: SessionStore<S>
26
- defaultSession?: (ctx: C) => S
27
- }
28
-
29
- /** @deprecated session can use custom properties now. Construct this type directly. */
30
- export interface SessionContext<S extends object> extends Context {
31
- session?: S
32
- }
33
-
34
- /**
35
- * Returns middleware that adds `ctx.session` for storing arbitrary state per session key.
36
- *
37
- * The default `getSessionKey` is `${ctx.from.id}:${ctx.chat.id}`.
38
- * If either `ctx.from` or `ctx.chat` is `undefined`, default session key and thus `ctx.session` are also `undefined`.
39
- *
40
- * > ⚠️ Session data is kept only in memory by default, which means that all data will be lost when the process is terminated.
41
- * >
42
- * > If you want to persist data across process restarts, or share it among multiple instances, you should use
43
- * [@telegraf/session](https://www.npmjs.com/package/@telegraf/session), or pass custom `storage`.
44
- *
45
- * @see {@link https://github.com/feathers-studio/telegraf-docs/blob/b694bcc36b4f71fb1cd650a345c2009ab4d2a2a5/guide/session.md Telegraf Docs | Session}
46
- * @see {@link https://github.com/feathers-studio/telegraf-docs/blob/master/examples/session-bot.ts Example}
47
- */
48
- export function session<
49
- S extends NonNullable<C[P]>,
50
- C extends Context & { [key in P]?: C[P] },
51
- P extends (ExclusiveKeys<C, Context> & string) | 'session' = 'session',
52
- // ^ Only allow prop names that aren't keys in base Context.
53
- // At type level, this is cosmetic. To not get cluttered with all Context keys.
54
- >(options?: SessionOptions<S, C, P>): MiddlewareFn<C> {
55
- const prop = options?.property ?? ('session' as P)
56
- const getSessionKey = options?.getSessionKey ?? defaultGetSessionKey
57
- const store = options?.store ?? new MemorySessionStore()
58
- // caches value from store in-memory while simultaneous updates share it
59
- // when counter reaches 0, the cached ref will be freed from memory
60
- const cache = new Map<string, { ref?: S; counter: number }>()
61
- // temporarily stores concurrent requests
62
- const concurrents = new Map<string, MaybePromise<S | undefined>>()
63
-
64
- // this function must be handled with care
65
- // read full description on the original PR: https://github.com/telegraf/telegraf/pull/1713
66
- // make sure to update the tests in test/session.js if you make any changes or fix bugs here
67
- return async (ctx, next) => {
68
- const updId = ctx.update.update_id
69
-
70
- let released = false
71
-
72
- function releaseChecks() {
73
- if (released && process.env.EXPERIMENTAL_SESSION_CHECKS)
74
- throw new Error(
75
- "Session was accessed or assigned to after the middleware chain exhausted. This is a bug in your code. You're probably accessing session asynchronously and missing awaits."
76
- )
77
- }
78
-
79
- // because this is async, requests may still race here, but it will get autocorrected at (1)
80
- // v5 getSessionKey should probably be synchronous to avoid that
81
- const key = await getSessionKey(ctx)
82
- if (!key) {
83
- // Leaving this here could be useful to check for `prop in ctx` in future middleware
84
- ctx[prop] = undefined as unknown as S
85
- return await next()
86
- }
87
-
88
- let cached = cache.get(key)
89
- if (cached) {
90
- debug(`(${updId}) found cached session, reusing from cache`)
91
- ++cached.counter
92
- } else {
93
- debug(`(${updId}) did not find cached session`)
94
- // if another concurrent request has already sent a store request, fetch that instead
95
- let promise = concurrents.get(key)
96
- if (promise)
97
- debug(`(${updId}) found a concurrent request, reusing promise`)
98
- else {
99
- debug(`(${updId}) fetching from upstream store`)
100
- promise = store.get(key)
101
- }
102
- // synchronously store promise so concurrent requests can share response
103
- concurrents.set(key, promise)
104
- const upstream = await promise
105
- // all concurrent awaits will have promise in their closure, safe to remove now
106
- concurrents.delete(key)
107
- debug(`(${updId}) updating cache`)
108
- // another request may have beaten us to the punch
109
- const c = cache.get(key)
110
- if (c) {
111
- // another request did beat us to the punch
112
- c.counter++
113
- // (1) preserve cached reference; in-memory reference is always newer than from store
114
- cached = c
115
- } else {
116
- // we're the first, so we must cache the reference
117
- cached = { ref: upstream ?? options?.defaultSession?.(ctx), counter: 1 }
118
- cache.set(key, cached)
119
- }
120
- }
121
-
122
- // TS already knows cached is always defined by this point, but does not guard cached.
123
- // It will, however, guard `c` here.
124
- const c = cached
125
-
126
- let touched = false
127
-
128
- Object.defineProperty(ctx, prop, {
129
- get() {
130
- releaseChecks()
131
- touched = true
132
- return c.ref
133
- },
134
- set(value: S) {
135
- releaseChecks()
136
- touched = true
137
- c.ref = value
138
- },
139
- })
140
-
141
- try {
142
- await next()
143
- released = true
144
- } finally {
145
- if (--c.counter === 0) {
146
- // decrement to avoid memory leak
147
- debug(`(${updId}) refcounter reached 0, removing cached`)
148
- cache.delete(key)
149
- }
150
- debug(`(${updId}) middlewares completed, checking session`)
151
-
152
- // only update store if ctx.session was touched
153
- if (touched)
154
- if (c.ref == null) {
155
- debug(`(${updId}) ctx.${prop} missing, removing from store`)
156
- await store.delete(key)
157
- } else {
158
- debug(`(${updId}) ctx.${prop} found, updating store`)
159
- await store.set(key, c.ref)
160
- }
161
- }
162
- }
163
- }
164
-
165
- function defaultGetSessionKey(ctx: Context): string | undefined {
166
- const fromId = ctx.from?.id
167
- const chatId = ctx.chat?.id
168
- if (fromId == null || chatId == null) return undefined
169
- return `${fromId}:${chatId}`
170
- }
171
-
172
- /** @deprecated Use `Map` */
173
- export class MemorySessionStore<T> implements SyncSessionStore<T> {
174
- private readonly store = new Map<string, { session: T; expires: number }>()
175
-
176
- constructor(private readonly ttl = Infinity) {}
177
-
178
- get(name: string): T | undefined {
179
- const entry = this.store.get(name)
180
- if (entry == null) {
181
- return undefined
182
- } else if (entry.expires < Date.now()) {
183
- this.delete(name)
184
- return undefined
185
- }
186
- return entry.session
187
- }
188
-
189
- set(name: string, value: T): void {
190
- const now = Date.now()
191
- this.store.set(name, { session: value, expires: now + this.ttl })
192
- }
193
-
194
- delete(name: string): void {
195
- this.store.delete(name)
196
- }
197
- }
198
-
199
- /** @deprecated session can use custom properties now. Directly use `'session' in ctx` instead */
200
- export function isSessionContext<S extends object>(
201
- ctx: Context
202
- ): ctx is SessionContext<S> {
203
- return 'session' in ctx
204
- }
1
+ import { Context } from './context'
2
+ import { ExclusiveKeys, MaybePromise } from './core/helpers/util'
3
+ import { MiddlewareFn } from './middleware'
4
+ import d from 'debug'
5
+ const debug = d('telegraf:session')
6
+
7
+ export interface SyncSessionStore<T> {
8
+ get: (name: string) => T | undefined
9
+ set: (name: string, value: T) => void
10
+ delete: (name: string) => void
11
+ }
12
+
13
+ export interface AsyncSessionStore<T> {
14
+ get: (name: string) => Promise<T | undefined>
15
+ set: (name: string, value: T) => Promise<unknown>
16
+ delete: (name: string) => Promise<unknown>
17
+ }
18
+
19
+ export type SessionStore<T> = SyncSessionStore<T> | AsyncSessionStore<T>
20
+
21
+ interface SessionOptions<S, C extends Context, P extends string> {
22
+ /** Customise the session prop. Defaults to "session" and is available as ctx.session. */
23
+ property?: P
24
+ getSessionKey?: (ctx: C) => MaybePromise<string | undefined>
25
+ store?: SessionStore<S>
26
+ defaultSession?: (ctx: C) => S
27
+ }
28
+
29
+ /** @deprecated session can use custom properties now. Construct this type directly. */
30
+ export interface SessionContext<S extends object> extends Context {
31
+ session?: S
32
+ }
33
+
34
+ /**
35
+ * Returns middleware that adds `ctx.session` for storing arbitrary state per session key.
36
+ *
37
+ * The default `getSessionKey` is `${ctx.from.id}:${ctx.chat.id}`.
38
+ * If either `ctx.from` or `ctx.chat` is `undefined`, default session key and thus `ctx.session` are also `undefined`.
39
+ *
40
+ * > ⚠️ Session data is kept only in memory by default, which means that all data will be lost when the process is terminated.
41
+ * >
42
+ * > If you want to persist data across process restarts, or share it among multiple instances, you should use
43
+ * [@telegraf/session](https://www.npmjs.com/package/@telegraf/session), or pass custom `storage`.
44
+ *
45
+ * @see {@link https://github.com/feathers-studio/telegraf-docs/blob/b694bcc36b4f71fb1cd650a345c2009ab4d2a2a5/guide/session.md Telegraf Docs | Session}
46
+ * @see {@link https://github.com/feathers-studio/telegraf-docs/blob/master/examples/session-bot.ts Example}
47
+ */
48
+ export function session<
49
+ S extends NonNullable<C[P]>,
50
+ C extends Context & { [key in P]?: C[P] },
51
+ P extends (ExclusiveKeys<C, Context> & string) | 'session' = 'session',
52
+ // ^ Only allow prop names that aren't keys in base Context.
53
+ // At type level, this is cosmetic. To not get cluttered with all Context keys.
54
+ >(options?: SessionOptions<S, C, P>): MiddlewareFn<C> {
55
+ const prop = options?.property ?? ('session' as P)
56
+ const getSessionKey = options?.getSessionKey ?? defaultGetSessionKey
57
+ const store = options?.store ?? new MemorySessionStore()
58
+ // caches value from store in-memory while simultaneous updates share it
59
+ // when counter reaches 0, the cached ref will be freed from memory
60
+ const cache = new Map<string, { ref?: S; counter: number }>()
61
+ // temporarily stores concurrent requests
62
+ const concurrents = new Map<string, MaybePromise<S | undefined>>()
63
+
64
+ // this function must be handled with care
65
+ // read full description on the original PR: https://github.com/telegraf/telegraf/pull/1713
66
+ // make sure to update the tests in test/session.js if you make any changes or fix bugs here
67
+ return async (ctx, next) => {
68
+ const updId = ctx.update.update_id
69
+
70
+ let released = false
71
+
72
+ function releaseChecks() {
73
+ if (released && process.env.EXPERIMENTAL_SESSION_CHECKS)
74
+ throw new Error(
75
+ "Session was accessed or assigned to after the middleware chain exhausted. This is a bug in your code. You're probably accessing session asynchronously and missing awaits."
76
+ )
77
+ }
78
+
79
+ // because this is async, requests may still race here, but it will get autocorrected at (1)
80
+ // v5 getSessionKey should probably be synchronous to avoid that
81
+ const key = await getSessionKey(ctx)
82
+ if (!key) {
83
+ // Leaving this here could be useful to check for `prop in ctx` in future middleware
84
+ ctx[prop] = undefined as unknown as S
85
+ return await next()
86
+ }
87
+
88
+ let cached = cache.get(key)
89
+ if (cached) {
90
+ debug(`(${updId}) found cached session, reusing from cache`)
91
+ ++cached.counter
92
+ } else {
93
+ debug(`(${updId}) did not find cached session`)
94
+ // if another concurrent request has already sent a store request, fetch that instead
95
+ let promise = concurrents.get(key)
96
+ if (promise)
97
+ debug(`(${updId}) found a concurrent request, reusing promise`)
98
+ else {
99
+ debug(`(${updId}) fetching from upstream store`)
100
+ promise = store.get(key)
101
+ }
102
+ // synchronously store promise so concurrent requests can share response
103
+ concurrents.set(key, promise)
104
+ const upstream = await promise
105
+ // all concurrent awaits will have promise in their closure, safe to remove now
106
+ concurrents.delete(key)
107
+ debug(`(${updId}) updating cache`)
108
+ // another request may have beaten us to the punch
109
+ const c = cache.get(key)
110
+ if (c) {
111
+ // another request did beat us to the punch
112
+ c.counter++
113
+ // (1) preserve cached reference; in-memory reference is always newer than from store
114
+ cached = c
115
+ } else {
116
+ // we're the first, so we must cache the reference
117
+ cached = { ref: upstream ?? options?.defaultSession?.(ctx), counter: 1 }
118
+ cache.set(key, cached)
119
+ }
120
+ }
121
+
122
+ // TS already knows cached is always defined by this point, but does not guard cached.
123
+ // It will, however, guard `c` here.
124
+ const c = cached
125
+
126
+ let touched = false
127
+
128
+ Object.defineProperty(ctx, prop, {
129
+ get() {
130
+ releaseChecks()
131
+ touched = true
132
+ return c.ref
133
+ },
134
+ set(value: S) {
135
+ releaseChecks()
136
+ touched = true
137
+ c.ref = value
138
+ },
139
+ })
140
+
141
+ try {
142
+ await next()
143
+ released = true
144
+ } finally {
145
+ if (--c.counter === 0) {
146
+ // decrement to avoid memory leak
147
+ debug(`(${updId}) refcounter reached 0, removing cached`)
148
+ cache.delete(key)
149
+ }
150
+ debug(`(${updId}) middlewares completed, checking session`)
151
+
152
+ // only update store if ctx.session was touched
153
+ if (touched)
154
+ if (c.ref == null) {
155
+ debug(`(${updId}) ctx.${prop} missing, removing from store`)
156
+ await store.delete(key)
157
+ } else {
158
+ debug(`(${updId}) ctx.${prop} found, updating store`)
159
+ await store.set(key, c.ref)
160
+ }
161
+ }
162
+ }
163
+ }
164
+
165
+ function defaultGetSessionKey(ctx: Context): string | undefined {
166
+ const fromId = ctx.from?.id
167
+ const chatId = ctx.chat?.id
168
+ if (fromId == null || chatId == null) return undefined
169
+ return `${fromId}:${chatId}`
170
+ }
171
+
172
+ /** @deprecated Use `Map` */
173
+ export class MemorySessionStore<T> implements SyncSessionStore<T> {
174
+ private readonly store = new Map<string, { session: T; expires: number }>()
175
+
176
+ constructor(private readonly ttl = Infinity) {}
177
+
178
+ get(name: string): T | undefined {
179
+ const entry = this.store.get(name)
180
+ if (entry == null) {
181
+ return undefined
182
+ } else if (entry.expires < Date.now()) {
183
+ this.delete(name)
184
+ return undefined
185
+ }
186
+ return entry.session
187
+ }
188
+
189
+ set(name: string, value: T): void {
190
+ const now = Date.now()
191
+ this.store.set(name, { session: value, expires: now + this.ttl })
192
+ }
193
+
194
+ delete(name: string): void {
195
+ this.store.delete(name)
196
+ }
197
+ }
198
+
199
+ /** @deprecated session can use custom properties now. Directly use `'session' in ctx` instead */
200
+ export function isSessionContext<S extends object>(
201
+ ctx: Context
202
+ ): ctx is SessionContext<S> {
203
+ return 'session' in ctx
204
+ }