@tgify/tgify 0.1.0

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 (168) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +356 -0
  3. package/filters.d.ts +1 -0
  4. package/filters.js +1 -0
  5. package/format.d.ts +1 -0
  6. package/format.js +1 -0
  7. package/future.d.ts +1 -0
  8. package/future.js +1 -0
  9. package/lib/button.js +100 -0
  10. package/lib/cli.mjs +105 -0
  11. package/lib/composer.js +582 -0
  12. package/lib/context.js +1219 -0
  13. package/lib/core/helpers/args.js +57 -0
  14. package/lib/core/helpers/check.js +55 -0
  15. package/lib/core/helpers/compact.js +16 -0
  16. package/lib/core/helpers/deunionize.js +12 -0
  17. package/lib/core/helpers/formatting.js +91 -0
  18. package/lib/core/helpers/util.js +50 -0
  19. package/lib/core/network/client.js +330 -0
  20. package/lib/core/network/error.js +21 -0
  21. package/lib/core/network/multipart-stream.js +71 -0
  22. package/lib/core/network/polling.js +87 -0
  23. package/lib/core/network/webhook.js +54 -0
  24. package/lib/core/types/typegram.js +27 -0
  25. package/lib/filters.js +69 -0
  26. package/lib/format.js +38 -0
  27. package/lib/future.js +149 -0
  28. package/lib/index.js +58 -0
  29. package/lib/input.js +61 -0
  30. package/lib/markup.js +121 -0
  31. package/lib/middleware.js +2 -0
  32. package/lib/reactions.js +84 -0
  33. package/lib/router.js +46 -0
  34. package/lib/scenes/base.js +39 -0
  35. package/lib/scenes/context.js +104 -0
  36. package/lib/scenes/index.js +21 -0
  37. package/lib/scenes/stage.js +49 -0
  38. package/lib/scenes/wizard/context.js +31 -0
  39. package/lib/scenes/wizard/index.js +45 -0
  40. package/lib/scenes.js +17 -0
  41. package/lib/session.js +166 -0
  42. package/lib/telegraf.js +256 -0
  43. package/lib/telegram-types.js +6 -0
  44. package/lib/telegram.js +1240 -0
  45. package/lib/types.js +2 -0
  46. package/lib/utils.js +5 -0
  47. package/markup.d.ts +1 -0
  48. package/markup.js +1 -0
  49. package/package.json +140 -0
  50. package/scenes.d.ts +1 -0
  51. package/scenes.js +1 -0
  52. package/session.d.ts +1 -0
  53. package/session.js +1 -0
  54. package/src/button.ts +182 -0
  55. package/src/composer.ts +1008 -0
  56. package/src/context.ts +1661 -0
  57. package/src/core/helpers/args.ts +63 -0
  58. package/src/core/helpers/check.ts +71 -0
  59. package/src/core/helpers/compact.ts +18 -0
  60. package/src/core/helpers/deunionize.ts +26 -0
  61. package/src/core/helpers/formatting.ts +119 -0
  62. package/src/core/helpers/util.ts +96 -0
  63. package/src/core/network/client.ts +396 -0
  64. package/src/core/network/error.ts +29 -0
  65. package/src/core/network/multipart-stream.ts +45 -0
  66. package/src/core/network/polling.ts +94 -0
  67. package/src/core/network/webhook.ts +58 -0
  68. package/src/core/types/typegram.ts +54 -0
  69. package/src/filters.ts +109 -0
  70. package/src/format.ts +110 -0
  71. package/src/future.ts +213 -0
  72. package/src/index.ts +17 -0
  73. package/src/input.ts +59 -0
  74. package/src/markup.ts +142 -0
  75. package/src/middleware.ts +24 -0
  76. package/src/reactions.ts +118 -0
  77. package/src/router.ts +55 -0
  78. package/src/scenes/base.ts +52 -0
  79. package/src/scenes/context.ts +136 -0
  80. package/src/scenes/index.ts +21 -0
  81. package/src/scenes/stage.ts +71 -0
  82. package/src/scenes/wizard/context.ts +58 -0
  83. package/src/scenes/wizard/index.ts +63 -0
  84. package/src/scenes.ts +1 -0
  85. package/src/session.ts +204 -0
  86. package/src/telegraf.ts +354 -0
  87. package/src/telegram-types.ts +219 -0
  88. package/src/telegram.ts +1635 -0
  89. package/src/types.ts +2 -0
  90. package/src/utils.ts +1 -0
  91. package/types.d.ts +1 -0
  92. package/types.js +1 -0
  93. package/typings/button.d.ts +36 -0
  94. package/typings/button.d.ts.map +1 -0
  95. package/typings/composer.d.ts +227 -0
  96. package/typings/composer.d.ts.map +1 -0
  97. package/typings/context.d.ts +655 -0
  98. package/typings/context.d.ts.map +1 -0
  99. package/typings/core/helpers/args.d.ts +11 -0
  100. package/typings/core/helpers/args.d.ts.map +1 -0
  101. package/typings/core/helpers/check.d.ts +56 -0
  102. package/typings/core/helpers/check.d.ts.map +1 -0
  103. package/typings/core/helpers/compact.d.ts +4 -0
  104. package/typings/core/helpers/compact.d.ts.map +1 -0
  105. package/typings/core/helpers/deunionize.d.ts +18 -0
  106. package/typings/core/helpers/deunionize.d.ts.map +1 -0
  107. package/typings/core/helpers/formatting.d.ts +30 -0
  108. package/typings/core/helpers/formatting.d.ts.map +1 -0
  109. package/typings/core/helpers/util.d.ts +26 -0
  110. package/typings/core/helpers/util.d.ts.map +1 -0
  111. package/typings/core/network/client.d.ts +53 -0
  112. package/typings/core/network/client.d.ts.map +1 -0
  113. package/typings/core/network/error.d.ts +16 -0
  114. package/typings/core/network/error.d.ts.map +1 -0
  115. package/typings/core/network/multipart-stream.d.ts +16 -0
  116. package/typings/core/network/multipart-stream.d.ts.map +1 -0
  117. package/typings/core/network/polling.d.ts +16 -0
  118. package/typings/core/network/polling.d.ts.map +1 -0
  119. package/typings/core/network/webhook.d.ts +6 -0
  120. package/typings/core/network/webhook.d.ts.map +1 -0
  121. package/typings/core/types/typegram.d.ts +42 -0
  122. package/typings/core/types/typegram.d.ts.map +1 -0
  123. package/typings/filters.d.ts +18 -0
  124. package/typings/filters.d.ts.map +1 -0
  125. package/typings/format.d.ts +22 -0
  126. package/typings/format.d.ts.map +1 -0
  127. package/typings/future.d.ts +12 -0
  128. package/typings/future.d.ts.map +1 -0
  129. package/typings/index.d.ts +15 -0
  130. package/typings/index.d.ts.map +1 -0
  131. package/typings/input.d.ts +50 -0
  132. package/typings/input.d.ts.map +1 -0
  133. package/typings/markup.d.ts +27 -0
  134. package/typings/markup.d.ts.map +1 -0
  135. package/typings/middleware.d.ts +8 -0
  136. package/typings/middleware.d.ts.map +1 -0
  137. package/typings/reactions.d.ts +32 -0
  138. package/typings/reactions.d.ts.map +1 -0
  139. package/typings/router.d.ts +21 -0
  140. package/typings/router.d.ts.map +1 -0
  141. package/typings/scenes/base.d.ts +22 -0
  142. package/typings/scenes/base.d.ts.map +1 -0
  143. package/typings/scenes/context.d.ts +36 -0
  144. package/typings/scenes/context.d.ts.map +1 -0
  145. package/typings/scenes/index.d.ts +11 -0
  146. package/typings/scenes/index.d.ts.map +1 -0
  147. package/typings/scenes/stage.d.ts +24 -0
  148. package/typings/scenes/stage.d.ts.map +1 -0
  149. package/typings/scenes/wizard/context.d.ts +29 -0
  150. package/typings/scenes/wizard/context.d.ts.map +1 -0
  151. package/typings/scenes/wizard/index.d.ts +16 -0
  152. package/typings/scenes/wizard/index.d.ts.map +1 -0
  153. package/typings/scenes.d.ts +2 -0
  154. package/typings/scenes.d.ts.map +1 -0
  155. package/typings/session.d.ts +55 -0
  156. package/typings/session.d.ts.map +1 -0
  157. package/typings/telegraf.d.ts +115 -0
  158. package/typings/telegraf.d.ts.map +1 -0
  159. package/typings/telegram-types.d.ts +117 -0
  160. package/typings/telegram-types.d.ts.map +1 -0
  161. package/typings/telegram.d.ts +675 -0
  162. package/typings/telegram.d.ts.map +1 -0
  163. package/typings/types.d.ts +3 -0
  164. package/typings/types.d.ts.map +1 -0
  165. package/typings/utils.d.ts +2 -0
  166. package/typings/utils.d.ts.map +1 -0
  167. package/utils.d.ts +1 -0
  168. package/utils.js +1 -0
package/src/session.ts ADDED
@@ -0,0 +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
+ }
@@ -0,0 +1,354 @@
1
+ import * as crypto from 'crypto'
2
+ import * as http from 'http'
3
+ import * as https from 'https'
4
+ import * as tg from './core/types/typegram'
5
+ import * as tt from './telegram-types'
6
+ import { Composer } from './composer'
7
+ import { MaybePromise } from './core/helpers/util'
8
+ import ApiClient from './core/network/client'
9
+ import { compactOptions } from './core/helpers/compact'
10
+ import Context from './context'
11
+ import d from 'debug'
12
+ import generateCallback from './core/network/webhook'
13
+ import { Polling } from './core/network/polling'
14
+ import pTimeout from 'p-timeout'
15
+ import Telegram from './telegram'
16
+ import { TlsOptions } from 'tls'
17
+ import { URL } from 'url'
18
+ import safeCompare = require('safe-compare')
19
+ const debug = d('telegraf:main')
20
+
21
+ const DEFAULT_OPTIONS: Telegraf.Options<Context> = {
22
+ telegram: {},
23
+ handlerTimeout: 90_000, // 90s in ms
24
+ contextType: Context,
25
+ }
26
+
27
+ function always<T>(x: T) {
28
+ return () => x
29
+ }
30
+
31
+ const anoop = always(Promise.resolve())
32
+
33
+ export namespace Telegraf {
34
+ export interface Options<TContext extends Context> {
35
+ contextType: new (
36
+ ...args: ConstructorParameters<typeof Context>
37
+ ) => TContext
38
+ handlerTimeout: number
39
+ telegram?: Partial<ApiClient.Options>
40
+ }
41
+
42
+ export interface LaunchOptions {
43
+ dropPendingUpdates?: boolean
44
+ /** List the types of updates you want your bot to receive */
45
+ allowedUpdates?: tt.UpdateType[]
46
+ /** Configuration options for when the bot is run via webhooks */
47
+ webhook?: {
48
+ /** Public domain for webhook. */
49
+ domain: string
50
+
51
+ /**
52
+ * Webhook url path; will be automatically generated if not specified
53
+ * @deprecated Pass `path` instead
54
+ * */
55
+ hookPath?: string
56
+
57
+ /** Webhook url path; will be automatically generated if not specified */
58
+ path?: string
59
+
60
+ host?: string
61
+ port?: number
62
+
63
+ /** The fixed IP address which will be used to send webhook requests instead of the IP address resolved through DNS */
64
+ ipAddress?: string
65
+
66
+ /**
67
+ * Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery, 1-100. Defaults to 40.
68
+ * Use lower values to limit the load on your bot's server, and higher values to increase your bot's throughput.
69
+ */
70
+ maxConnections?: number
71
+
72
+ /** TLS server options. Omit to use http. */
73
+ tlsOptions?: TlsOptions
74
+
75
+ /**
76
+ * A secret token to be sent in a header `“X-Telegram-Bot-Api-Secret-Token”` in every webhook request.
77
+ * 1-256 characters. Only characters `A-Z`, `a-z`, `0-9`, `_` and `-` are allowed.
78
+ * The header is useful to ensure that the request comes from a webhook set by you.
79
+ */
80
+ secretToken?: string
81
+
82
+ /**
83
+ * Upload your public key certificate so that the root certificate in use can be checked.
84
+ * See [self-signed guide](https://core.telegram.org/bots/self-signed) for details.
85
+ */
86
+ certificate?: tg.InputFile
87
+
88
+ cb?: http.RequestListener
89
+ }
90
+ }
91
+ }
92
+
93
+ const TOKEN_HEADER = 'x-telegram-bot-api-secret-token'
94
+
95
+ export class Telegraf<C extends Context = Context> extends Composer<C> {
96
+ private readonly options: Telegraf.Options<C>
97
+ private webhookServer?: http.Server | https.Server
98
+ private polling?: Polling
99
+ /** Set manually to avoid implicit `getMe` call in `launch` or `webhookCallback` */
100
+ public botInfo?: tg.UserFromGetMe
101
+ public telegram: Telegram
102
+ readonly context: Partial<C> = {}
103
+
104
+ /** Assign to this to customise the webhook filter middleware.
105
+ * `{ path, secretToken }` will be bound to this rather than the Telegraf instance.
106
+ * Remember to assign a regular function and not an arrow function so it's bindable.
107
+ */
108
+ public webhookFilter = function (
109
+ // NOTE: this function is assigned to a variable instead of being a method to signify that it's assignable
110
+ // NOTE: the `this` binding is so custom impls don't need to double wrap
111
+ this: {
112
+ /** @deprecated Use path instead */
113
+ hookPath: string
114
+ path: string
115
+ secretToken?: string
116
+ },
117
+ req: http.IncomingMessage
118
+ ) {
119
+ const debug = d('telegraf:webhook')
120
+
121
+ if (req.method === 'POST') {
122
+ if (safeCompare(this.path, req.url as string)) {
123
+ // no need to check if secret_token was not set
124
+ if (!this.secretToken) return true
125
+ else {
126
+ const token = req.headers[TOKEN_HEADER] as string
127
+ if (safeCompare(this.secretToken, token)) return true
128
+ else debug('Secret token does not match:', token, this.secretToken)
129
+ }
130
+ } else debug('Path does not match:', req.url, this.path)
131
+ } else debug('Unexpected request method, not POST. Received:', req.method)
132
+
133
+ return false
134
+ }
135
+
136
+ private handleError = (err: unknown, ctx: C): MaybePromise<void> => {
137
+ // set exit code to emulate `warn-with-error-code` behavior of
138
+ // https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode
139
+ // to prevent a clean exit despite an error being thrown
140
+ process.exitCode = 1
141
+ console.error('Unhandled error while processing', ctx.update)
142
+ throw err
143
+ }
144
+
145
+ constructor(token: string, options?: Partial<Telegraf.Options<C>>) {
146
+ super()
147
+ // @ts-expect-error Trust me, TS
148
+ this.options = {
149
+ ...DEFAULT_OPTIONS,
150
+ ...compactOptions(options),
151
+ }
152
+ this.telegram = new Telegram(token, this.options.telegram)
153
+ debug('Created a `Telegraf` instance')
154
+ }
155
+
156
+ private get token() {
157
+ return this.telegram.token
158
+ }
159
+
160
+ /** @deprecated use `ctx.telegram.webhookReply` */
161
+ set webhookReply(webhookReply: boolean) {
162
+ this.telegram.webhookReply = webhookReply
163
+ }
164
+
165
+ /** @deprecated use `ctx.telegram.webhookReply` */
166
+ get webhookReply() {
167
+ return this.telegram.webhookReply
168
+ }
169
+
170
+ /**
171
+ * _Override_ error handling
172
+ */
173
+ catch(handler: (err: unknown, ctx: C) => MaybePromise<void>) {
174
+ this.handleError = handler
175
+ return this
176
+ }
177
+
178
+ /**
179
+ * You must call `bot.telegram.setWebhook` for this to work.
180
+ * You should probably use {@link Telegraf.createWebhook} instead.
181
+ */
182
+ webhookCallback(path = '/', opts: { secretToken?: string } = {}) {
183
+ const { secretToken } = opts
184
+ return generateCallback(
185
+ this.webhookFilter.bind({ hookPath: path, path, secretToken }),
186
+ (update: tg.Update, res: http.ServerResponse) =>
187
+ this.handleUpdate(update, res)
188
+ )
189
+ }
190
+
191
+ private getDomainOpts(opts: { domain: string; path?: string }) {
192
+ const protocol =
193
+ opts.domain.startsWith('https://') || opts.domain.startsWith('http://')
194
+
195
+ if (protocol)
196
+ debug(
197
+ 'Unexpected protocol in domain, telegraf will use https:',
198
+ opts.domain
199
+ )
200
+
201
+ const domain = protocol ? new URL(opts.domain).host : opts.domain
202
+ const path = opts.path ?? `/telegraf/${this.secretPathComponent()}`
203
+ const url = `https://${domain}${path}`
204
+
205
+ return { domain, path, url }
206
+ }
207
+
208
+ /**
209
+ * Specify a url to receive incoming updates via webhook.
210
+ * Returns an Express-style middleware you can pass to app.use()
211
+ */
212
+ async createWebhook(
213
+ opts: { domain: string; path?: string } & tt.ExtraSetWebhook
214
+ ) {
215
+ const { domain, path, ...extra } = opts
216
+
217
+ const domainOpts = this.getDomainOpts({ domain, path })
218
+
219
+ await this.telegram.setWebhook(domainOpts.url, extra)
220
+ debug(`Webhook set to ${domainOpts.url}`)
221
+
222
+ return this.webhookCallback(domainOpts.path, {
223
+ secretToken: extra.secret_token,
224
+ })
225
+ }
226
+
227
+ private startPolling(allowedUpdates: tt.UpdateType[] = []) {
228
+ this.polling = new Polling(this.telegram, allowedUpdates)
229
+ return this.polling.loop(async (update) => {
230
+ await this.handleUpdate(update)
231
+ })
232
+ }
233
+
234
+ private startWebhook(
235
+ path: string,
236
+ tlsOptions?: TlsOptions,
237
+ port?: number,
238
+ host?: string,
239
+ cb?: http.RequestListener,
240
+ secretToken?: string
241
+ ) {
242
+ const webhookCb = this.webhookCallback(path, { secretToken })
243
+ const callback: http.RequestListener =
244
+ typeof cb === 'function'
245
+ ? (req, res) => webhookCb(req, res, () => cb(req, res))
246
+ : webhookCb
247
+ this.webhookServer =
248
+ tlsOptions != null
249
+ ? https.createServer(tlsOptions, callback)
250
+ : http.createServer(callback)
251
+ this.webhookServer.listen(port, host, () => {
252
+ debug('Webhook listening on port: %s', port)
253
+ })
254
+ return this
255
+ }
256
+
257
+ secretPathComponent() {
258
+ return crypto
259
+ .createHash('sha3-256')
260
+ .update(this.token)
261
+ .update(process.version) // salt
262
+ .digest('hex')
263
+ }
264
+
265
+ async launch(onLaunch?: () => void): Promise<void>
266
+ async launch(
267
+ config: Telegraf.LaunchOptions,
268
+ onLaunch?: () => void
269
+ ): Promise<void>
270
+ /**
271
+ * @see https://github.com/telegraf/telegraf/discussions/1344#discussioncomment-335700
272
+ */
273
+ async launch(
274
+ config: Telegraf.LaunchOptions | (() => void) = {},
275
+ /** @experimental */
276
+ onLaunch?: () => void
277
+ ) {
278
+ const [cfg, onMe] =
279
+ typeof config === 'function' ? [{}, config] : [config, onLaunch]
280
+ const drop_pending_updates = cfg.dropPendingUpdates
281
+ const allowed_updates = cfg.allowedUpdates
282
+ const webhook = cfg.webhook
283
+
284
+ debug('Connecting to Telegram')
285
+ this.botInfo ??= await this.telegram.getMe()
286
+ onMe?.()
287
+ debug(`Launching @${this.botInfo.username}`)
288
+
289
+ if (webhook === undefined) {
290
+ await this.telegram.deleteWebhook({ drop_pending_updates })
291
+ debug('Bot started with long polling')
292
+ await this.startPolling(allowed_updates)
293
+ return
294
+ }
295
+
296
+ const domainOpts = this.getDomainOpts({
297
+ domain: webhook.domain,
298
+ path: webhook.path ?? webhook.hookPath,
299
+ })
300
+
301
+ const { tlsOptions, port, host, cb, secretToken } = webhook
302
+
303
+ this.startWebhook(domainOpts.path, tlsOptions, port, host, cb, secretToken)
304
+
305
+ await this.telegram.setWebhook(domainOpts.url, {
306
+ drop_pending_updates: drop_pending_updates,
307
+ allowed_updates: allowed_updates,
308
+ ip_address: webhook.ipAddress,
309
+ max_connections: webhook.maxConnections,
310
+ secret_token: webhook.secretToken,
311
+ certificate: webhook.certificate,
312
+ })
313
+
314
+ debug(`Bot started with webhook @ ${domainOpts.url}`)
315
+ }
316
+
317
+ stop(reason = 'unspecified') {
318
+ debug('Stopping bot... Reason:', reason)
319
+ // https://github.com/telegraf/telegraf/pull/1224#issuecomment-742693770
320
+ if (this.polling === undefined && this.webhookServer === undefined) {
321
+ throw new Error('Bot is not running!')
322
+ }
323
+ this.webhookServer?.close()
324
+ this.polling?.stop()
325
+ }
326
+
327
+ private botInfoCall?: Promise<tg.UserFromGetMe>
328
+ async handleUpdate(update: tg.Update, webhookResponse?: http.ServerResponse) {
329
+ this.botInfo ??=
330
+ (debug(
331
+ 'Update %d is waiting for `botInfo` to be initialized',
332
+ update.update_id
333
+ ),
334
+ await (this.botInfoCall ??= this.telegram.getMe()))
335
+ debug('Processing update', update.update_id)
336
+ const tg = new Telegram(this.token, this.telegram.options, webhookResponse)
337
+ const TelegrafContext = this.options.contextType
338
+ const ctx = new TelegrafContext(update, tg, this.botInfo)
339
+ Object.assign(ctx, this.context)
340
+ try {
341
+ await pTimeout(
342
+ Promise.resolve(this.middleware()(ctx, anoop)),
343
+ this.options.handlerTimeout
344
+ )
345
+ } catch (err) {
346
+ return await this.handleError(err, ctx)
347
+ } finally {
348
+ if (webhookResponse?.writableEnded === false) {
349
+ webhookResponse.end()
350
+ }
351
+ debug('Finished processing update', update.update_id)
352
+ }
353
+ }
354
+ }