teledzik 1.0.3 → 1.0.5
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 +75 -5
- package/lib/button.js +48 -5
- package/lib/context.js +34 -9
- package/lib/index.js +6 -1
- package/lib/telegram.js +90 -11
- package/package.json +2 -2
- package/src/button.ts +89 -6
- package/src/context.ts +60 -20
- package/src/core/types/rich-message.ts +15 -0
- package/src/index.ts +1 -0
- package/src/telegram.ts +117 -13
- package/typings/button.d.ts +51 -2
- package/typings/context.d.ts +4 -2
- package/typings/core/types/rich-message.d.ts +20 -0
- package/typings/index.d.ts +1 -0
- package/typings/telegram.d.ts +4 -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/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; } });
|