@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.
- package/LICENSE +23 -0
- package/README.md +356 -0
- package/filters.d.ts +1 -0
- package/filters.js +1 -0
- package/format.d.ts +1 -0
- package/format.js +1 -0
- package/future.d.ts +1 -0
- package/future.js +1 -0
- package/lib/button.js +100 -0
- package/lib/cli.mjs +105 -0
- package/lib/composer.js +582 -0
- package/lib/context.js +1219 -0
- package/lib/core/helpers/args.js +57 -0
- package/lib/core/helpers/check.js +55 -0
- package/lib/core/helpers/compact.js +16 -0
- package/lib/core/helpers/deunionize.js +12 -0
- package/lib/core/helpers/formatting.js +91 -0
- package/lib/core/helpers/util.js +50 -0
- package/lib/core/network/client.js +330 -0
- package/lib/core/network/error.js +21 -0
- package/lib/core/network/multipart-stream.js +71 -0
- package/lib/core/network/polling.js +87 -0
- package/lib/core/network/webhook.js +54 -0
- package/lib/core/types/typegram.js +27 -0
- package/lib/filters.js +69 -0
- package/lib/format.js +38 -0
- package/lib/future.js +149 -0
- package/lib/index.js +58 -0
- package/lib/input.js +61 -0
- package/lib/markup.js +121 -0
- package/lib/middleware.js +2 -0
- package/lib/reactions.js +84 -0
- package/lib/router.js +46 -0
- package/lib/scenes/base.js +39 -0
- package/lib/scenes/context.js +104 -0
- package/lib/scenes/index.js +21 -0
- package/lib/scenes/stage.js +49 -0
- package/lib/scenes/wizard/context.js +31 -0
- package/lib/scenes/wizard/index.js +45 -0
- package/lib/scenes.js +17 -0
- package/lib/session.js +166 -0
- package/lib/telegraf.js +256 -0
- package/lib/telegram-types.js +6 -0
- package/lib/telegram.js +1240 -0
- package/lib/types.js +2 -0
- package/lib/utils.js +5 -0
- package/markup.d.ts +1 -0
- package/markup.js +1 -0
- package/package.json +140 -0
- package/scenes.d.ts +1 -0
- package/scenes.js +1 -0
- package/session.d.ts +1 -0
- package/session.js +1 -0
- package/src/button.ts +182 -0
- package/src/composer.ts +1008 -0
- package/src/context.ts +1661 -0
- package/src/core/helpers/args.ts +63 -0
- package/src/core/helpers/check.ts +71 -0
- package/src/core/helpers/compact.ts +18 -0
- package/src/core/helpers/deunionize.ts +26 -0
- package/src/core/helpers/formatting.ts +119 -0
- package/src/core/helpers/util.ts +96 -0
- package/src/core/network/client.ts +396 -0
- package/src/core/network/error.ts +29 -0
- package/src/core/network/multipart-stream.ts +45 -0
- package/src/core/network/polling.ts +94 -0
- package/src/core/network/webhook.ts +58 -0
- package/src/core/types/typegram.ts +54 -0
- package/src/filters.ts +109 -0
- package/src/format.ts +110 -0
- package/src/future.ts +213 -0
- package/src/index.ts +17 -0
- package/src/input.ts +59 -0
- package/src/markup.ts +142 -0
- package/src/middleware.ts +24 -0
- package/src/reactions.ts +118 -0
- package/src/router.ts +55 -0
- package/src/scenes/base.ts +52 -0
- package/src/scenes/context.ts +136 -0
- package/src/scenes/index.ts +21 -0
- package/src/scenes/stage.ts +71 -0
- package/src/scenes/wizard/context.ts +58 -0
- package/src/scenes/wizard/index.ts +63 -0
- package/src/scenes.ts +1 -0
- package/src/session.ts +204 -0
- package/src/telegraf.ts +354 -0
- package/src/telegram-types.ts +219 -0
- package/src/telegram.ts +1635 -0
- package/src/types.ts +2 -0
- package/src/utils.ts +1 -0
- package/types.d.ts +1 -0
- package/types.js +1 -0
- package/typings/button.d.ts +36 -0
- package/typings/button.d.ts.map +1 -0
- package/typings/composer.d.ts +227 -0
- package/typings/composer.d.ts.map +1 -0
- package/typings/context.d.ts +655 -0
- package/typings/context.d.ts.map +1 -0
- package/typings/core/helpers/args.d.ts +11 -0
- package/typings/core/helpers/args.d.ts.map +1 -0
- package/typings/core/helpers/check.d.ts +56 -0
- package/typings/core/helpers/check.d.ts.map +1 -0
- package/typings/core/helpers/compact.d.ts +4 -0
- package/typings/core/helpers/compact.d.ts.map +1 -0
- package/typings/core/helpers/deunionize.d.ts +18 -0
- package/typings/core/helpers/deunionize.d.ts.map +1 -0
- package/typings/core/helpers/formatting.d.ts +30 -0
- package/typings/core/helpers/formatting.d.ts.map +1 -0
- package/typings/core/helpers/util.d.ts +26 -0
- package/typings/core/helpers/util.d.ts.map +1 -0
- package/typings/core/network/client.d.ts +53 -0
- package/typings/core/network/client.d.ts.map +1 -0
- package/typings/core/network/error.d.ts +16 -0
- package/typings/core/network/error.d.ts.map +1 -0
- package/typings/core/network/multipart-stream.d.ts +16 -0
- package/typings/core/network/multipart-stream.d.ts.map +1 -0
- package/typings/core/network/polling.d.ts +16 -0
- package/typings/core/network/polling.d.ts.map +1 -0
- package/typings/core/network/webhook.d.ts +6 -0
- package/typings/core/network/webhook.d.ts.map +1 -0
- package/typings/core/types/typegram.d.ts +42 -0
- package/typings/core/types/typegram.d.ts.map +1 -0
- package/typings/filters.d.ts +18 -0
- package/typings/filters.d.ts.map +1 -0
- package/typings/format.d.ts +22 -0
- package/typings/format.d.ts.map +1 -0
- package/typings/future.d.ts +12 -0
- package/typings/future.d.ts.map +1 -0
- package/typings/index.d.ts +15 -0
- package/typings/index.d.ts.map +1 -0
- package/typings/input.d.ts +50 -0
- package/typings/input.d.ts.map +1 -0
- package/typings/markup.d.ts +27 -0
- package/typings/markup.d.ts.map +1 -0
- package/typings/middleware.d.ts +8 -0
- package/typings/middleware.d.ts.map +1 -0
- package/typings/reactions.d.ts +32 -0
- package/typings/reactions.d.ts.map +1 -0
- package/typings/router.d.ts +21 -0
- package/typings/router.d.ts.map +1 -0
- package/typings/scenes/base.d.ts +22 -0
- package/typings/scenes/base.d.ts.map +1 -0
- package/typings/scenes/context.d.ts +36 -0
- package/typings/scenes/context.d.ts.map +1 -0
- package/typings/scenes/index.d.ts +11 -0
- package/typings/scenes/index.d.ts.map +1 -0
- package/typings/scenes/stage.d.ts +24 -0
- package/typings/scenes/stage.d.ts.map +1 -0
- package/typings/scenes/wizard/context.d.ts +29 -0
- package/typings/scenes/wizard/context.d.ts.map +1 -0
- package/typings/scenes/wizard/index.d.ts +16 -0
- package/typings/scenes/wizard/index.d.ts.map +1 -0
- package/typings/scenes.d.ts +2 -0
- package/typings/scenes.d.ts.map +1 -0
- package/typings/session.d.ts +55 -0
- package/typings/session.d.ts.map +1 -0
- package/typings/telegraf.d.ts +115 -0
- package/typings/telegraf.d.ts.map +1 -0
- package/typings/telegram-types.d.ts +117 -0
- package/typings/telegram-types.d.ts.map +1 -0
- package/typings/telegram.d.ts +675 -0
- package/typings/telegram.d.ts.map +1 -0
- package/typings/types.d.ts +3 -0
- package/typings/types.d.ts.map +1 -0
- package/typings/utils.d.ts +2 -0
- package/typings/utils.d.ts.map +1 -0
- package/utils.d.ts +1 -0
- package/utils.js +1 -0
package/src/filters.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CallbackQuery,
|
|
3
|
+
CommonMessageBundle,
|
|
4
|
+
Message,
|
|
5
|
+
Update,
|
|
6
|
+
} from '@telegraf/types'
|
|
7
|
+
import { DistinctKeys, KeyedDistinct, Guarded } from './core/helpers/util'
|
|
8
|
+
|
|
9
|
+
export type Filter<U extends Update> = (update: Update) => update is U
|
|
10
|
+
|
|
11
|
+
export { Guarded }
|
|
12
|
+
|
|
13
|
+
export type AllGuarded<Fs extends Filter<Update>[]> = Fs extends [
|
|
14
|
+
infer A,
|
|
15
|
+
...infer B,
|
|
16
|
+
]
|
|
17
|
+
? B extends []
|
|
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
|
+
: never
|
|
24
|
+
|
|
25
|
+
export const message =
|
|
26
|
+
<Ks extends DistinctKeys<Message>[]>(...keys: Ks) =>
|
|
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
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const editedMessage =
|
|
38
|
+
<Ks extends DistinctKeys<CommonMessageBundle>[]>(...keys: Ks) =>
|
|
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
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const channelPost =
|
|
52
|
+
<Ks extends DistinctKeys<Message>[]>(...keys: Ks) =>
|
|
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
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export const editedChannelPost =
|
|
64
|
+
<Ks extends DistinctKeys<CommonMessageBundle>[]>(...keys: Ks) =>
|
|
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
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const callbackQuery =
|
|
78
|
+
<Ks extends DistinctKeys<CallbackQuery>[]>(...keys: Ks) =>
|
|
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
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Any of the provided filters must match */
|
|
92
|
+
export const anyOf =
|
|
93
|
+
<Us extends Update[]>(
|
|
94
|
+
...filters: {
|
|
95
|
+
[UIdx in keyof Us]: Filter<Us[UIdx]>
|
|
96
|
+
}
|
|
97
|
+
) =>
|
|
98
|
+
(update: Update): update is Us[number] => {
|
|
99
|
+
for (const filter of filters) if (filter(update)) return true
|
|
100
|
+
return false
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** All of the provided filters must match */
|
|
104
|
+
export const allOf =
|
|
105
|
+
<U extends Update, Fs extends Filter<U>[]>(...filters: Fs) =>
|
|
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
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { User } from '@telegraf/types'
|
|
2
|
+
import {
|
|
3
|
+
FmtString,
|
|
4
|
+
createFmt,
|
|
5
|
+
linkOrMention,
|
|
6
|
+
join as _join,
|
|
7
|
+
} from './core/helpers/formatting'
|
|
8
|
+
|
|
9
|
+
export { FmtString }
|
|
10
|
+
|
|
11
|
+
type Nestable<Kind extends string> = string | number | boolean | FmtString<Kind>
|
|
12
|
+
type Nesting<Kind extends string> = [
|
|
13
|
+
parts: Nestable<Kind> | readonly Nestable<Kind>[],
|
|
14
|
+
...items: Nestable<Kind>[],
|
|
15
|
+
]
|
|
16
|
+
type Nests<Is extends string, Kind extends string> = (
|
|
17
|
+
...args: Nesting<Kind>
|
|
18
|
+
) => FmtString<Is>
|
|
19
|
+
|
|
20
|
+
// Nests<A, B> means the function will return A, and it can nest B
|
|
21
|
+
// Nests<'fmt', string> means it will nest anything
|
|
22
|
+
// Nests<'code', never> means it will not nest anything
|
|
23
|
+
|
|
24
|
+
// Allowing everything to nest 'fmt' is a necessary evil; it allows to indirectly nest illegal entities
|
|
25
|
+
// Except for 'code' and 'pre', which don't nest anything anyway, so they only deal with strings
|
|
26
|
+
|
|
27
|
+
export const join = _join as Nests<'fmt', string>
|
|
28
|
+
|
|
29
|
+
export const fmt = createFmt() as Nests<'fmt', string>
|
|
30
|
+
|
|
31
|
+
export const bold = createFmt('bold') as Nests<
|
|
32
|
+
'bold',
|
|
33
|
+
'fmt' | 'bold' | 'italic' | 'underline' | 'strikethrough' | 'spoiler'
|
|
34
|
+
>
|
|
35
|
+
|
|
36
|
+
export const italic = createFmt('italic') as Nests<
|
|
37
|
+
'italic',
|
|
38
|
+
'fmt' | 'bold' | 'italic' | 'underline' | 'strikethrough' | 'spoiler'
|
|
39
|
+
>
|
|
40
|
+
|
|
41
|
+
export const spoiler = createFmt('spoiler') as Nests<
|
|
42
|
+
'spoiler',
|
|
43
|
+
'fmt' | 'bold' | 'italic' | 'underline' | 'strikethrough' | 'spoiler'
|
|
44
|
+
>
|
|
45
|
+
|
|
46
|
+
export const strikethrough =
|
|
47
|
+
//
|
|
48
|
+
createFmt('strikethrough') as Nests<
|
|
49
|
+
'strikethrough',
|
|
50
|
+
'fmt' | 'bold' | 'italic' | 'underline' | 'strikethrough' | 'spoiler'
|
|
51
|
+
>
|
|
52
|
+
|
|
53
|
+
export const underline =
|
|
54
|
+
//
|
|
55
|
+
createFmt('underline') as Nests<
|
|
56
|
+
'underline',
|
|
57
|
+
'fmt' | 'bold' | 'italic' | 'underline' | 'strikethrough' | 'spoiler'
|
|
58
|
+
>
|
|
59
|
+
|
|
60
|
+
export const quote =
|
|
61
|
+
//
|
|
62
|
+
createFmt('blockquote') as Nests<
|
|
63
|
+
'blockquote',
|
|
64
|
+
| 'fmt'
|
|
65
|
+
| 'bold'
|
|
66
|
+
| 'italic'
|
|
67
|
+
| 'underline'
|
|
68
|
+
| 'strikethrough'
|
|
69
|
+
| 'spoiler'
|
|
70
|
+
| 'code'
|
|
71
|
+
>
|
|
72
|
+
|
|
73
|
+
export const code = createFmt('code') as Nests<'code', never>
|
|
74
|
+
|
|
75
|
+
export const pre = (language: string) =>
|
|
76
|
+
createFmt('pre', { language }) as Nests<'pre', never>
|
|
77
|
+
|
|
78
|
+
export const link = (
|
|
79
|
+
content: Nestable<
|
|
80
|
+
| 'fmt'
|
|
81
|
+
| 'bold'
|
|
82
|
+
| 'italic'
|
|
83
|
+
| 'underline'
|
|
84
|
+
| 'strikethrough'
|
|
85
|
+
| 'spoiler'
|
|
86
|
+
| 'code'
|
|
87
|
+
>,
|
|
88
|
+
url: string
|
|
89
|
+
) =>
|
|
90
|
+
//
|
|
91
|
+
linkOrMention(content, { type: 'text_link', url }) as FmtString<'text_link'>
|
|
92
|
+
|
|
93
|
+
export const mention = (
|
|
94
|
+
name: Nestable<
|
|
95
|
+
| 'fmt'
|
|
96
|
+
| 'bold'
|
|
97
|
+
| 'italic'
|
|
98
|
+
| 'underline'
|
|
99
|
+
| 'strikethrough'
|
|
100
|
+
| 'spoiler'
|
|
101
|
+
| 'code'
|
|
102
|
+
>,
|
|
103
|
+
user: number | User
|
|
104
|
+
) =>
|
|
105
|
+
typeof user === 'number'
|
|
106
|
+
? link(name, 'tg://user?id=' + user)
|
|
107
|
+
: (linkOrMention(name, {
|
|
108
|
+
type: 'text_mention',
|
|
109
|
+
user,
|
|
110
|
+
}) as FmtString<'text_mention'>)
|
package/src/future.ts
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { ReplyParameters } from '@telegraf/types'
|
|
2
|
+
import Context from './context'
|
|
3
|
+
import { Middleware } from './middleware'
|
|
4
|
+
|
|
5
|
+
type ReplyContext = { [key in keyof Context & `reply${string}`]: Context[key] }
|
|
6
|
+
|
|
7
|
+
function makeReply<
|
|
8
|
+
C extends Context,
|
|
9
|
+
E extends { reply_parameters?: ReplyParameters },
|
|
10
|
+
>(ctx: C, extra?: E) {
|
|
11
|
+
if (ctx.msgId)
|
|
12
|
+
return {
|
|
13
|
+
// overrides in this order so user can override all properties
|
|
14
|
+
reply_parameters: {
|
|
15
|
+
message_id: ctx.msgId,
|
|
16
|
+
...extra?.reply_parameters,
|
|
17
|
+
},
|
|
18
|
+
...extra,
|
|
19
|
+
}
|
|
20
|
+
else return extra
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const replyContext: ReplyContext = {
|
|
24
|
+
replyWithChatAction: function () {
|
|
25
|
+
throw new TypeError(
|
|
26
|
+
'ctx.replyWithChatAction has been removed, use ctx.sendChatAction instead'
|
|
27
|
+
)
|
|
28
|
+
},
|
|
29
|
+
reply(this: Context, text, extra) {
|
|
30
|
+
this.assert(this.chat, 'reply')
|
|
31
|
+
return this.telegram.sendMessage(this.chat.id, text, makeReply(this, extra))
|
|
32
|
+
},
|
|
33
|
+
replyWithAnimation(this: Context, animation, extra) {
|
|
34
|
+
this.assert(this.chat, 'replyWithAnimation')
|
|
35
|
+
return this.telegram.sendAnimation(
|
|
36
|
+
this.chat.id,
|
|
37
|
+
animation,
|
|
38
|
+
makeReply(this, extra)
|
|
39
|
+
)
|
|
40
|
+
},
|
|
41
|
+
replyWithAudio(this: Context, audio, extra) {
|
|
42
|
+
this.assert(this.chat, 'replyWithAudio')
|
|
43
|
+
return this.telegram.sendAudio(this.chat.id, audio, makeReply(this, extra))
|
|
44
|
+
},
|
|
45
|
+
replyWithContact(this: Context, phoneNumber, firstName, extra) {
|
|
46
|
+
this.assert(this.chat, 'replyWithContact')
|
|
47
|
+
return this.telegram.sendContact(
|
|
48
|
+
this.chat.id,
|
|
49
|
+
phoneNumber,
|
|
50
|
+
firstName,
|
|
51
|
+
makeReply(this, extra)
|
|
52
|
+
)
|
|
53
|
+
},
|
|
54
|
+
replyWithDice(this: Context, extra) {
|
|
55
|
+
this.assert(this.chat, 'replyWithDice')
|
|
56
|
+
return this.telegram.sendDice(this.chat.id, makeReply(this, extra))
|
|
57
|
+
},
|
|
58
|
+
replyWithDocument(this: Context, document, extra) {
|
|
59
|
+
this.assert(this.chat, 'replyWithDocument')
|
|
60
|
+
return this.telegram.sendDocument(
|
|
61
|
+
this.chat.id,
|
|
62
|
+
document,
|
|
63
|
+
makeReply(this, extra)
|
|
64
|
+
)
|
|
65
|
+
},
|
|
66
|
+
replyWithGame(this: Context, gameName, extra) {
|
|
67
|
+
this.assert(this.chat, 'replyWithGame')
|
|
68
|
+
return this.telegram.sendGame(
|
|
69
|
+
this.chat.id,
|
|
70
|
+
gameName,
|
|
71
|
+
makeReply(this, extra)
|
|
72
|
+
)
|
|
73
|
+
},
|
|
74
|
+
replyWithHTML(this: Context, html, extra) {
|
|
75
|
+
this.assert(this.chat, 'replyWithHTML')
|
|
76
|
+
return this.telegram.sendMessage(this.chat.id, html, {
|
|
77
|
+
parse_mode: 'HTML',
|
|
78
|
+
...makeReply(this, extra),
|
|
79
|
+
})
|
|
80
|
+
},
|
|
81
|
+
replyWithInvoice(this: Context, invoice, extra) {
|
|
82
|
+
this.assert(this.chat, 'replyWithInvoice')
|
|
83
|
+
return this.telegram.sendInvoice(
|
|
84
|
+
this.chat.id,
|
|
85
|
+
invoice,
|
|
86
|
+
makeReply(this, extra)
|
|
87
|
+
)
|
|
88
|
+
},
|
|
89
|
+
replyWithLocation(this: Context, latitude, longitude, extra) {
|
|
90
|
+
this.assert(this.chat, 'replyWithLocation')
|
|
91
|
+
return this.telegram.sendLocation(
|
|
92
|
+
this.chat.id,
|
|
93
|
+
latitude,
|
|
94
|
+
longitude,
|
|
95
|
+
makeReply(this, extra)
|
|
96
|
+
)
|
|
97
|
+
},
|
|
98
|
+
replyWithMarkdown(this: Context, markdown, extra) {
|
|
99
|
+
this.assert(this.chat, 'replyWithMarkdown')
|
|
100
|
+
return this.telegram.sendMessage(this.chat.id, markdown, {
|
|
101
|
+
parse_mode: 'Markdown',
|
|
102
|
+
...makeReply(this, extra),
|
|
103
|
+
})
|
|
104
|
+
},
|
|
105
|
+
replyWithMarkdownV2(this: Context, markdown, extra) {
|
|
106
|
+
this.assert(this.chat, 'replyWithMarkdownV2')
|
|
107
|
+
return this.telegram.sendMessage(this.chat.id, markdown, {
|
|
108
|
+
parse_mode: 'MarkdownV2',
|
|
109
|
+
...makeReply(this, extra),
|
|
110
|
+
})
|
|
111
|
+
},
|
|
112
|
+
replyWithMediaGroup(this: Context, media, extra) {
|
|
113
|
+
this.assert(this.chat, 'replyWithMediaGroup')
|
|
114
|
+
return this.telegram.sendMediaGroup(
|
|
115
|
+
this.chat.id,
|
|
116
|
+
media,
|
|
117
|
+
makeReply(this, extra)
|
|
118
|
+
)
|
|
119
|
+
},
|
|
120
|
+
replyWithPhoto(this: Context, photo, extra) {
|
|
121
|
+
this.assert(this.chat, 'replyWithPhoto')
|
|
122
|
+
return this.telegram.sendPhoto(this.chat.id, photo, makeReply(this, extra))
|
|
123
|
+
},
|
|
124
|
+
replyWithPoll(this: Context, question, options, extra) {
|
|
125
|
+
this.assert(this.chat, 'replyWithPoll')
|
|
126
|
+
return this.telegram.sendPoll(
|
|
127
|
+
this.chat.id,
|
|
128
|
+
question,
|
|
129
|
+
options,
|
|
130
|
+
makeReply(this, extra)
|
|
131
|
+
)
|
|
132
|
+
},
|
|
133
|
+
replyWithQuiz(this: Context, question, options, extra) {
|
|
134
|
+
this.assert(this.chat, 'replyWithQuiz')
|
|
135
|
+
return this.telegram.sendQuiz(
|
|
136
|
+
this.chat.id,
|
|
137
|
+
question,
|
|
138
|
+
options,
|
|
139
|
+
makeReply(this, extra)
|
|
140
|
+
)
|
|
141
|
+
},
|
|
142
|
+
replyWithSticker(this: Context, sticker, extra) {
|
|
143
|
+
this.assert(this.chat, 'replyWithSticker')
|
|
144
|
+
return this.telegram.sendSticker(
|
|
145
|
+
this.chat.id,
|
|
146
|
+
sticker,
|
|
147
|
+
makeReply(this, extra)
|
|
148
|
+
)
|
|
149
|
+
},
|
|
150
|
+
replyWithVenue(this: Context, latitude, longitude, title, address, extra) {
|
|
151
|
+
this.assert(this.chat, 'replyWithVenue')
|
|
152
|
+
return this.telegram.sendVenue(
|
|
153
|
+
this.chat.id,
|
|
154
|
+
latitude,
|
|
155
|
+
longitude,
|
|
156
|
+
title,
|
|
157
|
+
address,
|
|
158
|
+
makeReply(this, extra)
|
|
159
|
+
)
|
|
160
|
+
},
|
|
161
|
+
replyWithVideo(this: Context, video, extra) {
|
|
162
|
+
this.assert(this.chat, 'replyWithVideo')
|
|
163
|
+
return this.telegram.sendVideo(this.chat.id, video, makeReply(this, extra))
|
|
164
|
+
},
|
|
165
|
+
replyWithVideoNote(this: Context, videoNote, extra) {
|
|
166
|
+
this.assert(this.chat, 'replyWithVideoNote')
|
|
167
|
+
return this.telegram.sendVideoNote(
|
|
168
|
+
this.chat.id,
|
|
169
|
+
videoNote,
|
|
170
|
+
makeReply(this, extra)
|
|
171
|
+
)
|
|
172
|
+
},
|
|
173
|
+
replyWithVoice(this: Context, voice, extra) {
|
|
174
|
+
this.assert(this.chat, 'replyWithVoice')
|
|
175
|
+
return this.telegram.sendVoice(this.chat.id, voice, makeReply(this, extra))
|
|
176
|
+
},
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Sets up Context to use the new reply methods.
|
|
181
|
+
* This middleware makes `ctx.reply()` and `ctx.replyWith*()` methods will actually reply to the message they are replying to.
|
|
182
|
+
* Use `ctx.sendMessage()` to send a message in chat without replying to it.
|
|
183
|
+
*
|
|
184
|
+
* If the message to reply is deleted, `reply()` will send a normal message.
|
|
185
|
+
* If the update is not a message and we are unable to reply, `reply()` will send a normal message.
|
|
186
|
+
*/
|
|
187
|
+
export function useNewReplies<C extends Context>(): Middleware<C> {
|
|
188
|
+
return (ctx, next) => {
|
|
189
|
+
ctx.reply = replyContext.reply
|
|
190
|
+
ctx.replyWithPhoto = replyContext.replyWithPhoto
|
|
191
|
+
ctx.replyWithMediaGroup = replyContext.replyWithMediaGroup
|
|
192
|
+
ctx.replyWithAudio = replyContext.replyWithAudio
|
|
193
|
+
ctx.replyWithDice = replyContext.replyWithDice
|
|
194
|
+
ctx.replyWithDocument = replyContext.replyWithDocument
|
|
195
|
+
ctx.replyWithSticker = replyContext.replyWithSticker
|
|
196
|
+
ctx.replyWithVideo = replyContext.replyWithVideo
|
|
197
|
+
ctx.replyWithAnimation = replyContext.replyWithAnimation
|
|
198
|
+
ctx.replyWithVideoNote = replyContext.replyWithVideoNote
|
|
199
|
+
ctx.replyWithInvoice = replyContext.replyWithInvoice
|
|
200
|
+
ctx.replyWithGame = replyContext.replyWithGame
|
|
201
|
+
ctx.replyWithVoice = replyContext.replyWithVoice
|
|
202
|
+
ctx.replyWithPoll = replyContext.replyWithPoll
|
|
203
|
+
ctx.replyWithQuiz = replyContext.replyWithQuiz
|
|
204
|
+
ctx.replyWithChatAction = replyContext.replyWithChatAction
|
|
205
|
+
ctx.replyWithLocation = replyContext.replyWithLocation
|
|
206
|
+
ctx.replyWithVenue = replyContext.replyWithVenue
|
|
207
|
+
ctx.replyWithContact = replyContext.replyWithContact
|
|
208
|
+
ctx.replyWithMarkdown = replyContext.replyWithMarkdown
|
|
209
|
+
ctx.replyWithMarkdownV2 = replyContext.replyWithMarkdownV2
|
|
210
|
+
ctx.replyWithHTML = replyContext.replyWithHTML
|
|
211
|
+
return next()
|
|
212
|
+
}
|
|
213
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export { Telegraf } from './telegraf'
|
|
2
|
+
export { Context, NarrowedContext } from './context'
|
|
3
|
+
export { Composer } from './composer'
|
|
4
|
+
export { Middleware, MiddlewareFn, MiddlewareObj } from './middleware'
|
|
5
|
+
export { Router } from './router'
|
|
6
|
+
export { TelegramError } from './core/network/error'
|
|
7
|
+
export { Telegram } from './telegram'
|
|
8
|
+
|
|
9
|
+
export * as Types from './telegram-types'
|
|
10
|
+
export * as Markup from './markup'
|
|
11
|
+
export * as Input from './input'
|
|
12
|
+
export * as Format from './format'
|
|
13
|
+
|
|
14
|
+
export { deunionize } from './core/helpers/deunionize'
|
|
15
|
+
export { session, MemorySessionStore, SessionStore } from './session'
|
|
16
|
+
|
|
17
|
+
export * as Scenes from './scenes'
|
package/src/input.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { InputFile } from './core/types/typegram'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The local file specified by path will be uploaded to Telegram using multipart/form-data.
|
|
5
|
+
*
|
|
6
|
+
* 10 MB max size for photos, 50 MB for other files.
|
|
7
|
+
*/
|
|
8
|
+
// prettier-ignore
|
|
9
|
+
export const fromLocalFile = (path: string, filename?: string): InputFile => ({ source: path, filename })
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The buffer will be uploaded as file to Telegram using multipart/form-data.
|
|
13
|
+
*
|
|
14
|
+
* 10 MB max size for photos, 50 MB for other files.
|
|
15
|
+
*/
|
|
16
|
+
// prettier-ignore
|
|
17
|
+
export const fromBuffer = (buffer: Buffer, filename?: string): InputFile => ({ source: buffer, filename })
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Contents of the stream will be uploaded as file to Telegram using multipart/form-data.
|
|
21
|
+
*
|
|
22
|
+
* 10 MB max size for photos, 50 MB for other files.
|
|
23
|
+
*/
|
|
24
|
+
// prettier-ignore
|
|
25
|
+
export const fromReadableStream = (stream: NodeJS.ReadableStream, filename?: string): InputFile => ({ source: stream, filename })
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Contents of the URL will be streamed to Telegram.
|
|
29
|
+
*
|
|
30
|
+
* 10 MB max size for photos, 50 MB for other files.
|
|
31
|
+
*/
|
|
32
|
+
// prettier-ignore
|
|
33
|
+
export const fromURLStream = (url: string | URL, filename?: string): InputFile => ({ url: url.toString(), filename })
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Provide Telegram with an HTTP URL for the file to be sent.
|
|
37
|
+
* Telegram will download and send the file.
|
|
38
|
+
*
|
|
39
|
+
* * The target file must have the correct MIME type (e.g., audio/mpeg for `sendAudio`, etc.).
|
|
40
|
+
* * `sendDocument` with URL will currently only work for GIF, PDF and ZIP files.
|
|
41
|
+
* * To use `sendVoice`, the file must have the type audio/ogg and be no more than 1MB in size.
|
|
42
|
+
* 1-20MB voice notes will be sent as files.
|
|
43
|
+
*
|
|
44
|
+
* 5 MB max size for photos and 20 MB max for other types of content.
|
|
45
|
+
*/
|
|
46
|
+
export const fromURL = (url: string | URL): string => url.toString()
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* If the file is already stored somewhere on the Telegram servers, you don't need to reupload it:
|
|
50
|
+
* each file object has a file_id field, simply pass this file_id as a parameter instead of uploading.
|
|
51
|
+
*
|
|
52
|
+
* It is not possible to change the file type when resending by file_id.
|
|
53
|
+
*
|
|
54
|
+
* It is not possible to resend thumbnails using file_id.
|
|
55
|
+
* They have to be uploaded using one of the other Input methods.
|
|
56
|
+
*
|
|
57
|
+
* There are no limits for files sent this way.
|
|
58
|
+
*/
|
|
59
|
+
export const fromFileId = (fileId: string): string => fileId
|
package/src/markup.ts
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ForceReply,
|
|
3
|
+
InlineKeyboardButton,
|
|
4
|
+
InlineKeyboardMarkup,
|
|
5
|
+
KeyboardButton,
|
|
6
|
+
ReplyKeyboardMarkup,
|
|
7
|
+
ReplyKeyboardRemove,
|
|
8
|
+
} from './core/types/typegram'
|
|
9
|
+
import { is2D } from './core/helpers/check'
|
|
10
|
+
|
|
11
|
+
type Hideable<B> = B & { hide?: boolean }
|
|
12
|
+
type HideableKBtn = Hideable<KeyboardButton>
|
|
13
|
+
type HideableIKBtn = Hideable<InlineKeyboardButton>
|
|
14
|
+
|
|
15
|
+
export class Markup<
|
|
16
|
+
T extends
|
|
17
|
+
| InlineKeyboardMarkup
|
|
18
|
+
| ReplyKeyboardMarkup
|
|
19
|
+
| ReplyKeyboardRemove
|
|
20
|
+
| ForceReply,
|
|
21
|
+
> {
|
|
22
|
+
constructor(readonly reply_markup: T) {}
|
|
23
|
+
|
|
24
|
+
selective<T extends ForceReply | ReplyKeyboardMarkup>(
|
|
25
|
+
this: Markup<T>,
|
|
26
|
+
value = true
|
|
27
|
+
) {
|
|
28
|
+
return new Markup<T>({ ...this.reply_markup, selective: value })
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
placeholder<T extends ForceReply | ReplyKeyboardMarkup>(
|
|
32
|
+
this: Markup<T>,
|
|
33
|
+
placeholder: string
|
|
34
|
+
) {
|
|
35
|
+
return new Markup<T>({
|
|
36
|
+
...this.reply_markup,
|
|
37
|
+
input_field_placeholder: placeholder,
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
resize(this: Markup<ReplyKeyboardMarkup>, value = true) {
|
|
42
|
+
return new Markup<ReplyKeyboardMarkup>({
|
|
43
|
+
...this.reply_markup,
|
|
44
|
+
resize_keyboard: value,
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
oneTime(this: Markup<ReplyKeyboardMarkup>, value = true) {
|
|
49
|
+
return new Markup<ReplyKeyboardMarkup>({
|
|
50
|
+
...this.reply_markup,
|
|
51
|
+
one_time_keyboard: value,
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
persistent(this: Markup<ReplyKeyboardMarkup>, value = true) {
|
|
56
|
+
return new Markup<ReplyKeyboardMarkup>({
|
|
57
|
+
...this.reply_markup,
|
|
58
|
+
is_persistent: value,
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export * as button from './button'
|
|
64
|
+
|
|
65
|
+
export function removeKeyboard(): Markup<ReplyKeyboardRemove> {
|
|
66
|
+
return new Markup<ReplyKeyboardRemove>({ remove_keyboard: true })
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function forceReply(): Markup<ForceReply> {
|
|
70
|
+
return new Markup<ForceReply>({ force_reply: true })
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function keyboard(buttons: HideableKBtn[][]): Markup<ReplyKeyboardMarkup>
|
|
74
|
+
export function keyboard(
|
|
75
|
+
buttons: HideableKBtn[],
|
|
76
|
+
options?: Partial<KeyboardBuildingOptions<HideableKBtn>>
|
|
77
|
+
): Markup<ReplyKeyboardMarkup>
|
|
78
|
+
export function keyboard(
|
|
79
|
+
buttons: HideableKBtn[] | HideableKBtn[][],
|
|
80
|
+
options?: Partial<KeyboardBuildingOptions<HideableKBtn>>
|
|
81
|
+
): Markup<ReplyKeyboardMarkup> {
|
|
82
|
+
const keyboard = buildKeyboard(buttons, {
|
|
83
|
+
columns: 1,
|
|
84
|
+
...options,
|
|
85
|
+
})
|
|
86
|
+
return new Markup<ReplyKeyboardMarkup>({ keyboard })
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function inlineKeyboard(
|
|
90
|
+
buttons: HideableIKBtn[][]
|
|
91
|
+
): Markup<InlineKeyboardMarkup>
|
|
92
|
+
export function inlineKeyboard(
|
|
93
|
+
buttons: HideableIKBtn[],
|
|
94
|
+
options?: Partial<KeyboardBuildingOptions<HideableIKBtn>>
|
|
95
|
+
): Markup<InlineKeyboardMarkup>
|
|
96
|
+
export function inlineKeyboard(
|
|
97
|
+
buttons: HideableIKBtn[] | HideableIKBtn[][],
|
|
98
|
+
options?: Partial<KeyboardBuildingOptions<HideableIKBtn>>
|
|
99
|
+
): Markup<InlineKeyboardMarkup> {
|
|
100
|
+
const inlineKeyboard = buildKeyboard(buttons, {
|
|
101
|
+
columns: buttons.length,
|
|
102
|
+
...options,
|
|
103
|
+
})
|
|
104
|
+
return new Markup<InlineKeyboardMarkup>({ inline_keyboard: inlineKeyboard })
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
interface KeyboardBuildingOptions<B extends HideableKBtn | HideableIKBtn> {
|
|
108
|
+
wrap?: (btn: B, index: number, currentRow: B[]) => boolean
|
|
109
|
+
columns: number
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function buildKeyboard<B extends HideableKBtn | HideableIKBtn>(
|
|
113
|
+
buttons: B[] | B[][],
|
|
114
|
+
options: KeyboardBuildingOptions<B>
|
|
115
|
+
): B[][] {
|
|
116
|
+
const result: B[][] = []
|
|
117
|
+
if (!Array.isArray(buttons)) {
|
|
118
|
+
return result
|
|
119
|
+
}
|
|
120
|
+
if (is2D(buttons)) {
|
|
121
|
+
return buttons.map((row) => row.filter((button) => !button.hide))
|
|
122
|
+
}
|
|
123
|
+
const wrapFn =
|
|
124
|
+
options.wrap !== undefined
|
|
125
|
+
? options.wrap
|
|
126
|
+
: (_btn: B, _index: number, currentRow: B[]) =>
|
|
127
|
+
currentRow.length >= options.columns
|
|
128
|
+
let currentRow: B[] = []
|
|
129
|
+
let index = 0
|
|
130
|
+
for (const btn of buttons.filter((button) => !button.hide)) {
|
|
131
|
+
if (wrapFn(btn, index, currentRow) && currentRow.length > 0) {
|
|
132
|
+
result.push(currentRow)
|
|
133
|
+
currentRow = []
|
|
134
|
+
}
|
|
135
|
+
currentRow.push(btn)
|
|
136
|
+
index++
|
|
137
|
+
}
|
|
138
|
+
if (currentRow.length > 0) {
|
|
139
|
+
result.push(currentRow)
|
|
140
|
+
}
|
|
141
|
+
return result
|
|
142
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Context } from './context'
|
|
2
|
+
import { Update } from './core/types/typegram'
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
next's parameter is in a contravariant position, and thus, trying to type it
|
|
6
|
+
prevents assigning `MiddlewareFn<ContextMessageUpdate>`
|
|
7
|
+
to `MiddlewareFn<CustomContext>`.
|
|
8
|
+
Middleware passing the parameter should be a separate type instead.
|
|
9
|
+
*/
|
|
10
|
+
export type MiddlewareFn<C extends Context<U>, U extends Update = Update> = (
|
|
11
|
+
ctx: C,
|
|
12
|
+
next: () => Promise<void>
|
|
13
|
+
) => Promise<unknown> | void
|
|
14
|
+
|
|
15
|
+
export interface MiddlewareObj<
|
|
16
|
+
C extends Context<U>,
|
|
17
|
+
U extends Update = Update,
|
|
18
|
+
> {
|
|
19
|
+
middleware: () => MiddlewareFn<C, U>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type Middleware<C extends Context<U>, U extends Update = Update> =
|
|
23
|
+
| MiddlewareFn<C, U>
|
|
24
|
+
| MiddlewareObj<C, U>
|