teledzik 1.0.3 â 1.0.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.
- package/README.md +38 -5
- package/lib/button.js +48 -5
- package/package.json +1 -1
- package/src/button.ts +89 -6
- package/typings/button.d.ts +51 -2
- package/src/filters.js +0 -69
- package/src/format.js +0 -38
- package/src/future.js +0 -147
- package/src/markup.js +0 -93
- package/src/scenes.js +0 -17
- package/src/session.js +0 -175
- package/src/types.js +0 -2
- package/src/utils.js +0 -5
package/README.md
CHANGED
|
@@ -467,7 +467,7 @@ const msg = new MD()
|
|
|
467
467
|
.details('Expand me', '### Hidden heading\n\n- item 1\n- item 2')
|
|
468
468
|
.divider()
|
|
469
469
|
.paragraph('See footnote' + MD.sup('[1]'))
|
|
470
|
-
.footnote('1', MD.url('https://t.me/
|
|
470
|
+
.footnote('1', MD.url('https://t.me/lorddzik', '@lorddzik'))
|
|
471
471
|
.build()
|
|
472
472
|
```
|
|
473
473
|
|
|
@@ -526,7 +526,7 @@ bot.command('ai', async (ctx) => {
|
|
|
526
526
|
.heading(2, 'đ¤ AI Response')
|
|
527
527
|
.paragraph('Here is the final answer from the AI.')
|
|
528
528
|
.divider()
|
|
529
|
-
.footer(HTML.url('https://t.me/
|
|
529
|
+
.footer(HTML.url('https://t.me/lorddzik', '@lorddzik'))
|
|
530
530
|
.build()
|
|
531
531
|
)
|
|
532
532
|
})
|
|
@@ -564,7 +564,7 @@ await ctx.sendRichMessage(msg, {
|
|
|
564
564
|
Markup.button.callback('â
Yes', 'answer:yes'),
|
|
565
565
|
Markup.button.callback('â No', 'answer:no'),
|
|
566
566
|
],
|
|
567
|
-
[Markup.button.url('đ Visit', 'https://t.me/
|
|
567
|
+
[Markup.button.url('đ Visit', 'https://t.me/lorddzik')],
|
|
568
568
|
]).reply_markup,
|
|
569
569
|
})
|
|
570
570
|
|
|
@@ -600,7 +600,7 @@ bot.on('inline_query', async (ctx) => {
|
|
|
600
600
|
const richContent: RichMessage.InputRichMessageContent = {
|
|
601
601
|
rich_message: new HTML()
|
|
602
602
|
.heading(1, 'Result from Inline Query')
|
|
603
|
-
.paragraph('Sent via ' + HTML.url('https://t.me/
|
|
603
|
+
.paragraph('Sent via ' + HTML.url('https://t.me/lorddzik', '@lorddzik') + '.')
|
|
604
604
|
.build(),
|
|
605
605
|
}
|
|
606
606
|
|
|
@@ -1117,10 +1117,43 @@ bot.use((ctx, next) => {
|
|
|
1117
1117
|
```
|
|
1118
1118
|
|
|
1119
1119
|
|
|
1120
|
+
### Native Colored Buttons (Telegram Bot API 9.4+)
|
|
1121
|
+
|
|
1122
|
+
Teledzik provides first-class support for native Telegram button background colors (`primary` Blue, `success` Green, `danger` Red) on inline keyboards:
|
|
1123
|
+
|
|
1124
|
+
```ts
|
|
1125
|
+
import { Markup } from 'teledzik'
|
|
1126
|
+
|
|
1127
|
+
const keyboard = Markup.inlineKeyboard([
|
|
1128
|
+
// đĩ Blue Background (Primary action)
|
|
1129
|
+
[Markup.button.primary('âšī¸ Bantuan', 'help')],
|
|
1130
|
+
|
|
1131
|
+
// đĸ Green Background (Success / positive action)
|
|
1132
|
+
[Markup.button.success('đ Status Build', 'status')],
|
|
1133
|
+
|
|
1134
|
+
// đ´ Red Background (Danger / destructive action)
|
|
1135
|
+
[Markup.button.danger('đ Perintah / Hapus', 'commands')],
|
|
1136
|
+
|
|
1137
|
+
// đĩ Native Styled URL Buttons
|
|
1138
|
+
[
|
|
1139
|
+
Markup.button.primaryUrl('đ Owner', 'https://t.me/LordDzik'),
|
|
1140
|
+
Markup.button.primaryUrl('âšī¸ Information', 'https://t.me/LordDzik')
|
|
1141
|
+
],
|
|
1142
|
+
|
|
1143
|
+
// đĸ Green & đ´ Red URL Buttons
|
|
1144
|
+
[
|
|
1145
|
+
Markup.button.successUrl('â
Website', 'https://example.com'),
|
|
1146
|
+
Markup.button.dangerUrl('đ¨ Report', 'https://example.com/report')
|
|
1147
|
+
]
|
|
1148
|
+
])
|
|
1149
|
+
|
|
1150
|
+
await ctx.reply('Menu:', keyboard)
|
|
1151
|
+
```
|
|
1152
|
+
|
|
1120
1153
|
---
|
|
1121
1154
|
|
|
1122
1155
|
## Author & Maintainer
|
|
1123
1156
|
|
|
1124
|
-
This fork is maintained by **@
|
|
1157
|
+
This fork is maintained by **@lorddzik** â [t.me/lorddzik](https://t.me/lorddzik)
|
|
1125
1158
|
|
|
1126
1159
|
Upstream project: [telegraf/telegraf](https://github.com/telegraf/telegraf) by The Telegraf Contributors.
|
package/lib/button.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.webApp = exports.login = exports.pay = exports.game = exports.switchToCurrentChat = exports.switchToChat = exports.callback = exports.url = exports.channelRequest = exports.groupRequest = exports.botRequest = exports.userRequest = exports.pollRequest = exports.locationRequest = exports.contactRequest = exports.text = void 0;
|
|
3
|
+
exports.dangerUrl = exports.successUrl = exports.primaryUrl = exports.danger = exports.success = exports.primary = exports.webApp = exports.login = exports.pay = exports.game = exports.switchToCurrentChat = exports.switchToChat = exports.callback = exports.url = exports.channelRequest = exports.groupRequest = exports.botRequest = exports.userRequest = exports.pollRequest = exports.locationRequest = exports.contactRequest = exports.text = void 0;
|
|
4
4
|
function text(text, hide = false) {
|
|
5
5
|
return { text, hide };
|
|
6
6
|
}
|
|
@@ -57,12 +57,12 @@ request_id, extra, hide = false) {
|
|
|
57
57
|
};
|
|
58
58
|
}
|
|
59
59
|
exports.channelRequest = channelRequest;
|
|
60
|
-
function url(text, url, hide = false) {
|
|
61
|
-
return { text, url, hide };
|
|
60
|
+
function url(text, url, hide = false, style, icon_custom_emoji_id) {
|
|
61
|
+
return { text, url, hide, ...(style ? { style } : {}), ...(icon_custom_emoji_id ? { icon_custom_emoji_id } : {}) };
|
|
62
62
|
}
|
|
63
63
|
exports.url = url;
|
|
64
|
-
function callback(text, data, hide = false) {
|
|
65
|
-
return { text, callback_data: data, hide };
|
|
64
|
+
function callback(text, data, hide = false, style, icon_custom_emoji_id) {
|
|
65
|
+
return { text, callback_data: data, hide, ...(style ? { style } : {}), ...(icon_custom_emoji_id ? { icon_custom_emoji_id } : {}) };
|
|
66
66
|
}
|
|
67
67
|
exports.callback = callback;
|
|
68
68
|
function switchToChat(text, value, hide = false) {
|
|
@@ -99,3 +99,46 @@ function webApp(text, url, hide = false
|
|
|
99
99
|
};
|
|
100
100
|
}
|
|
101
101
|
exports.webApp = webApp;
|
|
102
|
+
// ââ Native Telegram Background Color Buttons (Bot API 9.4+) âââââââââââââââ
|
|
103
|
+
/**
|
|
104
|
+
* đĩ Primary callback button (Blue background)
|
|
105
|
+
*/
|
|
106
|
+
function primary(text, data, hide = false, icon_custom_emoji_id) {
|
|
107
|
+
return callback(text, data, hide, 'primary', icon_custom_emoji_id);
|
|
108
|
+
}
|
|
109
|
+
exports.primary = primary;
|
|
110
|
+
/**
|
|
111
|
+
* đĸ Success callback button (Green background)
|
|
112
|
+
*/
|
|
113
|
+
function success(text, data, hide = false, icon_custom_emoji_id) {
|
|
114
|
+
return callback(text, data, hide, 'success', icon_custom_emoji_id);
|
|
115
|
+
}
|
|
116
|
+
exports.success = success;
|
|
117
|
+
/**
|
|
118
|
+
* đ´ Danger / Delete callback button (Red background)
|
|
119
|
+
*/
|
|
120
|
+
function danger(text, data, hide = false, icon_custom_emoji_id) {
|
|
121
|
+
return callback(text, data, hide, 'danger', icon_custom_emoji_id);
|
|
122
|
+
}
|
|
123
|
+
exports.danger = danger;
|
|
124
|
+
/**
|
|
125
|
+
* đĩ Primary URL button (Blue background)
|
|
126
|
+
*/
|
|
127
|
+
function primaryUrl(text, targetUrl, hide = false, icon_custom_emoji_id) {
|
|
128
|
+
return url(text, targetUrl, hide, 'primary', icon_custom_emoji_id);
|
|
129
|
+
}
|
|
130
|
+
exports.primaryUrl = primaryUrl;
|
|
131
|
+
/**
|
|
132
|
+
* đĸ Success URL button (Green background)
|
|
133
|
+
*/
|
|
134
|
+
function successUrl(text, targetUrl, hide = false, icon_custom_emoji_id) {
|
|
135
|
+
return url(text, targetUrl, hide, 'success', icon_custom_emoji_id);
|
|
136
|
+
}
|
|
137
|
+
exports.successUrl = successUrl;
|
|
138
|
+
/**
|
|
139
|
+
* đ´ Danger URL button (Red background)
|
|
140
|
+
*/
|
|
141
|
+
function dangerUrl(text, targetUrl, hide = false, icon_custom_emoji_id) {
|
|
142
|
+
return url(text, targetUrl, hide, 'danger', icon_custom_emoji_id);
|
|
143
|
+
}
|
|
144
|
+
exports.dangerUrl = dangerUrl;
|
package/package.json
CHANGED
package/src/button.ts
CHANGED
|
@@ -105,20 +105,26 @@ export function channelRequest(
|
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
+
export type ButtonStyle = 'primary' | 'success' | 'danger'
|
|
109
|
+
|
|
108
110
|
export function url(
|
|
109
111
|
text: string,
|
|
110
112
|
url: string,
|
|
111
|
-
hide = false
|
|
112
|
-
|
|
113
|
-
|
|
113
|
+
hide = false,
|
|
114
|
+
style?: ButtonStyle,
|
|
115
|
+
icon_custom_emoji_id?: string
|
|
116
|
+
): Hideable<InlineKeyboardButton.UrlButton & { style?: ButtonStyle; icon_custom_emoji_id?: string }> {
|
|
117
|
+
return { text, url, hide, ...(style ? { style } : {}), ...(icon_custom_emoji_id ? { icon_custom_emoji_id } : {}) }
|
|
114
118
|
}
|
|
115
119
|
|
|
116
120
|
export function callback(
|
|
117
121
|
text: string,
|
|
118
122
|
data: string,
|
|
119
|
-
hide = false
|
|
120
|
-
|
|
121
|
-
|
|
123
|
+
hide = false,
|
|
124
|
+
style?: ButtonStyle,
|
|
125
|
+
icon_custom_emoji_id?: string
|
|
126
|
+
): Hideable<InlineKeyboardButton.CallbackButton & { style?: ButtonStyle; icon_custom_emoji_id?: string }> {
|
|
127
|
+
return { text, callback_data: data, hide, ...(style ? { style } : {}), ...(icon_custom_emoji_id ? { icon_custom_emoji_id } : {}) }
|
|
122
128
|
}
|
|
123
129
|
|
|
124
130
|
export function switchToChat(
|
|
@@ -180,3 +186,80 @@ export function webApp(
|
|
|
180
186
|
hide,
|
|
181
187
|
}
|
|
182
188
|
}
|
|
189
|
+
|
|
190
|
+
// ââ Native Telegram Background Color Buttons (Bot API 9.4+) âââââââââââââââ
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* đĩ Primary callback button (Blue background)
|
|
194
|
+
*/
|
|
195
|
+
export function primary(
|
|
196
|
+
text: string,
|
|
197
|
+
data: string,
|
|
198
|
+
hide = false,
|
|
199
|
+
icon_custom_emoji_id?: string
|
|
200
|
+
) {
|
|
201
|
+
return callback(text, data, hide, 'primary', icon_custom_emoji_id)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* đĸ Success callback button (Green background)
|
|
206
|
+
*/
|
|
207
|
+
export function success(
|
|
208
|
+
text: string,
|
|
209
|
+
data: string,
|
|
210
|
+
hide = false,
|
|
211
|
+
icon_custom_emoji_id?: string
|
|
212
|
+
) {
|
|
213
|
+
return callback(text, data, hide, 'success', icon_custom_emoji_id)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* đ´ Danger / Delete callback button (Red background)
|
|
218
|
+
*/
|
|
219
|
+
export function danger(
|
|
220
|
+
text: string,
|
|
221
|
+
data: string,
|
|
222
|
+
hide = false,
|
|
223
|
+
icon_custom_emoji_id?: string
|
|
224
|
+
) {
|
|
225
|
+
return callback(text, data, hide, 'danger', icon_custom_emoji_id)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* đĩ Primary URL button (Blue background)
|
|
230
|
+
*/
|
|
231
|
+
export function primaryUrl(
|
|
232
|
+
text: string,
|
|
233
|
+
targetUrl: string,
|
|
234
|
+
hide = false,
|
|
235
|
+
icon_custom_emoji_id?: string
|
|
236
|
+
) {
|
|
237
|
+
return url(text, targetUrl, hide, 'primary', icon_custom_emoji_id)
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* đĸ Success URL button (Green background)
|
|
242
|
+
*/
|
|
243
|
+
export function successUrl(
|
|
244
|
+
text: string,
|
|
245
|
+
targetUrl: string,
|
|
246
|
+
hide = false,
|
|
247
|
+
icon_custom_emoji_id?: string
|
|
248
|
+
) {
|
|
249
|
+
return url(text, targetUrl, hide, 'success', icon_custom_emoji_id)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* đ´ Danger URL button (Red background)
|
|
254
|
+
*/
|
|
255
|
+
export function dangerUrl(
|
|
256
|
+
text: string,
|
|
257
|
+
targetUrl: string,
|
|
258
|
+
hide = false,
|
|
259
|
+
icon_custom_emoji_id?: string
|
|
260
|
+
) {
|
|
261
|
+
return url(text, targetUrl, hide, 'danger', icon_custom_emoji_id)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
package/typings/button.d.ts
CHANGED
|
@@ -20,8 +20,15 @@ type KeyboardButtonRequestChannel = Omit<KeyboardButtonRequestChat, 'request_id'
|
|
|
20
20
|
export declare function channelRequest(text: string,
|
|
21
21
|
/** Must fit in a signed 32 bit int */
|
|
22
22
|
request_id: number, extra?: KeyboardButtonRequestChannel, hide?: boolean): Hideable<KeyboardButton.RequestChatButton>;
|
|
23
|
-
export
|
|
24
|
-
export declare function
|
|
23
|
+
export type ButtonStyle = 'primary' | 'success' | 'danger';
|
|
24
|
+
export declare function url(text: string, url: string, hide?: boolean, style?: ButtonStyle, icon_custom_emoji_id?: string): Hideable<InlineKeyboardButton.UrlButton & {
|
|
25
|
+
style?: ButtonStyle;
|
|
26
|
+
icon_custom_emoji_id?: string;
|
|
27
|
+
}>;
|
|
28
|
+
export declare function callback(text: string, data: string, hide?: boolean, style?: ButtonStyle, icon_custom_emoji_id?: string): Hideable<InlineKeyboardButton.CallbackButton & {
|
|
29
|
+
style?: ButtonStyle;
|
|
30
|
+
icon_custom_emoji_id?: string;
|
|
31
|
+
}>;
|
|
25
32
|
export declare function switchToChat(text: string, value: string, hide?: boolean): Hideable<InlineKeyboardButton.SwitchInlineButton>;
|
|
26
33
|
export declare function switchToCurrentChat(text: string, value: string, hide?: boolean): Hideable<InlineKeyboardButton.SwitchInlineCurrentChatButton>;
|
|
27
34
|
export declare function game(text: string, hide?: boolean): Hideable<InlineKeyboardButton.GameButton>;
|
|
@@ -32,4 +39,46 @@ export declare function login(text: string, url: string, opts?: {
|
|
|
32
39
|
request_write_access?: boolean;
|
|
33
40
|
}, hide?: boolean): Hideable<InlineKeyboardButton.LoginButton>;
|
|
34
41
|
export declare function webApp(text: string, url: string, hide?: boolean): Hideable<InlineKeyboardButton.WebAppButton>;
|
|
42
|
+
/**
|
|
43
|
+
* đĩ Primary callback button (Blue background)
|
|
44
|
+
*/
|
|
45
|
+
export declare function primary(text: string, data: string, hide?: boolean, icon_custom_emoji_id?: string): Hideable<InlineKeyboardButton.CallbackButton & {
|
|
46
|
+
style?: ButtonStyle | undefined;
|
|
47
|
+
icon_custom_emoji_id?: string | undefined;
|
|
48
|
+
}>;
|
|
49
|
+
/**
|
|
50
|
+
* đĸ Success callback button (Green background)
|
|
51
|
+
*/
|
|
52
|
+
export declare function success(text: string, data: string, hide?: boolean, icon_custom_emoji_id?: string): Hideable<InlineKeyboardButton.CallbackButton & {
|
|
53
|
+
style?: ButtonStyle | undefined;
|
|
54
|
+
icon_custom_emoji_id?: string | undefined;
|
|
55
|
+
}>;
|
|
56
|
+
/**
|
|
57
|
+
* đ´ Danger / Delete callback button (Red background)
|
|
58
|
+
*/
|
|
59
|
+
export declare function danger(text: string, data: string, hide?: boolean, icon_custom_emoji_id?: string): Hideable<InlineKeyboardButton.CallbackButton & {
|
|
60
|
+
style?: ButtonStyle | undefined;
|
|
61
|
+
icon_custom_emoji_id?: string | undefined;
|
|
62
|
+
}>;
|
|
63
|
+
/**
|
|
64
|
+
* đĩ Primary URL button (Blue background)
|
|
65
|
+
*/
|
|
66
|
+
export declare function primaryUrl(text: string, targetUrl: string, hide?: boolean, icon_custom_emoji_id?: string): Hideable<InlineKeyboardButton.UrlButton & {
|
|
67
|
+
style?: ButtonStyle | undefined;
|
|
68
|
+
icon_custom_emoji_id?: string | undefined;
|
|
69
|
+
}>;
|
|
70
|
+
/**
|
|
71
|
+
* đĸ Success URL button (Green background)
|
|
72
|
+
*/
|
|
73
|
+
export declare function successUrl(text: string, targetUrl: string, hide?: boolean, icon_custom_emoji_id?: string): Hideable<InlineKeyboardButton.UrlButton & {
|
|
74
|
+
style?: ButtonStyle | undefined;
|
|
75
|
+
icon_custom_emoji_id?: string | undefined;
|
|
76
|
+
}>;
|
|
77
|
+
/**
|
|
78
|
+
* đ´ Danger URL button (Red background)
|
|
79
|
+
*/
|
|
80
|
+
export declare function dangerUrl(text: string, targetUrl: string, hide?: boolean, icon_custom_emoji_id?: string): Hideable<InlineKeyboardButton.UrlButton & {
|
|
81
|
+
style?: ButtonStyle | undefined;
|
|
82
|
+
icon_custom_emoji_id?: string | undefined;
|
|
83
|
+
}>;
|
|
35
84
|
export {};
|
package/src/filters.js
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.allOf = exports.anyOf = exports.callbackQuery = exports.editedChannelPost = exports.channelPost = exports.editedMessage = exports.message = void 0;
|
|
4
|
-
const message = (...keys) => (update) => {
|
|
5
|
-
if (!('message' in update))
|
|
6
|
-
return false;
|
|
7
|
-
for (const key of keys) {
|
|
8
|
-
if (!(key in update.message))
|
|
9
|
-
return false;
|
|
10
|
-
}
|
|
11
|
-
return true;
|
|
12
|
-
};
|
|
13
|
-
exports.message = message;
|
|
14
|
-
const editedMessage = (...keys) => (update) => {
|
|
15
|
-
if (!('edited_message' in update))
|
|
16
|
-
return false;
|
|
17
|
-
for (const key of keys) {
|
|
18
|
-
if (!(key in update.edited_message))
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
return true;
|
|
22
|
-
};
|
|
23
|
-
exports.editedMessage = editedMessage;
|
|
24
|
-
const channelPost = (...keys) => (update) => {
|
|
25
|
-
if (!('channel_post' in update))
|
|
26
|
-
return false;
|
|
27
|
-
for (const key of keys) {
|
|
28
|
-
if (!(key in update.channel_post))
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
return true;
|
|
32
|
-
};
|
|
33
|
-
exports.channelPost = channelPost;
|
|
34
|
-
const editedChannelPost = (...keys) => (update) => {
|
|
35
|
-
if (!('edited_channel_post' in update))
|
|
36
|
-
return false;
|
|
37
|
-
for (const key of keys) {
|
|
38
|
-
if (!(key in update.edited_channel_post))
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
return true;
|
|
42
|
-
};
|
|
43
|
-
exports.editedChannelPost = editedChannelPost;
|
|
44
|
-
const callbackQuery = (...keys) => (update) => {
|
|
45
|
-
if (!('callback_query' in update))
|
|
46
|
-
return false;
|
|
47
|
-
for (const key of keys) {
|
|
48
|
-
if (!(key in update.callback_query))
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
return true;
|
|
52
|
-
};
|
|
53
|
-
exports.callbackQuery = callbackQuery;
|
|
54
|
-
/** Any of the provided filters must match */
|
|
55
|
-
const anyOf = (...filters) => (update) => {
|
|
56
|
-
for (const filter of filters)
|
|
57
|
-
if (filter(update))
|
|
58
|
-
return true;
|
|
59
|
-
return false;
|
|
60
|
-
};
|
|
61
|
-
exports.anyOf = anyOf;
|
|
62
|
-
/** All of the provided filters must match */
|
|
63
|
-
const allOf = (...filters) => (update) => {
|
|
64
|
-
for (const filter of filters)
|
|
65
|
-
if (!filter(update))
|
|
66
|
-
return false;
|
|
67
|
-
return true;
|
|
68
|
-
};
|
|
69
|
-
exports.allOf = allOf;
|
package/src/format.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.mention = exports.link = exports.pre = exports.code = exports.quote = exports.underline = exports.strikethrough = exports.spoiler = exports.italic = exports.bold = exports.fmt = exports.join = exports.FmtString = void 0;
|
|
4
|
-
const formatting_1 = require("./core/helpers/formatting");
|
|
5
|
-
Object.defineProperty(exports, "FmtString", { enumerable: true, get: function () { return formatting_1.FmtString; } });
|
|
6
|
-
// Nests<A, B> means the function will return A, and it can nest B
|
|
7
|
-
// Nests<'fmt', string> means it will nest anything
|
|
8
|
-
// Nests<'code', never> means it will not nest anything
|
|
9
|
-
// Allowing everything to nest 'fmt' is a necessary evil; it allows to indirectly nest illegal entities
|
|
10
|
-
// Except for 'code' and 'pre', which don't nest anything anyway, so they only deal with strings
|
|
11
|
-
exports.join = formatting_1.join;
|
|
12
|
-
exports.fmt = (0, formatting_1.createFmt)();
|
|
13
|
-
exports.bold = (0, formatting_1.createFmt)('bold');
|
|
14
|
-
exports.italic = (0, formatting_1.createFmt)('italic');
|
|
15
|
-
exports.spoiler = (0, formatting_1.createFmt)('spoiler');
|
|
16
|
-
exports.strikethrough =
|
|
17
|
-
//
|
|
18
|
-
(0, formatting_1.createFmt)('strikethrough');
|
|
19
|
-
exports.underline =
|
|
20
|
-
//
|
|
21
|
-
(0, formatting_1.createFmt)('underline');
|
|
22
|
-
exports.quote =
|
|
23
|
-
//
|
|
24
|
-
(0, formatting_1.createFmt)('blockquote');
|
|
25
|
-
exports.code = (0, formatting_1.createFmt)('code');
|
|
26
|
-
const pre = (language) => (0, formatting_1.createFmt)('pre', { language });
|
|
27
|
-
exports.pre = pre;
|
|
28
|
-
const link = (content, url) =>
|
|
29
|
-
//
|
|
30
|
-
(0, formatting_1.linkOrMention)(content, { type: 'text_link', url });
|
|
31
|
-
exports.link = link;
|
|
32
|
-
const mention = (name, user) => typeof user === 'number'
|
|
33
|
-
? (0, exports.link)(name, 'tg://user?id=' + user)
|
|
34
|
-
: (0, formatting_1.linkOrMention)(name, {
|
|
35
|
-
type: 'text_mention',
|
|
36
|
-
user,
|
|
37
|
-
});
|
|
38
|
-
exports.mention = mention;
|
package/src/future.js
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useNewReplies = void 0;
|
|
4
|
-
function makeReply(ctx, extra) {
|
|
5
|
-
if (ctx.msgId)
|
|
6
|
-
return Object.assign({
|
|
7
|
-
// overrides in this order so user can override all properties
|
|
8
|
-
reply_parameters: Object.assign({ message_id: ctx.msgId }, extra === null || extra === void 0 ? void 0 : extra.reply_parameters) }, extra);
|
|
9
|
-
else
|
|
10
|
-
return extra;
|
|
11
|
-
}
|
|
12
|
-
const replyContext = {
|
|
13
|
-
replyWithChatAction: function () {
|
|
14
|
-
throw new TypeError('ctx.replyWithChatAction has been removed, use ctx.sendChatAction instead');
|
|
15
|
-
},
|
|
16
|
-
reply(text, extra) {
|
|
17
|
-
this.assert(this.chat, 'reply');
|
|
18
|
-
return this.telegram.sendMessage(this.chat.id, text, makeReply(this, extra));
|
|
19
|
-
},
|
|
20
|
-
replyWithAnimation(animation, extra) {
|
|
21
|
-
this.assert(this.chat, 'replyWithAnimation');
|
|
22
|
-
return this.telegram.sendAnimation(this.chat.id, animation, makeReply(this, extra));
|
|
23
|
-
},
|
|
24
|
-
replyWithAudio(audio, extra) {
|
|
25
|
-
this.assert(this.chat, 'replyWithAudio');
|
|
26
|
-
return this.telegram.sendAudio(this.chat.id, audio, makeReply(this, extra));
|
|
27
|
-
},
|
|
28
|
-
replyWithContact(phoneNumber, firstName, extra) {
|
|
29
|
-
this.assert(this.chat, 'replyWithContact');
|
|
30
|
-
return this.telegram.sendContact(this.chat.id, phoneNumber, firstName, makeReply(this, extra));
|
|
31
|
-
},
|
|
32
|
-
replyWithDice(extra) {
|
|
33
|
-
this.assert(this.chat, 'replyWithDice');
|
|
34
|
-
return this.telegram.sendDice(this.chat.id, makeReply(this, extra));
|
|
35
|
-
},
|
|
36
|
-
replyWithDocument(document, extra) {
|
|
37
|
-
this.assert(this.chat, 'replyWithDocument');
|
|
38
|
-
return this.telegram.sendDocument(this.chat.id, document, makeReply(this, extra));
|
|
39
|
-
},
|
|
40
|
-
replyWithGame(gameName, extra) {
|
|
41
|
-
this.assert(this.chat, 'replyWithGame');
|
|
42
|
-
return this.telegram.sendGame(this.chat.id, gameName, makeReply(this, extra));
|
|
43
|
-
},
|
|
44
|
-
replyWithHTML(html, extra) {
|
|
45
|
-
this.assert(this.chat, 'replyWithHTML');
|
|
46
|
-
return this.telegram.sendMessage(this.chat.id, html, Object.assign({ parse_mode: 'HTML' }, makeReply(this, extra)));
|
|
47
|
-
},
|
|
48
|
-
replyWithInvoice(invoice, extra) {
|
|
49
|
-
this.assert(this.chat, 'replyWithInvoice');
|
|
50
|
-
return this.telegram.sendInvoice(this.chat.id, invoice, makeReply(this, extra));
|
|
51
|
-
},
|
|
52
|
-
replyWithLocation(latitude, longitude, extra) {
|
|
53
|
-
this.assert(this.chat, 'replyWithLocation');
|
|
54
|
-
return this.telegram.sendLocation(this.chat.id, latitude, longitude, makeReply(this, extra));
|
|
55
|
-
},
|
|
56
|
-
replyWithMarkdown(markdown, extra) {
|
|
57
|
-
this.assert(this.chat, 'replyWithMarkdown');
|
|
58
|
-
return this.telegram.sendMessage(this.chat.id, markdown, Object.assign({ parse_mode: 'Markdown' }, makeReply(this, extra)));
|
|
59
|
-
},
|
|
60
|
-
replyWithMarkdownV2(markdown, extra) {
|
|
61
|
-
this.assert(this.chat, 'replyWithMarkdownV2');
|
|
62
|
-
return this.telegram.sendMessage(this.chat.id, markdown, Object.assign({ parse_mode: 'MarkdownV2' }, makeReply(this, extra)));
|
|
63
|
-
},
|
|
64
|
-
replyWithMediaGroup(media, extra) {
|
|
65
|
-
this.assert(this.chat, 'replyWithMediaGroup');
|
|
66
|
-
return this.telegram.sendMediaGroup(this.chat.id, media, makeReply(this, extra));
|
|
67
|
-
},
|
|
68
|
-
replyWithPhoto(photo, extra) {
|
|
69
|
-
this.assert(this.chat, 'replyWithPhoto');
|
|
70
|
-
return this.telegram.sendPhoto(this.chat.id, photo, makeReply(this, extra));
|
|
71
|
-
},
|
|
72
|
-
replyWithPoll(question, options, extra) {
|
|
73
|
-
this.assert(this.chat, 'replyWithPoll');
|
|
74
|
-
return this.telegram.sendPoll(this.chat.id, question, options, makeReply(this, extra));
|
|
75
|
-
},
|
|
76
|
-
replyWithQuiz(question, options, extra) {
|
|
77
|
-
this.assert(this.chat, 'replyWithQuiz');
|
|
78
|
-
return this.telegram.sendQuiz(this.chat.id, question, options, makeReply(this, extra));
|
|
79
|
-
},
|
|
80
|
-
replyWithSticker(sticker, extra) {
|
|
81
|
-
this.assert(this.chat, 'replyWithSticker');
|
|
82
|
-
return this.telegram.sendSticker(this.chat.id, sticker, makeReply(this, extra));
|
|
83
|
-
},
|
|
84
|
-
replyWithVenue(latitude, longitude, title, address, extra) {
|
|
85
|
-
this.assert(this.chat, 'replyWithVenue');
|
|
86
|
-
return this.telegram.sendVenue(this.chat.id, latitude, longitude, title, address, makeReply(this, extra));
|
|
87
|
-
},
|
|
88
|
-
replyWithVideo(video, extra) {
|
|
89
|
-
this.assert(this.chat, 'replyWithVideo');
|
|
90
|
-
return this.telegram.sendVideo(this.chat.id, video, makeReply(this, extra));
|
|
91
|
-
},
|
|
92
|
-
replyWithVideoNote(videoNote, extra) {
|
|
93
|
-
this.assert(this.chat, 'replyWithVideoNote');
|
|
94
|
-
return this.telegram.sendVideoNote(this.chat.id, videoNote, makeReply(this, extra));
|
|
95
|
-
},
|
|
96
|
-
replyWithVoice(voice, extra) {
|
|
97
|
-
this.assert(this.chat, 'replyWithVoice');
|
|
98
|
-
return this.telegram.sendVoice(this.chat.id, voice, makeReply(this, extra));
|
|
99
|
-
},
|
|
100
|
-
replyWithRichMessage(options) {
|
|
101
|
-
this.assert(this.chat, 'replyWithRichMessage');
|
|
102
|
-
const { text, parseMode = 'HTML', buttons, disableLinkPreview, extra } = options;
|
|
103
|
-
return this.telegram.sendMessage(this.chat.id, text, makeReply(this, Object.assign({ parse_mode: parseMode, link_preview_options: disableLinkPreview ? { is_disabled: true } : undefined, reply_markup: (buttons === null || buttons === void 0 ? void 0 : buttons.length) ? { inline_keyboard: buttons } : undefined }, extra)));
|
|
104
|
-
},
|
|
105
|
-
replyWithRichMessageContent(richMessage, extra) {
|
|
106
|
-
this.assert(this.chat, 'replyWithRichMessageContent');
|
|
107
|
-
return this.telegram.sendRichMessage(this.chat.id, richMessage, makeReply(this, extra));
|
|
108
|
-
},
|
|
109
|
-
};
|
|
110
|
-
/**
|
|
111
|
-
* Sets up Context to use the new reply methods.
|
|
112
|
-
* This middleware makes `ctx.reply()` and `ctx.replyWith*()` methods will actually reply to the message they are replying to.
|
|
113
|
-
* Use `ctx.sendMessage()` to send a message in chat without replying to it.
|
|
114
|
-
*
|
|
115
|
-
* If the message to reply is deleted, `reply()` will send a normal message.
|
|
116
|
-
* If the update is not a message and we are unable to reply, `reply()` will send a normal message.
|
|
117
|
-
*/
|
|
118
|
-
function useNewReplies() {
|
|
119
|
-
return (ctx, next) => {
|
|
120
|
-
ctx.reply = replyContext.reply;
|
|
121
|
-
ctx.replyWithPhoto = replyContext.replyWithPhoto;
|
|
122
|
-
ctx.replyWithMediaGroup = replyContext.replyWithMediaGroup;
|
|
123
|
-
ctx.replyWithAudio = replyContext.replyWithAudio;
|
|
124
|
-
ctx.replyWithDice = replyContext.replyWithDice;
|
|
125
|
-
ctx.replyWithDocument = replyContext.replyWithDocument;
|
|
126
|
-
ctx.replyWithSticker = replyContext.replyWithSticker;
|
|
127
|
-
ctx.replyWithVideo = replyContext.replyWithVideo;
|
|
128
|
-
ctx.replyWithAnimation = replyContext.replyWithAnimation;
|
|
129
|
-
ctx.replyWithVideoNote = replyContext.replyWithVideoNote;
|
|
130
|
-
ctx.replyWithInvoice = replyContext.replyWithInvoice;
|
|
131
|
-
ctx.replyWithGame = replyContext.replyWithGame;
|
|
132
|
-
ctx.replyWithVoice = replyContext.replyWithVoice;
|
|
133
|
-
ctx.replyWithPoll = replyContext.replyWithPoll;
|
|
134
|
-
ctx.replyWithQuiz = replyContext.replyWithQuiz;
|
|
135
|
-
ctx.replyWithChatAction = replyContext.replyWithChatAction;
|
|
136
|
-
ctx.replyWithLocation = replyContext.replyWithLocation;
|
|
137
|
-
ctx.replyWithVenue = replyContext.replyWithVenue;
|
|
138
|
-
ctx.replyWithContact = replyContext.replyWithContact;
|
|
139
|
-
ctx.replyWithMarkdown = replyContext.replyWithMarkdown;
|
|
140
|
-
ctx.replyWithMarkdownV2 = replyContext.replyWithMarkdownV2;
|
|
141
|
-
ctx.replyWithHTML = replyContext.replyWithHTML;
|
|
142
|
-
ctx.replyWithRichMessage = replyContext.replyWithRichMessage;
|
|
143
|
-
ctx.replyWithRichMessageContent = replyContext.replyWithRichMessageContent;
|
|
144
|
-
return next();
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
exports.useNewReplies = useNewReplies;
|
package/src/markup.js
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.inlineKeyboard = exports.keyboard = exports.forceReply = exports.removeKeyboard = exports.button = exports.Markup = void 0;
|
|
27
|
-
const check_1 = require("./core/helpers/check");
|
|
28
|
-
class Markup {
|
|
29
|
-
constructor(reply_markup) {
|
|
30
|
-
this.reply_markup = reply_markup;
|
|
31
|
-
}
|
|
32
|
-
selective(value = true) {
|
|
33
|
-
return new Markup(Object.assign(Object.assign({}, this.reply_markup), { selective: value }));
|
|
34
|
-
}
|
|
35
|
-
placeholder(placeholder) {
|
|
36
|
-
return new Markup(Object.assign(Object.assign({}, this.reply_markup), { input_field_placeholder: placeholder }));
|
|
37
|
-
}
|
|
38
|
-
resize(value = true) {
|
|
39
|
-
return new Markup(Object.assign(Object.assign({}, this.reply_markup), { resize_keyboard: value }));
|
|
40
|
-
}
|
|
41
|
-
oneTime(value = true) {
|
|
42
|
-
return new Markup(Object.assign(Object.assign({}, this.reply_markup), { one_time_keyboard: value }));
|
|
43
|
-
}
|
|
44
|
-
persistent(value = true) {
|
|
45
|
-
return new Markup(Object.assign(Object.assign({}, this.reply_markup), { is_persistent: value }));
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
exports.Markup = Markup;
|
|
49
|
-
exports.button = __importStar(require("./button"));
|
|
50
|
-
function removeKeyboard() {
|
|
51
|
-
return new Markup({ remove_keyboard: true });
|
|
52
|
-
}
|
|
53
|
-
exports.removeKeyboard = removeKeyboard;
|
|
54
|
-
function forceReply() {
|
|
55
|
-
return new Markup({ force_reply: true });
|
|
56
|
-
}
|
|
57
|
-
exports.forceReply = forceReply;
|
|
58
|
-
function keyboard(buttons, options) {
|
|
59
|
-
const keyboard = buildKeyboard(buttons, Object.assign({ columns: 1 }, options));
|
|
60
|
-
return new Markup({ keyboard });
|
|
61
|
-
}
|
|
62
|
-
exports.keyboard = keyboard;
|
|
63
|
-
function inlineKeyboard(buttons, options) {
|
|
64
|
-
const inlineKeyboard = buildKeyboard(buttons, Object.assign({ columns: buttons.length }, options));
|
|
65
|
-
return new Markup({ inline_keyboard: inlineKeyboard });
|
|
66
|
-
}
|
|
67
|
-
exports.inlineKeyboard = inlineKeyboard;
|
|
68
|
-
function buildKeyboard(buttons, options) {
|
|
69
|
-
const result = [];
|
|
70
|
-
if (!Array.isArray(buttons)) {
|
|
71
|
-
return result;
|
|
72
|
-
}
|
|
73
|
-
if ((0, check_1.is2D)(buttons)) {
|
|
74
|
-
return buttons.map((row) => row.filter((button) => !button.hide));
|
|
75
|
-
}
|
|
76
|
-
const wrapFn = options.wrap !== undefined
|
|
77
|
-
? options.wrap
|
|
78
|
-
: (_btn, _index, currentRow) => currentRow.length >= options.columns;
|
|
79
|
-
let currentRow = [];
|
|
80
|
-
let index = 0;
|
|
81
|
-
for (const btn of buttons.filter((button) => !button.hide)) {
|
|
82
|
-
if (wrapFn(btn, index, currentRow) && currentRow.length > 0) {
|
|
83
|
-
result.push(currentRow);
|
|
84
|
-
currentRow = [];
|
|
85
|
-
}
|
|
86
|
-
currentRow.push(btn);
|
|
87
|
-
index++;
|
|
88
|
-
}
|
|
89
|
-
if (currentRow.length > 0) {
|
|
90
|
-
result.push(currentRow);
|
|
91
|
-
}
|
|
92
|
-
return result;
|
|
93
|
-
}
|
package/src/scenes.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./scenes/index.js"), exports);
|
package/src/session.js
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.isSessionContext = exports.MemorySessionStore = exports.session = void 0;
|
|
16
|
-
const debug_1 = __importDefault(require("debug"));
|
|
17
|
-
const debug = (0, debug_1.default)('telegraf:session');
|
|
18
|
-
/**
|
|
19
|
-
* Returns middleware that adds `ctx.session` for storing arbitrary state per session key.
|
|
20
|
-
*
|
|
21
|
-
* The default `getSessionKey` is `${ctx.from.id}:${ctx.chat.id}`.
|
|
22
|
-
* If either `ctx.from` or `ctx.chat` is `undefined`, default session key and thus `ctx.session` are also `undefined`.
|
|
23
|
-
*
|
|
24
|
-
* > â ī¸ Session data is kept only in memory by default, which means that all data will be lost when the process is terminated.
|
|
25
|
-
* >
|
|
26
|
-
* > If you want to persist data across process restarts, or share it among multiple instances, you should use
|
|
27
|
-
* [@telegraf/session](https://www.npmjs.com/package/@telegraf/session), or pass custom `storage`.
|
|
28
|
-
*
|
|
29
|
-
* @see {@link https://github.com/feathers-studio/telegraf-docs/blob/b694bcc36b4f71fb1cd650a345c2009ab4d2a2a5/guide/session.md Telegraf Docs | Session}
|
|
30
|
-
* @see {@link https://github.com/feathers-studio/telegraf-docs/blob/master/examples/session-bot.ts Example}
|
|
31
|
-
*/
|
|
32
|
-
function session(options) {
|
|
33
|
-
var _a, _b, _c;
|
|
34
|
-
const prop = (_a = options === null || options === void 0 ? void 0 : options.property) !== null && _a !== void 0 ? _a : 'session';
|
|
35
|
-
const getSessionKey = (_b = options === null || options === void 0 ? void 0 : options.getSessionKey) !== null && _b !== void 0 ? _b : defaultGetSessionKey;
|
|
36
|
-
const store = (_c = options === null || options === void 0 ? void 0 : options.store) !== null && _c !== void 0 ? _c : new MemorySessionStore();
|
|
37
|
-
// caches value from store in-memory while simultaneous updates share it
|
|
38
|
-
// when counter reaches 0, the cached ref will be freed from memory
|
|
39
|
-
const cache = new Map();
|
|
40
|
-
// temporarily stores concurrent requests
|
|
41
|
-
const concurrents = new Map();
|
|
42
|
-
// this function must be handled with care
|
|
43
|
-
// read full description on the original PR: https://github.com/telegraf/telegraf/pull/1713
|
|
44
|
-
// make sure to update the tests in test/session.js if you make any changes or fix bugs here
|
|
45
|
-
return (ctx, next) => __awaiter(this, void 0, void 0, function* () {
|
|
46
|
-
var _d;
|
|
47
|
-
const updId = ctx.update.update_id;
|
|
48
|
-
let released = false;
|
|
49
|
-
function releaseChecks() {
|
|
50
|
-
if (released && process.env.EXPERIMENTAL_SESSION_CHECKS)
|
|
51
|
-
throw new Error("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.");
|
|
52
|
-
}
|
|
53
|
-
// because this is async, requests may still race here, but it will get autocorrected at (1)
|
|
54
|
-
// v5 getSessionKey should probably be synchronous to avoid that
|
|
55
|
-
const key = yield getSessionKey(ctx);
|
|
56
|
-
if (!key) {
|
|
57
|
-
// Leaving this here could be useful to check for `prop in ctx` in future middleware
|
|
58
|
-
ctx[prop] = undefined;
|
|
59
|
-
return yield next();
|
|
60
|
-
}
|
|
61
|
-
let cached = cache.get(key);
|
|
62
|
-
if (cached) {
|
|
63
|
-
debug(`(${updId}) found cached session, reusing from cache`);
|
|
64
|
-
++cached.counter;
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
debug(`(${updId}) did not find cached session`);
|
|
68
|
-
// if another concurrent request has already sent a store request, fetch that instead
|
|
69
|
-
let promise = concurrents.get(key);
|
|
70
|
-
if (promise)
|
|
71
|
-
debug(`(${updId}) found a concurrent request, reusing promise`);
|
|
72
|
-
else {
|
|
73
|
-
debug(`(${updId}) fetching from upstream store`);
|
|
74
|
-
promise = store.get(key);
|
|
75
|
-
}
|
|
76
|
-
// synchronously store promise so concurrent requests can share response
|
|
77
|
-
concurrents.set(key, promise);
|
|
78
|
-
const upstream = yield promise;
|
|
79
|
-
// all concurrent awaits will have promise in their closure, safe to remove now
|
|
80
|
-
concurrents.delete(key);
|
|
81
|
-
debug(`(${updId}) updating cache`);
|
|
82
|
-
// another request may have beaten us to the punch
|
|
83
|
-
const c = cache.get(key);
|
|
84
|
-
if (c) {
|
|
85
|
-
// another request did beat us to the punch
|
|
86
|
-
c.counter++;
|
|
87
|
-
// (1) preserve cached reference; in-memory reference is always newer than from store
|
|
88
|
-
cached = c;
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
// we're the first, so we must cache the reference
|
|
92
|
-
cached = { ref: upstream !== null && upstream !== void 0 ? upstream : (_d = options === null || options === void 0 ? void 0 : options.defaultSession) === null || _d === void 0 ? void 0 : _d.call(options, ctx), counter: 1 };
|
|
93
|
-
cache.set(key, cached);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
// TS already knows cached is always defined by this point, but does not guard cached.
|
|
97
|
-
// It will, however, guard `c` here.
|
|
98
|
-
const c = cached;
|
|
99
|
-
let touched = false;
|
|
100
|
-
Object.defineProperty(ctx, prop, {
|
|
101
|
-
get() {
|
|
102
|
-
releaseChecks();
|
|
103
|
-
touched = true;
|
|
104
|
-
return c.ref;
|
|
105
|
-
},
|
|
106
|
-
set(value) {
|
|
107
|
-
releaseChecks();
|
|
108
|
-
touched = true;
|
|
109
|
-
c.ref = value;
|
|
110
|
-
},
|
|
111
|
-
});
|
|
112
|
-
try {
|
|
113
|
-
yield next();
|
|
114
|
-
released = true;
|
|
115
|
-
}
|
|
116
|
-
finally {
|
|
117
|
-
if (--c.counter === 0) {
|
|
118
|
-
// decrement to avoid memory leak
|
|
119
|
-
debug(`(${updId}) refcounter reached 0, removing cached`);
|
|
120
|
-
cache.delete(key);
|
|
121
|
-
}
|
|
122
|
-
debug(`(${updId}) middlewares completed, checking session`);
|
|
123
|
-
// only update store if ctx.session was touched
|
|
124
|
-
if (touched)
|
|
125
|
-
if (c.ref == null) {
|
|
126
|
-
debug(`(${updId}) ctx.${prop} missing, removing from store`);
|
|
127
|
-
yield store.delete(key);
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
debug(`(${updId}) ctx.${prop} found, updating store`);
|
|
131
|
-
yield store.set(key, c.ref);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
exports.session = session;
|
|
137
|
-
function defaultGetSessionKey(ctx) {
|
|
138
|
-
var _a, _b;
|
|
139
|
-
const fromId = (_a = ctx.from) === null || _a === void 0 ? void 0 : _a.id;
|
|
140
|
-
const chatId = (_b = ctx.chat) === null || _b === void 0 ? void 0 : _b.id;
|
|
141
|
-
if (fromId == null || chatId == null)
|
|
142
|
-
return undefined;
|
|
143
|
-
return `${fromId}:${chatId}`;
|
|
144
|
-
}
|
|
145
|
-
/** @deprecated Use `Map` */
|
|
146
|
-
class MemorySessionStore {
|
|
147
|
-
constructor(ttl = Infinity) {
|
|
148
|
-
this.ttl = ttl;
|
|
149
|
-
this.store = new Map();
|
|
150
|
-
}
|
|
151
|
-
get(name) {
|
|
152
|
-
const entry = this.store.get(name);
|
|
153
|
-
if (entry == null) {
|
|
154
|
-
return undefined;
|
|
155
|
-
}
|
|
156
|
-
else if (entry.expires < Date.now()) {
|
|
157
|
-
this.delete(name);
|
|
158
|
-
return undefined;
|
|
159
|
-
}
|
|
160
|
-
return entry.session;
|
|
161
|
-
}
|
|
162
|
-
set(name, value) {
|
|
163
|
-
const now = Date.now();
|
|
164
|
-
this.store.set(name, { session: value, expires: now + this.ttl });
|
|
165
|
-
}
|
|
166
|
-
delete(name) {
|
|
167
|
-
this.store.delete(name);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
exports.MemorySessionStore = MemorySessionStore;
|
|
171
|
-
/** @deprecated session can use custom properties now. Directly use `'session' in ctx` instead */
|
|
172
|
-
function isSessionContext(ctx) {
|
|
173
|
-
return 'session' in ctx;
|
|
174
|
-
}
|
|
175
|
-
exports.isSessionContext = isSessionContext;
|
package/src/types.js
DELETED
package/src/utils.js
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.argsParser = void 0;
|
|
4
|
-
var args_1 = require("./core/helpers/args");
|
|
5
|
-
Object.defineProperty(exports, "argsParser", { enumerable: true, get: function () { return args_1.argsParser; } });
|