@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/lib/session.js
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MemorySessionStore = void 0;
|
|
7
|
+
exports.session = session;
|
|
8
|
+
exports.isSessionContext = isSessionContext;
|
|
9
|
+
const debug_1 = __importDefault(require("debug"));
|
|
10
|
+
const debug = (0, debug_1.default)('telegraf:session');
|
|
11
|
+
/**
|
|
12
|
+
* Returns middleware that adds `ctx.session` for storing arbitrary state per session key.
|
|
13
|
+
*
|
|
14
|
+
* The default `getSessionKey` is `${ctx.from.id}:${ctx.chat.id}`.
|
|
15
|
+
* If either `ctx.from` or `ctx.chat` is `undefined`, default session key and thus `ctx.session` are also `undefined`.
|
|
16
|
+
*
|
|
17
|
+
* > ⚠️ Session data is kept only in memory by default, which means that all data will be lost when the process is terminated.
|
|
18
|
+
* >
|
|
19
|
+
* > If you want to persist data across process restarts, or share it among multiple instances, you should use
|
|
20
|
+
* [@telegraf/session](https://www.npmjs.com/package/@telegraf/session), or pass custom `storage`.
|
|
21
|
+
*
|
|
22
|
+
* @see {@link https://github.com/feathers-studio/telegraf-docs/blob/b694bcc36b4f71fb1cd650a345c2009ab4d2a2a5/guide/session.md Telegraf Docs | Session}
|
|
23
|
+
* @see {@link https://github.com/feathers-studio/telegraf-docs/blob/master/examples/session-bot.ts Example}
|
|
24
|
+
*/
|
|
25
|
+
function session(options) {
|
|
26
|
+
var _a, _b, _c;
|
|
27
|
+
const prop = (_a = options === null || options === void 0 ? void 0 : options.property) !== null && _a !== void 0 ? _a : 'session';
|
|
28
|
+
const getSessionKey = (_b = options === null || options === void 0 ? void 0 : options.getSessionKey) !== null && _b !== void 0 ? _b : defaultGetSessionKey;
|
|
29
|
+
const store = (_c = options === null || options === void 0 ? void 0 : options.store) !== null && _c !== void 0 ? _c : new MemorySessionStore();
|
|
30
|
+
// caches value from store in-memory while simultaneous updates share it
|
|
31
|
+
// when counter reaches 0, the cached ref will be freed from memory
|
|
32
|
+
const cache = new Map();
|
|
33
|
+
// temporarily stores concurrent requests
|
|
34
|
+
const concurrents = new Map();
|
|
35
|
+
// this function must be handled with care
|
|
36
|
+
// read full description on the original PR: https://github.com/telegraf/telegraf/pull/1713
|
|
37
|
+
// make sure to update the tests in test/session.js if you make any changes or fix bugs here
|
|
38
|
+
return async (ctx, next) => {
|
|
39
|
+
var _a;
|
|
40
|
+
const updId = ctx.update.update_id;
|
|
41
|
+
let released = false;
|
|
42
|
+
function releaseChecks() {
|
|
43
|
+
if (released && process.env.EXPERIMENTAL_SESSION_CHECKS)
|
|
44
|
+
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.");
|
|
45
|
+
}
|
|
46
|
+
// because this is async, requests may still race here, but it will get autocorrected at (1)
|
|
47
|
+
// v5 getSessionKey should probably be synchronous to avoid that
|
|
48
|
+
const key = await getSessionKey(ctx);
|
|
49
|
+
if (!key) {
|
|
50
|
+
// Leaving this here could be useful to check for `prop in ctx` in future middleware
|
|
51
|
+
ctx[prop] = undefined;
|
|
52
|
+
return await next();
|
|
53
|
+
}
|
|
54
|
+
let cached = cache.get(key);
|
|
55
|
+
if (cached) {
|
|
56
|
+
debug(`(${updId}) found cached session, reusing from cache`);
|
|
57
|
+
++cached.counter;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
debug(`(${updId}) did not find cached session`);
|
|
61
|
+
// if another concurrent request has already sent a store request, fetch that instead
|
|
62
|
+
let promise = concurrents.get(key);
|
|
63
|
+
if (promise)
|
|
64
|
+
debug(`(${updId}) found a concurrent request, reusing promise`);
|
|
65
|
+
else {
|
|
66
|
+
debug(`(${updId}) fetching from upstream store`);
|
|
67
|
+
promise = store.get(key);
|
|
68
|
+
}
|
|
69
|
+
// synchronously store promise so concurrent requests can share response
|
|
70
|
+
concurrents.set(key, promise);
|
|
71
|
+
const upstream = await promise;
|
|
72
|
+
// all concurrent awaits will have promise in their closure, safe to remove now
|
|
73
|
+
concurrents.delete(key);
|
|
74
|
+
debug(`(${updId}) updating cache`);
|
|
75
|
+
// another request may have beaten us to the punch
|
|
76
|
+
const c = cache.get(key);
|
|
77
|
+
if (c) {
|
|
78
|
+
// another request did beat us to the punch
|
|
79
|
+
c.counter++;
|
|
80
|
+
// (1) preserve cached reference; in-memory reference is always newer than from store
|
|
81
|
+
cached = c;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
// we're the first, so we must cache the reference
|
|
85
|
+
cached = { ref: upstream !== null && upstream !== void 0 ? upstream : (_a = options === null || options === void 0 ? void 0 : options.defaultSession) === null || _a === void 0 ? void 0 : _a.call(options, ctx), counter: 1 };
|
|
86
|
+
cache.set(key, cached);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// TS already knows cached is always defined by this point, but does not guard cached.
|
|
90
|
+
// It will, however, guard `c` here.
|
|
91
|
+
const c = cached;
|
|
92
|
+
let touched = false;
|
|
93
|
+
Object.defineProperty(ctx, prop, {
|
|
94
|
+
get() {
|
|
95
|
+
releaseChecks();
|
|
96
|
+
touched = true;
|
|
97
|
+
return c.ref;
|
|
98
|
+
},
|
|
99
|
+
set(value) {
|
|
100
|
+
releaseChecks();
|
|
101
|
+
touched = true;
|
|
102
|
+
c.ref = value;
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
try {
|
|
106
|
+
await next();
|
|
107
|
+
released = true;
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
if (--c.counter === 0) {
|
|
111
|
+
// decrement to avoid memory leak
|
|
112
|
+
debug(`(${updId}) refcounter reached 0, removing cached`);
|
|
113
|
+
cache.delete(key);
|
|
114
|
+
}
|
|
115
|
+
debug(`(${updId}) middlewares completed, checking session`);
|
|
116
|
+
// only update store if ctx.session was touched
|
|
117
|
+
if (touched)
|
|
118
|
+
if (c.ref == null) {
|
|
119
|
+
debug(`(${updId}) ctx.${prop} missing, removing from store`);
|
|
120
|
+
await store.delete(key);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
debug(`(${updId}) ctx.${prop} found, updating store`);
|
|
124
|
+
await store.set(key, c.ref);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
function defaultGetSessionKey(ctx) {
|
|
130
|
+
var _a, _b;
|
|
131
|
+
const fromId = (_a = ctx.from) === null || _a === void 0 ? void 0 : _a.id;
|
|
132
|
+
const chatId = (_b = ctx.chat) === null || _b === void 0 ? void 0 : _b.id;
|
|
133
|
+
if (fromId == null || chatId == null)
|
|
134
|
+
return undefined;
|
|
135
|
+
return `${fromId}:${chatId}`;
|
|
136
|
+
}
|
|
137
|
+
/** @deprecated Use `Map` */
|
|
138
|
+
class MemorySessionStore {
|
|
139
|
+
constructor(ttl = Infinity) {
|
|
140
|
+
this.ttl = ttl;
|
|
141
|
+
this.store = new Map();
|
|
142
|
+
}
|
|
143
|
+
get(name) {
|
|
144
|
+
const entry = this.store.get(name);
|
|
145
|
+
if (entry == null) {
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
else if (entry.expires < Date.now()) {
|
|
149
|
+
this.delete(name);
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
return entry.session;
|
|
153
|
+
}
|
|
154
|
+
set(name, value) {
|
|
155
|
+
const now = Date.now();
|
|
156
|
+
this.store.set(name, { session: value, expires: now + this.ttl });
|
|
157
|
+
}
|
|
158
|
+
delete(name) {
|
|
159
|
+
this.store.delete(name);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
exports.MemorySessionStore = MemorySessionStore;
|
|
163
|
+
/** @deprecated session can use custom properties now. Directly use `'session' in ctx` instead */
|
|
164
|
+
function isSessionContext(ctx) {
|
|
165
|
+
return 'session' in ctx;
|
|
166
|
+
}
|
package/lib/telegraf.js
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
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.Telegraf = 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)('telegraf: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 Telegraf 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 Telegraf 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 `Telegraf` 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 Telegraf.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
|
+
var _a;
|
|
138
|
+
const protocol = opts.domain.startsWith('https://') || opts.domain.startsWith('http://');
|
|
139
|
+
if (protocol)
|
|
140
|
+
debug('Unexpected protocol in domain, telegraf will use https:', opts.domain);
|
|
141
|
+
const domain = protocol ? new url_1.URL(opts.domain).host : opts.domain;
|
|
142
|
+
const path = (_a = opts.path) !== null && _a !== void 0 ? _a : `/telegraf/${this.secretPathComponent()}`;
|
|
143
|
+
const url = `https://${domain}${path}`;
|
|
144
|
+
return { domain, path, url };
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Specify a url to receive incoming updates via webhook.
|
|
148
|
+
* Returns an Express-style middleware you can pass to app.use()
|
|
149
|
+
*/
|
|
150
|
+
async createWebhook(opts) {
|
|
151
|
+
const { domain, path, ...extra } = opts;
|
|
152
|
+
const domainOpts = this.getDomainOpts({ domain, path });
|
|
153
|
+
await this.telegram.setWebhook(domainOpts.url, extra);
|
|
154
|
+
debug(`Webhook set to ${domainOpts.url}`);
|
|
155
|
+
return this.webhookCallback(domainOpts.path, {
|
|
156
|
+
secretToken: extra.secret_token,
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
startPolling(allowedUpdates = []) {
|
|
160
|
+
this.polling = new polling_1.Polling(this.telegram, allowedUpdates);
|
|
161
|
+
return this.polling.loop(async (update) => {
|
|
162
|
+
await this.handleUpdate(update);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
startWebhook(path, tlsOptions, port, host, cb, secretToken) {
|
|
166
|
+
const webhookCb = this.webhookCallback(path, { secretToken });
|
|
167
|
+
const callback = typeof cb === 'function'
|
|
168
|
+
? (req, res) => webhookCb(req, res, () => cb(req, res))
|
|
169
|
+
: webhookCb;
|
|
170
|
+
this.webhookServer =
|
|
171
|
+
tlsOptions != null
|
|
172
|
+
? https.createServer(tlsOptions, callback)
|
|
173
|
+
: http.createServer(callback);
|
|
174
|
+
this.webhookServer.listen(port, host, () => {
|
|
175
|
+
debug('Webhook listening on port: %s', port);
|
|
176
|
+
});
|
|
177
|
+
return this;
|
|
178
|
+
}
|
|
179
|
+
secretPathComponent() {
|
|
180
|
+
return crypto
|
|
181
|
+
.createHash('sha3-256')
|
|
182
|
+
.update(this.token)
|
|
183
|
+
.update(process.version) // salt
|
|
184
|
+
.digest('hex');
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* @see https://github.com/telegraf/telegraf/discussions/1344#discussioncomment-335700
|
|
188
|
+
*/
|
|
189
|
+
async launch(config = {},
|
|
190
|
+
/** @experimental */
|
|
191
|
+
onLaunch) {
|
|
192
|
+
var _a, _b;
|
|
193
|
+
const [cfg, onMe] = typeof config === 'function' ? [{}, config] : [config, onLaunch];
|
|
194
|
+
const drop_pending_updates = cfg.dropPendingUpdates;
|
|
195
|
+
const allowed_updates = cfg.allowedUpdates;
|
|
196
|
+
const webhook = cfg.webhook;
|
|
197
|
+
debug('Connecting to Telegram');
|
|
198
|
+
(_a = this.botInfo) !== null && _a !== void 0 ? _a : (this.botInfo = await this.telegram.getMe());
|
|
199
|
+
onMe === null || onMe === void 0 ? void 0 : onMe();
|
|
200
|
+
debug(`Launching @${this.botInfo.username}`);
|
|
201
|
+
if (webhook === undefined) {
|
|
202
|
+
await this.telegram.deleteWebhook({ drop_pending_updates });
|
|
203
|
+
debug('Bot started with long polling');
|
|
204
|
+
await this.startPolling(allowed_updates);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const domainOpts = this.getDomainOpts({
|
|
208
|
+
domain: webhook.domain,
|
|
209
|
+
path: (_b = webhook.path) !== null && _b !== void 0 ? _b : webhook.hookPath,
|
|
210
|
+
});
|
|
211
|
+
const { tlsOptions, port, host, cb, secretToken } = webhook;
|
|
212
|
+
this.startWebhook(domainOpts.path, tlsOptions, port, host, cb, secretToken);
|
|
213
|
+
await this.telegram.setWebhook(domainOpts.url, {
|
|
214
|
+
drop_pending_updates: drop_pending_updates,
|
|
215
|
+
allowed_updates: allowed_updates,
|
|
216
|
+
ip_address: webhook.ipAddress,
|
|
217
|
+
max_connections: webhook.maxConnections,
|
|
218
|
+
secret_token: webhook.secretToken,
|
|
219
|
+
certificate: webhook.certificate,
|
|
220
|
+
});
|
|
221
|
+
debug(`Bot started with webhook @ ${domainOpts.url}`);
|
|
222
|
+
}
|
|
223
|
+
stop(reason = 'unspecified') {
|
|
224
|
+
var _a, _b;
|
|
225
|
+
debug('Stopping bot... Reason:', reason);
|
|
226
|
+
// https://github.com/telegraf/telegraf/pull/1224#issuecomment-742693770
|
|
227
|
+
if (this.polling === undefined && this.webhookServer === undefined) {
|
|
228
|
+
throw new Error('Bot is not running!');
|
|
229
|
+
}
|
|
230
|
+
(_a = this.webhookServer) === null || _a === void 0 ? void 0 : _a.close();
|
|
231
|
+
(_b = this.polling) === null || _b === void 0 ? void 0 : _b.stop();
|
|
232
|
+
}
|
|
233
|
+
async handleUpdate(update, webhookResponse) {
|
|
234
|
+
var _a, _b;
|
|
235
|
+
(_a = this.botInfo) !== null && _a !== void 0 ? _a : (this.botInfo = (debug('Update %d is waiting for `botInfo` to be initialized', update.update_id),
|
|
236
|
+
await ((_b = this.botInfoCall) !== null && _b !== void 0 ? _b : (this.botInfoCall = this.telegram.getMe()))));
|
|
237
|
+
debug('Processing update', update.update_id);
|
|
238
|
+
const tg = new telegram_1.default(this.token, this.telegram.options, webhookResponse);
|
|
239
|
+
const TelegrafContext = this.options.contextType;
|
|
240
|
+
const ctx = new TelegrafContext(update, tg, this.botInfo);
|
|
241
|
+
Object.assign(ctx, this.context);
|
|
242
|
+
try {
|
|
243
|
+
await (0, p_timeout_1.default)(Promise.resolve(this.middleware()(ctx, anoop)), this.options.handlerTimeout);
|
|
244
|
+
}
|
|
245
|
+
catch (err) {
|
|
246
|
+
return await this.handleError(err, ctx);
|
|
247
|
+
}
|
|
248
|
+
finally {
|
|
249
|
+
if ((webhookResponse === null || webhookResponse === void 0 ? void 0 : webhookResponse.writableEnded) === false) {
|
|
250
|
+
webhookResponse.end();
|
|
251
|
+
}
|
|
252
|
+
debug('Finished processing update', update.update_id);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
exports.Telegraf = Telegraf;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/** @format */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Markup = void 0;
|
|
5
|
+
var markup_1 = require("./markup");
|
|
6
|
+
Object.defineProperty(exports, "Markup", { enumerable: true, get: function () { return markup_1.Markup; } });
|