@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/lib/tgify.js
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
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 () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.Tgify = void 0;
|
|
40
|
+
const crypto = __importStar(require("crypto"));
|
|
41
|
+
const http = __importStar(require("http"));
|
|
42
|
+
const https = __importStar(require("https"));
|
|
43
|
+
const composer_1 = require("./composer");
|
|
44
|
+
const compact_1 = require("./core/helpers/compact");
|
|
45
|
+
const context_1 = __importDefault(require("./context"));
|
|
46
|
+
const debug_1 = __importDefault(require("debug"));
|
|
47
|
+
const webhook_1 = __importDefault(require("./core/network/webhook"));
|
|
48
|
+
const polling_1 = require("./core/network/polling");
|
|
49
|
+
const p_timeout_1 = __importDefault(require("p-timeout"));
|
|
50
|
+
const telegram_1 = __importDefault(require("./telegram"));
|
|
51
|
+
const url_1 = require("url");
|
|
52
|
+
const safeCompare = require("safe-compare");
|
|
53
|
+
const debug = (0, debug_1.default)('tgify:main');
|
|
54
|
+
const DEFAULT_OPTIONS = {
|
|
55
|
+
telegram: {},
|
|
56
|
+
handlerTimeout: 90000, // 90s in ms
|
|
57
|
+
contextType: context_1.default,
|
|
58
|
+
};
|
|
59
|
+
function always(x) {
|
|
60
|
+
return () => x;
|
|
61
|
+
}
|
|
62
|
+
const anoop = always(Promise.resolve());
|
|
63
|
+
const TOKEN_HEADER = 'x-telegram-bot-api-secret-token';
|
|
64
|
+
class Tgify extends composer_1.Composer {
|
|
65
|
+
constructor(token, options) {
|
|
66
|
+
super();
|
|
67
|
+
this.context = {};
|
|
68
|
+
/** Assign to this to customise the webhook filter middleware.
|
|
69
|
+
* `{ path, secretToken }` will be bound to this rather than the Tgify instance.
|
|
70
|
+
* Remember to assign a regular function and not an arrow function so it's bindable.
|
|
71
|
+
*/
|
|
72
|
+
this.webhookFilter = function (req) {
|
|
73
|
+
const debug = (0, debug_1.default)('telegraf:webhook');
|
|
74
|
+
if (req.method === 'POST') {
|
|
75
|
+
if (safeCompare(this.path, req.url)) {
|
|
76
|
+
// no need to check if secret_token was not set
|
|
77
|
+
if (!this.secretToken)
|
|
78
|
+
return true;
|
|
79
|
+
else {
|
|
80
|
+
const token = req.headers[TOKEN_HEADER];
|
|
81
|
+
if (safeCompare(this.secretToken, token))
|
|
82
|
+
return true;
|
|
83
|
+
else
|
|
84
|
+
debug('Secret token does not match:', token, this.secretToken);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
else
|
|
88
|
+
debug('Path does not match:', req.url, this.path);
|
|
89
|
+
}
|
|
90
|
+
else
|
|
91
|
+
debug('Unexpected request method, not POST. Received:', req.method);
|
|
92
|
+
return false;
|
|
93
|
+
};
|
|
94
|
+
this.handleError = (err, ctx) => {
|
|
95
|
+
// set exit code to emulate `warn-with-error-code` behavior of
|
|
96
|
+
// https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode
|
|
97
|
+
// to prevent a clean exit despite an error being thrown
|
|
98
|
+
process.exitCode = 1;
|
|
99
|
+
console.error('Unhandled error while processing', ctx.update);
|
|
100
|
+
throw err;
|
|
101
|
+
};
|
|
102
|
+
// @ts-expect-error Trust me, TS
|
|
103
|
+
this.options = {
|
|
104
|
+
...DEFAULT_OPTIONS,
|
|
105
|
+
...(0, compact_1.compactOptions)(options),
|
|
106
|
+
};
|
|
107
|
+
this.telegram = new telegram_1.default(token, this.options.telegram);
|
|
108
|
+
debug('Created a `Tgify` instance');
|
|
109
|
+
}
|
|
110
|
+
get token() {
|
|
111
|
+
return this.telegram.token;
|
|
112
|
+
}
|
|
113
|
+
/** @deprecated use `ctx.telegram.webhookReply` */
|
|
114
|
+
set webhookReply(webhookReply) {
|
|
115
|
+
this.telegram.webhookReply = webhookReply;
|
|
116
|
+
}
|
|
117
|
+
/** @deprecated use `ctx.telegram.webhookReply` */
|
|
118
|
+
get webhookReply() {
|
|
119
|
+
return this.telegram.webhookReply;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* _Override_ error handling
|
|
123
|
+
*/
|
|
124
|
+
catch(handler) {
|
|
125
|
+
this.handleError = handler;
|
|
126
|
+
return this;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* You must call `bot.telegram.setWebhook` for this to work.
|
|
130
|
+
* You should probably use {@link Tgify.createWebhook} instead.
|
|
131
|
+
*/
|
|
132
|
+
webhookCallback(path = '/', opts = {}) {
|
|
133
|
+
const { secretToken } = opts;
|
|
134
|
+
return (0, webhook_1.default)(this.webhookFilter.bind({ hookPath: path, path, secretToken }), (update, res) => this.handleUpdate(update, res));
|
|
135
|
+
}
|
|
136
|
+
getDomainOpts(opts) {
|
|
137
|
+
const protocol = opts.local ? 'http:' : 'https:';
|
|
138
|
+
const path = opts.path || `/telegraf/${this.secretPathComponent()}`;
|
|
139
|
+
const domain = new url_1.URL(`${protocol}//${opts.domain}`).host;
|
|
140
|
+
const url = `${protocol}//${domain}${path}`;
|
|
141
|
+
return { domain, path, url };
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Specify a url to receive incoming updates via webhook.
|
|
145
|
+
* Returns an Express-style middleware you can pass to app.use()
|
|
146
|
+
*/
|
|
147
|
+
async createWebhook(opts) {
|
|
148
|
+
const { domain, path, local, ...extra } = opts;
|
|
149
|
+
const domainOpts = this.getDomainOpts({ local, domain, path });
|
|
150
|
+
await this.telegram.setWebhook(domainOpts.url, extra);
|
|
151
|
+
debug(`Webhook set to ${domainOpts.url}`);
|
|
152
|
+
return this.webhookCallback(domainOpts.path, {
|
|
153
|
+
secretToken: extra.secret_token,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
startPolling(allowedUpdates = []) {
|
|
157
|
+
this.polling = new polling_1.Polling(this.telegram, allowedUpdates);
|
|
158
|
+
return this.polling.loop(async (update) => {
|
|
159
|
+
await this.handleUpdate(update);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
startWebhook(path, tlsOptions, port, host, cb, secretToken) {
|
|
163
|
+
const webhookCb = this.webhookCallback(path, { secretToken });
|
|
164
|
+
const callback = typeof cb === 'function'
|
|
165
|
+
? (req, res) => webhookCb(req, res, () => cb(req, res))
|
|
166
|
+
: webhookCb;
|
|
167
|
+
this.webhookServer =
|
|
168
|
+
tlsOptions != null
|
|
169
|
+
? https.createServer(tlsOptions, callback)
|
|
170
|
+
: http.createServer(callback);
|
|
171
|
+
this.webhookServer.listen(port, host, () => {
|
|
172
|
+
debug('Webhook listening on port: %s', port);
|
|
173
|
+
});
|
|
174
|
+
return this;
|
|
175
|
+
}
|
|
176
|
+
secretPathComponent() {
|
|
177
|
+
return crypto
|
|
178
|
+
.createHash('sha3-256')
|
|
179
|
+
.update(this.token)
|
|
180
|
+
.update(process.version) // salt
|
|
181
|
+
.digest('hex');
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* @see https://github.com/telegraf/telegraf/discussions/1344#discussioncomment-335700
|
|
185
|
+
*/
|
|
186
|
+
async launch(config = {},
|
|
187
|
+
/** @experimental */
|
|
188
|
+
onLaunch) {
|
|
189
|
+
var _a, _b;
|
|
190
|
+
const [cfg, onMe] = typeof config === 'function' ? [{}, config] : [config, onLaunch];
|
|
191
|
+
const drop_pending_updates = cfg.dropPendingUpdates;
|
|
192
|
+
const allowed_updates = cfg.allowedUpdates;
|
|
193
|
+
const webhook = cfg.webhook;
|
|
194
|
+
debug('Connecting to Telegram');
|
|
195
|
+
(_a = this.botInfo) !== null && _a !== void 0 ? _a : (this.botInfo = await this.telegram.getMe());
|
|
196
|
+
onMe === null || onMe === void 0 ? void 0 : onMe();
|
|
197
|
+
debug(`Launching @${this.botInfo.username}`);
|
|
198
|
+
if (webhook === undefined) {
|
|
199
|
+
await this.telegram.deleteWebhook({ drop_pending_updates });
|
|
200
|
+
debug('Bot started with long polling');
|
|
201
|
+
await this.startPolling(allowed_updates);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
const domainOpts = this.getDomainOpts({
|
|
205
|
+
local: webhook.local,
|
|
206
|
+
domain: webhook.domain,
|
|
207
|
+
path: (_b = webhook.path) !== null && _b !== void 0 ? _b : webhook.hookPath,
|
|
208
|
+
});
|
|
209
|
+
const { tlsOptions, port, host, cb, secretToken } = webhook;
|
|
210
|
+
this.startWebhook(domainOpts.path, tlsOptions, port, host, cb, secretToken);
|
|
211
|
+
await this.telegram.setWebhook(domainOpts.url, {
|
|
212
|
+
drop_pending_updates: drop_pending_updates,
|
|
213
|
+
allowed_updates: allowed_updates,
|
|
214
|
+
ip_address: webhook.ipAddress,
|
|
215
|
+
max_connections: webhook.maxConnections,
|
|
216
|
+
secret_token: webhook.secretToken,
|
|
217
|
+
certificate: webhook.certificate,
|
|
218
|
+
});
|
|
219
|
+
debug(`Bot started with webhook @ ${domainOpts.url}`);
|
|
220
|
+
}
|
|
221
|
+
stop(reason = 'unspecified') {
|
|
222
|
+
var _a, _b;
|
|
223
|
+
debug('Stopping bot... Reason:', reason);
|
|
224
|
+
// https://github.com/telegraf/telegraf/pull/1224#issuecomment-742693770
|
|
225
|
+
if (this.polling === undefined && this.webhookServer === undefined) {
|
|
226
|
+
throw new Error('Bot is not running!');
|
|
227
|
+
}
|
|
228
|
+
(_a = this.webhookServer) === null || _a === void 0 ? void 0 : _a.close();
|
|
229
|
+
(_b = this.polling) === null || _b === void 0 ? void 0 : _b.stop();
|
|
230
|
+
}
|
|
231
|
+
async handleUpdate(update, webhookResponse) {
|
|
232
|
+
var _a, _b;
|
|
233
|
+
(_a = this.botInfo) !== null && _a !== void 0 ? _a : (this.botInfo = (debug('Update %d is waiting for `botInfo` to be initialized', update.update_id),
|
|
234
|
+
await ((_b = this.botInfoCall) !== null && _b !== void 0 ? _b : (this.botInfoCall = this.telegram.getMe()))));
|
|
235
|
+
debug('Processing update', update.update_id);
|
|
236
|
+
const tg = new telegram_1.default(this.token, this.telegram.options, webhookResponse);
|
|
237
|
+
const TelegrafContext = this.options.contextType;
|
|
238
|
+
const ctx = new TelegrafContext(update, tg, this.botInfo);
|
|
239
|
+
Object.assign(ctx, this.context);
|
|
240
|
+
try {
|
|
241
|
+
await (0, p_timeout_1.default)(Promise.resolve(this.middleware()(ctx, anoop)), this.options.handlerTimeout);
|
|
242
|
+
}
|
|
243
|
+
catch (err) {
|
|
244
|
+
return await this.handleError(err, ctx);
|
|
245
|
+
}
|
|
246
|
+
finally {
|
|
247
|
+
if ((webhookResponse === null || webhookResponse === void 0 ? void 0 : webhookResponse.writableEnded) === false) {
|
|
248
|
+
webhookResponse.end();
|
|
249
|
+
}
|
|
250
|
+
debug('Finished processing update', update.update_id);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
exports.Tgify = Tgify;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tgify/tgify",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Modern Telegram Bot Framework",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "IATNAOD",
|
|
@@ -94,7 +94,7 @@
|
|
|
94
94
|
},
|
|
95
95
|
"types": "./typings/index.d.ts",
|
|
96
96
|
"dependencies": {
|
|
97
|
-
"@
|
|
97
|
+
"@tgify/types": "^9.2.4",
|
|
98
98
|
"abort-controller": "^3.0.0",
|
|
99
99
|
"debug": "^4.3.5",
|
|
100
100
|
"mri": "^1.2.0",
|
package/src/button.ts
CHANGED
|
@@ -1,30 +1,32 @@
|
|
|
1
1
|
import {
|
|
2
|
-
InlineKeyboardButton,
|
|
3
2
|
KeyboardButton,
|
|
3
|
+
InlineKeyboardButton,
|
|
4
4
|
KeyboardButtonRequestChat,
|
|
5
5
|
KeyboardButtonRequestUsers,
|
|
6
6
|
} from './core/types/typegram'
|
|
7
7
|
|
|
8
8
|
type Hideable<B> = B & { hide: boolean }
|
|
9
9
|
|
|
10
|
+
|
|
10
11
|
export function text(
|
|
11
12
|
text: string,
|
|
12
|
-
hide = false
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
hide = false,
|
|
14
|
+
extra?: Pick<KeyboardButton.Text, 'style' | 'icon_custom_emoji_id'>,
|
|
15
|
+
): Hideable<KeyboardButton.Text> {
|
|
16
|
+
return { text, hide, ...extra }
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
export function contactRequest(
|
|
18
20
|
text: string,
|
|
19
21
|
hide = false
|
|
20
|
-
): Hideable<KeyboardButton.
|
|
22
|
+
): Hideable<KeyboardButton.RequestContact> {
|
|
21
23
|
return { text, request_contact: true, hide }
|
|
22
24
|
}
|
|
23
25
|
|
|
24
26
|
export function locationRequest(
|
|
25
27
|
text: string,
|
|
26
28
|
hide = false
|
|
27
|
-
): Hideable<KeyboardButton.
|
|
29
|
+
): Hideable<KeyboardButton.RequestLocation> {
|
|
28
30
|
return { text, request_location: true, hide }
|
|
29
31
|
}
|
|
30
32
|
|
|
@@ -32,7 +34,7 @@ export function pollRequest(
|
|
|
32
34
|
text: string,
|
|
33
35
|
type?: 'quiz' | 'regular',
|
|
34
36
|
hide = false
|
|
35
|
-
): Hideable<KeyboardButton.
|
|
37
|
+
): Hideable<KeyboardButton.RequestPoll> {
|
|
36
38
|
return { text, request_poll: { type }, hide }
|
|
37
39
|
}
|
|
38
40
|
|
|
@@ -42,7 +44,7 @@ export function userRequest(
|
|
|
42
44
|
request_id: number,
|
|
43
45
|
extra?: Omit<KeyboardButtonRequestUsers, 'request_id' | 'text'>,
|
|
44
46
|
hide = false
|
|
45
|
-
): Hideable<KeyboardButton.
|
|
47
|
+
): Hideable<KeyboardButton.RequestUsers> {
|
|
46
48
|
return {
|
|
47
49
|
text,
|
|
48
50
|
request_users: { request_id, ...extra },
|
|
@@ -59,7 +61,7 @@ export function botRequest(
|
|
|
59
61
|
'request_id' | 'user_is_bot' | 'text'
|
|
60
62
|
>,
|
|
61
63
|
hide = false
|
|
62
|
-
): Hideable<KeyboardButton.
|
|
64
|
+
): Hideable<KeyboardButton.RequestUsers> {
|
|
63
65
|
return {
|
|
64
66
|
text,
|
|
65
67
|
request_users: { request_id, user_is_bot: true, ...extra },
|
|
@@ -78,7 +80,7 @@ export function groupRequest(
|
|
|
78
80
|
request_id: number,
|
|
79
81
|
extra?: KeyboardButtonRequestGroup,
|
|
80
82
|
hide = false
|
|
81
|
-
): Hideable<KeyboardButton.
|
|
83
|
+
): Hideable<KeyboardButton.RequestChat> {
|
|
82
84
|
return {
|
|
83
85
|
text,
|
|
84
86
|
request_chat: { request_id, chat_is_channel: false, ...extra },
|
|
@@ -97,7 +99,7 @@ export function channelRequest(
|
|
|
97
99
|
request_id: number,
|
|
98
100
|
extra?: KeyboardButtonRequestChannel,
|
|
99
101
|
hide = false
|
|
100
|
-
): Hideable<KeyboardButton.
|
|
102
|
+
): Hideable<KeyboardButton.RequestChat> {
|
|
101
103
|
return {
|
|
102
104
|
text,
|
|
103
105
|
request_chat: { request_id, chat_is_channel: true, ...extra },
|
|
@@ -108,47 +110,53 @@ export function channelRequest(
|
|
|
108
110
|
export function url(
|
|
109
111
|
text: string,
|
|
110
112
|
url: string,
|
|
111
|
-
hide = false
|
|
112
|
-
|
|
113
|
-
|
|
113
|
+
hide = false,
|
|
114
|
+
extra?: Pick<InlineKeyboardButton.Url, 'style' | 'icon_custom_emoji_id'>,
|
|
115
|
+
): Hideable<InlineKeyboardButton.Url> {
|
|
116
|
+
return { text, url, hide, ...extra }
|
|
114
117
|
}
|
|
115
118
|
|
|
116
119
|
export function callback(
|
|
117
120
|
text: string,
|
|
118
121
|
data: string,
|
|
119
|
-
hide = false
|
|
120
|
-
|
|
121
|
-
|
|
122
|
+
hide = false,
|
|
123
|
+
extra?: Pick<InlineKeyboardButton.Callback, 'style' | 'icon_custom_emoji_id'>,
|
|
124
|
+
): Hideable<InlineKeyboardButton.Callback> {
|
|
125
|
+
return { text, callback_data: data, hide, ...extra }
|
|
122
126
|
}
|
|
123
127
|
|
|
124
128
|
export function switchToChat(
|
|
125
129
|
text: string,
|
|
126
130
|
value: string,
|
|
127
|
-
hide = false
|
|
128
|
-
|
|
129
|
-
|
|
131
|
+
hide = false,
|
|
132
|
+
extra?: Pick<InlineKeyboardButton.SwitchInline, 'style' | 'icon_custom_emoji_id'>,
|
|
133
|
+
): Hideable<InlineKeyboardButton.SwitchInline> {
|
|
134
|
+
return { text, switch_inline_query: value, hide, ...extra }
|
|
130
135
|
}
|
|
131
136
|
|
|
132
137
|
export function switchToCurrentChat(
|
|
133
138
|
text: string,
|
|
134
139
|
value: string,
|
|
135
|
-
hide = false
|
|
136
|
-
|
|
137
|
-
|
|
140
|
+
hide = false,
|
|
141
|
+
extra?: Pick<InlineKeyboardButton.SwitchInlineCurrentChat, 'style' | 'icon_custom_emoji_id'>,
|
|
142
|
+
): Hideable<InlineKeyboardButton.SwitchInlineCurrentChat> {
|
|
143
|
+
return { text, switch_inline_query_current_chat: value, hide, ...extra }
|
|
138
144
|
}
|
|
139
145
|
|
|
140
146
|
export function game(
|
|
141
147
|
text: string,
|
|
142
|
-
hide = false
|
|
143
|
-
|
|
144
|
-
|
|
148
|
+
hide = false,
|
|
149
|
+
extra?: Pick<InlineKeyboardButton.Game, 'style' | 'icon_custom_emoji_id'>,
|
|
150
|
+
): Hideable<InlineKeyboardButton.Game> {
|
|
151
|
+
return { text, callback_game: {}, hide, ...extra }
|
|
145
152
|
}
|
|
146
153
|
|
|
147
154
|
export function pay(
|
|
148
155
|
text: string,
|
|
149
|
-
hide = false
|
|
150
|
-
|
|
151
|
-
|
|
156
|
+
hide = false,
|
|
157
|
+
extra?: Pick<InlineKeyboardButton.Pay, 'style' | 'icon_custom_emoji_id'>,
|
|
158
|
+
): Hideable<InlineKeyboardButton.Pay> {
|
|
159
|
+
return { text, pay: true, hide, ...extra }
|
|
152
160
|
}
|
|
153
161
|
|
|
154
162
|
export function login(
|
|
@@ -159,24 +167,28 @@ export function login(
|
|
|
159
167
|
bot_username?: string
|
|
160
168
|
request_write_access?: boolean
|
|
161
169
|
} = {},
|
|
162
|
-
hide = false
|
|
163
|
-
|
|
170
|
+
hide = false,
|
|
171
|
+
extra?: Pick<InlineKeyboardButton.Login, 'style' | 'icon_custom_emoji_id'>,
|
|
172
|
+
): Hideable<InlineKeyboardButton.Login> {
|
|
164
173
|
return {
|
|
165
174
|
text,
|
|
166
|
-
login_url: { ...opts, url },
|
|
167
175
|
hide,
|
|
176
|
+
login_url: { ...opts, url },
|
|
177
|
+
...extra
|
|
168
178
|
}
|
|
169
179
|
}
|
|
170
180
|
|
|
181
|
+
// works as both InlineKeyboardButton and KeyboardButton
|
|
171
182
|
export function webApp(
|
|
172
183
|
text: string,
|
|
173
184
|
url: string,
|
|
174
|
-
hide = false
|
|
175
|
-
|
|
176
|
-
): Hideable<InlineKeyboardButton.
|
|
185
|
+
hide = false,
|
|
186
|
+
extra?: Pick<InlineKeyboardButton.WebApp, 'style' | 'icon_custom_emoji_id'>,
|
|
187
|
+
): Hideable<InlineKeyboardButton.WebApp> {
|
|
177
188
|
return {
|
|
178
189
|
text,
|
|
179
|
-
web_app: { url },
|
|
180
190
|
hide,
|
|
191
|
+
web_app: { url },
|
|
192
|
+
...extra
|
|
181
193
|
}
|
|
182
194
|
}
|
package/src/composer.ts
CHANGED
|
@@ -110,10 +110,10 @@ export class Composer<C extends Context> implements MiddlewareObj<C> {
|
|
|
110
110
|
...fns: NonemptyReadonlyArray<
|
|
111
111
|
Middleware<
|
|
112
112
|
Filter extends tt.MessageSubType
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
113
|
+
? MatchedContext<C, Filter>
|
|
114
|
+
: Filter extends tt.UpdateType | Guard<C['update']>
|
|
115
|
+
? FilteredContext<C, Filter>
|
|
116
|
+
: never
|
|
117
117
|
>
|
|
118
118
|
>
|
|
119
119
|
): this {
|
|
@@ -209,8 +209,8 @@ export class Composer<C extends Context> implements MiddlewareObj<C> {
|
|
|
209
209
|
|
|
210
210
|
private entity<
|
|
211
211
|
T extends 'message' | 'channel_post' | tt.MessageSubType =
|
|
212
|
-
|
|
213
|
-
|
|
212
|
+
| 'message'
|
|
213
|
+
| 'channel_post',
|
|
214
214
|
>(
|
|
215
215
|
predicate:
|
|
216
216
|
| MaybeArray<string>
|
|
@@ -497,12 +497,12 @@ export class Composer<C extends Context> implements MiddlewareObj<C> {
|
|
|
497
497
|
// https://github.com/microsoft/TypeScript/pull/51502
|
|
498
498
|
typeof filter !== 'string'
|
|
499
499
|
? // filter is a type guard
|
|
500
|
-
|
|
500
|
+
filter(update)
|
|
501
501
|
: // check if filter is the update type
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
502
|
+
filter in update ||
|
|
503
|
+
// check if filter is the msg type
|
|
504
|
+
// TODO: remove in v5!
|
|
505
|
+
('message' in update && filter in update.message)
|
|
506
506
|
) {
|
|
507
507
|
return true
|
|
508
508
|
}
|
|
@@ -523,8 +523,8 @@ export class Composer<C extends Context> implements MiddlewareObj<C> {
|
|
|
523
523
|
private static entity<
|
|
524
524
|
C extends Context,
|
|
525
525
|
T extends 'message' | 'channel_post' | tt.MessageSubType =
|
|
526
|
-
|
|
527
|
-
|
|
526
|
+
| 'message'
|
|
527
|
+
| 'channel_post',
|
|
528
528
|
>(
|
|
529
529
|
predicate:
|
|
530
530
|
| MaybeArray<string>
|
package/src/context.ts
CHANGED
|
@@ -245,9 +245,9 @@ export class Context<U extends Deunionize<tg.Update> = tg.Update> {
|
|
|
245
245
|
// https://github.com/microsoft/TypeScript/pull/51502
|
|
246
246
|
typeof filter !== 'string'
|
|
247
247
|
? // filter is a type guard
|
|
248
|
-
|
|
248
|
+
filter(this.update)
|
|
249
249
|
: // check if filter is the update type
|
|
250
|
-
|
|
250
|
+
filter in this.update
|
|
251
251
|
)
|
|
252
252
|
return true
|
|
253
253
|
|
|
@@ -856,7 +856,7 @@ export class Context<U extends Deunionize<tg.Update> = tg.Update> {
|
|
|
856
856
|
/**
|
|
857
857
|
* @see https://core.telegram.org/bots/api#sendpoll
|
|
858
858
|
*/
|
|
859
|
-
sendPoll(poll: string, options: readonly
|
|
859
|
+
sendPoll(poll: string, options: readonly tg.InputPollOption[], extra?: tt.ExtraPoll) {
|
|
860
860
|
this.assert(this.chat, 'sendPoll')
|
|
861
861
|
return this.telegram.sendPoll(this.chat.id, poll, options, {
|
|
862
862
|
message_thread_id: getThreadId(this),
|
|
@@ -874,7 +874,7 @@ export class Context<U extends Deunionize<tg.Update> = tg.Update> {
|
|
|
874
874
|
/**
|
|
875
875
|
* @see https://core.telegram.org/bots/api#sendpoll
|
|
876
876
|
*/
|
|
877
|
-
sendQuiz(quiz: string, options: readonly
|
|
877
|
+
sendQuiz(quiz: string, options: readonly tg.InputPollOption[], extra?: tt.ExtraPoll) {
|
|
878
878
|
this.assert(this.chat, 'sendQuiz')
|
|
879
879
|
return this.telegram.sendQuiz(this.chat.id, quiz, options, {
|
|
880
880
|
message_thread_id: getThreadId(this),
|
|
@@ -1518,8 +1518,8 @@ type UpdateTypes<U extends Deunionize<tg.Update>> = Extract<
|
|
|
1518
1518
|
|
|
1519
1519
|
export type GetUpdateContent<U extends tg.Update> =
|
|
1520
1520
|
U extends tg.Update.CallbackQueryUpdate
|
|
1521
|
-
|
|
1522
|
-
|
|
1521
|
+
? U['callback_query']['message']
|
|
1522
|
+
: U[UpdateTypes<U>]
|
|
1523
1523
|
|
|
1524
1524
|
type Getter<U extends Deunionize<tg.Update>, P extends string> = PropOr<
|
|
1525
1525
|
GetUpdateContent<U>,
|
|
@@ -1575,22 +1575,22 @@ function getMessageFromAnySource<U extends tg.Update>(ctx: Context<U>) {
|
|
|
1575
1575
|
type GetUserFromAnySource<U extends tg.Update> =
|
|
1576
1576
|
// check if it's a message type with `from`
|
|
1577
1577
|
GetMsg<U> extends { from: tg.User }
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1578
|
+
? tg.User
|
|
1579
|
+
: U extends // these updates have `from`
|
|
1580
|
+
| tg.Update.CallbackQueryUpdate
|
|
1581
|
+
| tg.Update.InlineQueryUpdate
|
|
1582
|
+
| tg.Update.ShippingQueryUpdate
|
|
1583
|
+
| tg.Update.PreCheckoutQueryUpdate
|
|
1584
|
+
| tg.Update.ChosenInlineResultUpdate
|
|
1585
|
+
| tg.Update.ChatMemberUpdate
|
|
1586
|
+
| tg.Update.MyChatMemberUpdate
|
|
1587
|
+
| tg.Update.ChatJoinRequestUpdate
|
|
1588
|
+
// these updates have `user`
|
|
1589
|
+
| tg.Update.MessageReactionUpdate
|
|
1590
|
+
| tg.Update.PollAnswerUpdate
|
|
1591
|
+
| tg.Update.ChatBoostUpdate
|
|
1592
|
+
? tg.User
|
|
1593
|
+
: undefined
|
|
1594
1594
|
|
|
1595
1595
|
function getUserFromAnySource<U extends tg.Update>(ctx: Context<U>) {
|
|
1596
1596
|
if (ctx.callbackQuery) return ctx.callbackQuery.from
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
|
|
2
|
-
import { MessageEntity, User } from '@
|
|
2
|
+
import { MessageEntity, User } from '@tgify/types'
|
|
3
3
|
import { Any, zip } from './util'
|
|
4
4
|
|
|
5
5
|
export type Nestable<Kind extends string> =
|
|
@@ -19,8 +19,7 @@ export interface FmtString<Brand extends string> {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
export class FmtString<Brand extends string = string>
|
|
22
|
-
implements FmtString<Brand>
|
|
23
|
-
{
|
|
22
|
+
implements FmtString<Brand> {
|
|
24
23
|
constructor(
|
|
25
24
|
public text: string,
|
|
26
25
|
entities?: MessageEntity[]
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import * as Typegram from '@
|
|
1
|
+
import * as Typegram from '@tgify/types'
|
|
2
2
|
|
|
3
3
|
// internal type provisions
|
|
4
|
-
export * from '@
|
|
5
|
-
export * from '@
|
|
6
|
-
export * from '@
|
|
7
|
-
export * from '@
|
|
8
|
-
export * from '@
|
|
9
|
-
export * from '@
|
|
10
|
-
export * from '@
|
|
11
|
-
export * from '@
|
|
12
|
-
export * from '@
|
|
13
|
-
export * from '@
|
|
4
|
+
export * from '@tgify/types/api'
|
|
5
|
+
export * from '@tgify/types/inline'
|
|
6
|
+
export * from '@tgify/types/manage'
|
|
7
|
+
export * from '@tgify/types/markup'
|
|
8
|
+
export * from '@tgify/types/message'
|
|
9
|
+
export * from '@tgify/types/methods'
|
|
10
|
+
export * from '@tgify/types/passport'
|
|
11
|
+
export * from '@tgify/types/payment'
|
|
12
|
+
export * from '@tgify/types/settings'
|
|
13
|
+
export * from '@tgify/types/update'
|
|
14
14
|
|
|
15
15
|
// telegraf input file definition
|
|
16
16
|
interface InputFileByPath {
|