@tgify/tgify 0.1.5 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -17
- package/lib/button.js +20 -19
- package/lib/core/types/typegram.js +10 -10
- package/lib/index.js +3 -1
- package/lib/reactions.js +3 -1
- package/lib/telegraf.js +2 -253
- package/lib/telegram.js +7 -5
- package/lib/tgify.js +254 -0
- package/package.json +2 -2
- package/src/button.ts +48 -36
- package/src/composer.ts +13 -13
- package/src/context.ts +22 -22
- package/src/core/helpers/formatting.ts +2 -3
- package/src/core/types/typegram.ts +11 -11
- package/src/filters.ts +60 -60
- package/src/format.ts +4 -4
- package/src/future.ts +1 -1
- package/src/index.ts +1 -0
- package/src/markup.ts +5 -5
- package/src/reactions.ts +3 -1
- package/src/session.ts +2 -2
- package/src/telegraf.ts +1 -354
- package/src/telegram-types.ts +5 -5
- package/src/telegram.ts +12 -10
- package/src/tgify.ts +351 -0
- package/typings/button.d.ts +17 -17
- package/typings/button.d.ts.map +1 -1
- package/typings/context.d.ts +49 -49
- package/typings/context.d.ts.map +1 -1
- package/typings/core/helpers/formatting.d.ts +1 -1
- package/typings/core/helpers/formatting.d.ts.map +1 -1
- package/typings/core/types/typegram.d.ts +11 -11
- package/typings/core/types/typegram.d.ts.map +1 -1
- package/typings/filters.d.ts +1 -1
- package/typings/filters.d.ts.map +1 -1
- package/typings/format.d.ts +1 -1
- package/typings/format.d.ts.map +1 -1
- package/typings/index.d.ts +1 -0
- package/typings/index.d.ts.map +1 -1
- package/typings/markup.d.ts.map +1 -1
- package/typings/reactions.d.ts.map +1 -1
- package/typings/telegraf.d.ts +1 -114
- package/typings/telegraf.d.ts.map +1 -1
- package/typings/telegram-types.d.ts.map +1 -1
- package/typings/telegram.d.ts +31 -30
- package/typings/telegram.d.ts.map +1 -1
- package/typings/tgify.d.ts +118 -0
- package/typings/tgify.d.ts.map +1 -0
package/src/filters.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type {
|
|
|
3
3
|
CommonMessageBundle,
|
|
4
4
|
Message,
|
|
5
5
|
Update,
|
|
6
|
-
} from '@
|
|
6
|
+
} from '@tgify/types'
|
|
7
7
|
import { DistinctKeys, KeyedDistinct, Guarded } from './core/helpers/util'
|
|
8
8
|
|
|
9
9
|
export type Filter<U extends Update> = (update: Update) => update is U
|
|
@@ -15,78 +15,78 @@ export type AllGuarded<Fs extends Filter<Update>[]> = Fs extends [
|
|
|
15
15
|
...infer B,
|
|
16
16
|
]
|
|
17
17
|
? B extends []
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
? Guarded<A>
|
|
19
|
+
: // TS doesn't know otherwise that B is Filter[]
|
|
20
|
+
B extends Filter<Update>[]
|
|
21
|
+
? Guarded<A> & AllGuarded<B>
|
|
22
|
+
: never
|
|
23
23
|
: never
|
|
24
24
|
|
|
25
25
|
export const message =
|
|
26
26
|
<Ks extends DistinctKeys<Message>[]>(...keys: Ks) =>
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
27
|
+
(
|
|
28
|
+
update: Update
|
|
29
|
+
): update is Update.MessageUpdate<KeyedDistinct<Message, Ks[number]>> => {
|
|
30
|
+
if (!('message' in update)) return false
|
|
31
|
+
for (const key of keys) {
|
|
32
|
+
if (!(key in update.message)) return false
|
|
33
|
+
}
|
|
34
|
+
return true
|
|
33
35
|
}
|
|
34
|
-
return true
|
|
35
|
-
}
|
|
36
36
|
|
|
37
37
|
export const editedMessage =
|
|
38
38
|
<Ks extends DistinctKeys<CommonMessageBundle>[]>(...keys: Ks) =>
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
39
|
+
(
|
|
40
|
+
update: Update
|
|
41
|
+
): update is Update.EditedMessageUpdate<
|
|
42
|
+
KeyedDistinct<CommonMessageBundle, Ks[number]>
|
|
43
|
+
> => {
|
|
44
|
+
if (!('edited_message' in update)) return false
|
|
45
|
+
for (const key of keys) {
|
|
46
|
+
if (!(key in update.edited_message)) return false
|
|
47
|
+
}
|
|
48
|
+
return true
|
|
47
49
|
}
|
|
48
|
-
return true
|
|
49
|
-
}
|
|
50
50
|
|
|
51
51
|
export const channelPost =
|
|
52
52
|
<Ks extends DistinctKeys<Message>[]>(...keys: Ks) =>
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
53
|
+
(
|
|
54
|
+
update: Update
|
|
55
|
+
): update is Update.ChannelPostUpdate<KeyedDistinct<Message, Ks[number]>> => {
|
|
56
|
+
if (!('channel_post' in update)) return false
|
|
57
|
+
for (const key of keys) {
|
|
58
|
+
if (!(key in update.channel_post)) return false
|
|
59
|
+
}
|
|
60
|
+
return true
|
|
59
61
|
}
|
|
60
|
-
return true
|
|
61
|
-
}
|
|
62
62
|
|
|
63
63
|
export const editedChannelPost =
|
|
64
64
|
<Ks extends DistinctKeys<CommonMessageBundle>[]>(...keys: Ks) =>
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
65
|
+
(
|
|
66
|
+
update: Update
|
|
67
|
+
): update is Update.EditedChannelPostUpdate<
|
|
68
|
+
KeyedDistinct<CommonMessageBundle, Ks[number]>
|
|
69
|
+
> => {
|
|
70
|
+
if (!('edited_channel_post' in update)) return false
|
|
71
|
+
for (const key of keys) {
|
|
72
|
+
if (!(key in update.edited_channel_post)) return false
|
|
73
|
+
}
|
|
74
|
+
return true
|
|
73
75
|
}
|
|
74
|
-
return true
|
|
75
|
-
}
|
|
76
76
|
|
|
77
77
|
export const callbackQuery =
|
|
78
78
|
<Ks extends DistinctKeys<CallbackQuery>[]>(...keys: Ks) =>
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
79
|
+
(
|
|
80
|
+
update: Update
|
|
81
|
+
): update is Update.CallbackQueryUpdate<
|
|
82
|
+
KeyedDistinct<CallbackQuery, Ks[number]>
|
|
83
|
+
> => {
|
|
84
|
+
if (!('callback_query' in update)) return false
|
|
85
|
+
for (const key of keys) {
|
|
86
|
+
if (!(key in update.callback_query)) return false
|
|
87
|
+
}
|
|
88
|
+
return true
|
|
87
89
|
}
|
|
88
|
-
return true
|
|
89
|
-
}
|
|
90
90
|
|
|
91
91
|
/** Any of the provided filters must match */
|
|
92
92
|
export const anyOf =
|
|
@@ -95,15 +95,15 @@ export const anyOf =
|
|
|
95
95
|
[UIdx in keyof Us]: Filter<Us[UIdx]>
|
|
96
96
|
}
|
|
97
97
|
) =>
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
98
|
+
(update: Update): update is Us[number] => {
|
|
99
|
+
for (const filter of filters) if (filter(update)) return true
|
|
100
|
+
return false
|
|
101
|
+
}
|
|
102
102
|
|
|
103
103
|
/** All of the provided filters must match */
|
|
104
104
|
export const allOf =
|
|
105
105
|
<U extends Update, Fs extends Filter<U>[]>(...filters: Fs) =>
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
106
|
+
(update: Update): update is AllGuarded<Fs> => {
|
|
107
|
+
for (const filter of filters) if (!filter(update)) return false
|
|
108
|
+
return true
|
|
109
|
+
}
|
package/src/format.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { User } from '@
|
|
1
|
+
import { User } from '@tgify/types'
|
|
2
2
|
import {
|
|
3
3
|
FmtString,
|
|
4
4
|
createFmt,
|
|
@@ -105,6 +105,6 @@ export const mention = (
|
|
|
105
105
|
typeof user === 'number'
|
|
106
106
|
? link(name, 'tg://user?id=' + user)
|
|
107
107
|
: (linkOrMention(name, {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
108
|
+
type: 'text_mention',
|
|
109
|
+
user,
|
|
110
|
+
}) as FmtString<'text_mention'>)
|
package/src/future.ts
CHANGED
package/src/index.ts
CHANGED
package/src/markup.ts
CHANGED
|
@@ -14,10 +14,10 @@ type HideableIKBtn = Hideable<InlineKeyboardButton>
|
|
|
14
14
|
|
|
15
15
|
export class Markup<
|
|
16
16
|
T extends
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
| InlineKeyboardMarkup
|
|
18
|
+
| ReplyKeyboardMarkup
|
|
19
|
+
| ReplyKeyboardRemove
|
|
20
|
+
| ForceReply,
|
|
21
21
|
> {
|
|
22
22
|
constructor(readonly reply_markup: T) {}
|
|
23
23
|
|
|
@@ -124,7 +124,7 @@ function buildKeyboard<B extends HideableKBtn | HideableIKBtn>(
|
|
|
124
124
|
options.wrap !== undefined
|
|
125
125
|
? options.wrap
|
|
126
126
|
: (_btn: B, _index: number, currentRow: B[]) =>
|
|
127
|
-
|
|
127
|
+
currentRow.length >= options.columns
|
|
128
128
|
let currentRow: B[] = []
|
|
129
129
|
let index = 0
|
|
130
130
|
for (const btn of buttons.filter((button) => !button.hide)) {
|
package/src/reactions.ts
CHANGED
|
@@ -12,9 +12,11 @@ export type Reaction =
|
|
|
12
12
|
type ReactionCtx = { update: Partial<tg.Update.MessageReactionUpdate> }
|
|
13
13
|
|
|
14
14
|
const inspectReaction = (reaction: tg.ReactionType) => {
|
|
15
|
+
if (reaction.type === 'emoji')
|
|
16
|
+
return reaction.emoji
|
|
15
17
|
if (reaction.type === 'custom_emoji')
|
|
16
18
|
return `Custom(${reaction.custom_emoji_id})`
|
|
17
|
-
else return
|
|
19
|
+
else return 'paid()'
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
export class ReactionList {
|
package/src/session.ts
CHANGED
|
@@ -49,8 +49,8 @@ export function session<
|
|
|
49
49
|
S extends NonNullable<C[P]>,
|
|
50
50
|
C extends Context & { [key in P]?: C[P] },
|
|
51
51
|
P extends (ExclusiveKeys<C, Context> & string) | 'session' = 'session',
|
|
52
|
-
|
|
53
|
-
|
|
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
54
|
>(options?: SessionOptions<S, C, P>): MiddlewareFn<C> {
|
|
55
55
|
const prop = options?.property ?? ('session' as P)
|
|
56
56
|
const getSessionKey = options?.getSessionKey ?? defaultGetSessionKey
|
package/src/telegraf.ts
CHANGED
|
@@ -1,354 +1 @@
|
|
|
1
|
-
|
|
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
|
-
}
|
|
1
|
+
export { Tgify as Telegraf } from './tgify'
|
package/src/telegram-types.ts
CHANGED
|
@@ -159,14 +159,14 @@ export type UpdateType = Exclude<UnionKeys<Update>, keyof Update>
|
|
|
159
159
|
export type MessageSubType =
|
|
160
160
|
| 'forward_date'
|
|
161
161
|
| Exclude<
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
162
|
+
UnionKeys<Message>,
|
|
163
|
+
keyof Message.CaptionableMessage | 'entities' | 'media_group_id'
|
|
164
|
+
>
|
|
165
165
|
|
|
166
166
|
type ExtractPartial<T extends object, U extends object> = T extends unknown
|
|
167
167
|
? Required<T> extends U
|
|
168
|
-
|
|
169
|
-
|
|
168
|
+
? T
|
|
169
|
+
: never
|
|
170
170
|
: never
|
|
171
171
|
|
|
172
172
|
/**
|
package/src/telegram.ts
CHANGED
|
@@ -42,9 +42,8 @@ export class Telegram extends ApiClient {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
return new URL(
|
|
45
|
-
`./file/${this.options.apiMode}${this.token}${
|
|
46
|
-
|
|
47
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
45
|
+
`./file/${this.options.apiMode}${this.token}${this.options.testEnv ? '/test' : ''
|
|
46
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
48
47
|
}/${fileId.file_path!}`,
|
|
49
48
|
this.options.apiRoot
|
|
50
49
|
)
|
|
@@ -460,7 +459,7 @@ export class Telegram extends ApiClient {
|
|
|
460
459
|
sendPoll(
|
|
461
460
|
chatId: number | string,
|
|
462
461
|
question: string,
|
|
463
|
-
options: readonly
|
|
462
|
+
options: readonly tg.InputPollOption[],
|
|
464
463
|
extra?: tt.ExtraPoll
|
|
465
464
|
) {
|
|
466
465
|
return this.callApi('sendPoll', {
|
|
@@ -481,7 +480,7 @@ export class Telegram extends ApiClient {
|
|
|
481
480
|
sendQuiz(
|
|
482
481
|
chatId: number | string,
|
|
483
482
|
question: string,
|
|
484
|
-
options: readonly
|
|
483
|
+
options: readonly tg.InputPollOption[],
|
|
485
484
|
extra?: tt.ExtraPoll
|
|
486
485
|
) {
|
|
487
486
|
return this.callApi('sendPoll', {
|
|
@@ -941,9 +940,9 @@ export class Telegram extends ApiClient {
|
|
|
941
940
|
markup: tg.InlineKeyboardMarkup | undefined
|
|
942
941
|
) {
|
|
943
942
|
return this.callApi('editMessageReplyMarkup', {
|
|
944
|
-
chat_id: chatId,
|
|
945
|
-
message_id: messageId,
|
|
946
|
-
inline_message_id: inlineMessageId,
|
|
943
|
+
...(chatId !== undefined && { chat_id: chatId }),
|
|
944
|
+
...(messageId !== undefined && { message_id: messageId }),
|
|
945
|
+
...(inlineMessageId !== undefined && { inline_message_id: inlineMessageId }),
|
|
947
946
|
reply_markup: markup,
|
|
948
947
|
})
|
|
949
948
|
}
|
|
@@ -1311,16 +1310,19 @@ export class Telegram extends ApiClient {
|
|
|
1311
1310
|
* HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using
|
|
1312
1311
|
* Input helpers. Animated and video sticker set thumbnails can't be uploaded via HTTP URL.
|
|
1313
1312
|
* If omitted, then the thumbnail is dropped and the first sticker is used as the thumbnail.
|
|
1313
|
+
* @param format Format of the sticker set thumbnail; must be one of "static", "animated", or "video"
|
|
1314
1314
|
*/
|
|
1315
1315
|
setStickerSetThumbnail(
|
|
1316
1316
|
name: string,
|
|
1317
1317
|
userId: number,
|
|
1318
|
-
thumbnail?: tg.Opts<'setStickerSetThumbnail'>['thumbnail']
|
|
1318
|
+
thumbnail?: tg.Opts<'setStickerSetThumbnail'>['thumbnail'],
|
|
1319
|
+
format: tg.Opts<'setStickerSetThumbnail'>['format'] = 'static'
|
|
1319
1320
|
) {
|
|
1320
1321
|
return this.callApi('setStickerSetThumbnail', {
|
|
1321
1322
|
name,
|
|
1322
|
-
|
|
1323
|
+
format,
|
|
1323
1324
|
thumbnail,
|
|
1325
|
+
user_id: userId,
|
|
1324
1326
|
})
|
|
1325
1327
|
}
|
|
1326
1328
|
|