@yaebal/prompt 0.0.1
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 +21 -0
- package/README.md +13 -0
- package/lib/index.d.ts +22 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +36 -0
- package/lib/index.js.map +1 -0
- package/lib/index.test.d.ts +2 -0
- package/lib/index.test.d.ts.map +1 -0
- package/lib/index.test.js +72 -0
- package/lib/index.test.js.map +1 -0
- package/package.json +47 -0
- package/src/index.test.ts +103 -0
- package/src/index.ts +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 neverlane
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @yaebal/prompt
|
|
2
|
+
|
|
3
|
+
handles the message that answers a prompt. may call `ctx.prompt` again to chain.
|
|
4
|
+
|
|
5
|
+
## install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @yaebal/prompt
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
part of [**yaebal**](https://github.com/neverlane/yaebal) — a type-safe, runtime-agnostic Telegram Bot API framework. MIT.
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Context, FormatResult, Message, Plugin } from "@yaebal/core";
|
|
2
|
+
/** handles the message that answers a prompt. may call `ctx.prompt` again to chain. */
|
|
3
|
+
export type PromptHandler = (ctx: Context) => unknown | Promise<unknown>;
|
|
4
|
+
export interface PromptControl {
|
|
5
|
+
/** send `question`, then run `handler` on the next message in this chat. */
|
|
6
|
+
prompt(question: string | FormatResult, handler: PromptHandler, extra?: Record<string, unknown>): Promise<Message>;
|
|
7
|
+
}
|
|
8
|
+
export interface PromptOptions {
|
|
9
|
+
/** prompt key for an update. defaults to per-chat (`ctx.chat.id`). */
|
|
10
|
+
getKey?: (ctx: Context) => string | undefined;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* prompt plugin: `ctx.prompt(question, handler)` sends a question and runs the
|
|
14
|
+
* handler on the next message — no suspended promise, so it is safe under the
|
|
15
|
+
* sequential update loop. pending handlers are in-memory (lost on restart).
|
|
16
|
+
*
|
|
17
|
+
* the answering message is consumed, so a user typing `/menu` instead of an
|
|
18
|
+
* answer has it eaten by the pending handler — check for an escape in the handler
|
|
19
|
+
* if that matters.
|
|
20
|
+
*/
|
|
21
|
+
export declare function prompt(options?: PromptOptions): Plugin<Context, PromptControl>;
|
|
22
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAE3E,uFAAuF;AACvF,MAAM,MAAM,aAAa,GAAG,CAAC,GAAG,EAAE,OAAO,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAEzE,MAAM,WAAW,aAAa;IAC7B,4EAA4E;IAC5E,MAAM,CACL,QAAQ,EAAE,MAAM,GAAG,YAAY,EAC/B,OAAO,EAAE,aAAa,EACtB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,OAAO,CAAC,OAAO,CAAC,CAAC;CACpB;AAED,MAAM,WAAW,aAAa;IAC7B,sEAAsE;IACtE,MAAM,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,MAAM,GAAG,SAAS,CAAC;CAC9C;AAED;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,OAAO,GAAE,aAAkB,GAAG,MAAM,CAAC,OAAO,EAAE,aAAa,CAAC,CAmClF"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* prompt plugin: `ctx.prompt(question, handler)` sends a question and runs the
|
|
3
|
+
* handler on the next message — no suspended promise, so it is safe under the
|
|
4
|
+
* sequential update loop. pending handlers are in-memory (lost on restart).
|
|
5
|
+
*
|
|
6
|
+
* the answering message is consumed, so a user typing `/menu` instead of an
|
|
7
|
+
* answer has it eaten by the pending handler — check for an escape in the handler
|
|
8
|
+
* if that matters.
|
|
9
|
+
*/
|
|
10
|
+
export function prompt(options = {}) {
|
|
11
|
+
const pending = new Map();
|
|
12
|
+
const getKey = options.getKey ?? ((ctx) => ctx.chat?.id?.toString());
|
|
13
|
+
const plugin = (composer) => composer
|
|
14
|
+
.derive((ctx) => ({
|
|
15
|
+
prompt: (question, handler, extra = {}) => {
|
|
16
|
+
const key = getKey(ctx);
|
|
17
|
+
if (key !== undefined)
|
|
18
|
+
pending.set(key, handler);
|
|
19
|
+
return ctx.send(question, extra);
|
|
20
|
+
},
|
|
21
|
+
}))
|
|
22
|
+
.use(async (ctx, next) => {
|
|
23
|
+
const key = getKey(ctx);
|
|
24
|
+
if (key !== undefined && ctx.message) {
|
|
25
|
+
const handler = pending.get(key);
|
|
26
|
+
if (handler) {
|
|
27
|
+
pending.delete(key);
|
|
28
|
+
await handler(ctx);
|
|
29
|
+
return; // consumed
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
await next();
|
|
33
|
+
});
|
|
34
|
+
return plugin;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAmBA;;;;;;;;GAQG;AACH,MAAM,UAAU,MAAM,CAAC,UAAyB,EAAE;IACjD,MAAM,OAAO,GAAG,IAAI,GAAG,EAAyB,CAAC;IACjD,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,CAAC,CAAC,GAAY,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,EAAE,QAAQ,EAAE,CAAC,CAAC;IAE9E,MAAM,MAAM,GAAmC,CAAC,QAAQ,EAAE,EAAE,CAC3D,QAAQ;SACN,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;QACjB,MAAM,EAAE,CACP,QAA+B,EAC/B,OAAsB,EACtB,QAAiC,EAAE,EAClC,EAAE;YACH,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;YACxB,IAAI,GAAG,KAAK,SAAS;gBAAE,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;YAEjD,OAAO,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;QAClC,CAAC;KACD,CAAC,CAAC;SACF,GAAG,CAAC,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QACxB,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;QAExB,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,CAAC,OAAO,EAAE,CAAC;YACtC,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YAEjC,IAAI,OAAO,EAAE,CAAC;gBACb,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;gBACpB,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC;gBAEnB,OAAO,CAAC,WAAW;YACpB,CAAC;QACF,CAAC;QACD,MAAM,IAAI,EAAE,CAAC;IACd,CAAC,CAAC,CAAC;IAEL,OAAO,MAAM,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { Composer, Context } from "@yaebal/core";
|
|
4
|
+
import { prompt } from "./index.js";
|
|
5
|
+
const noop = async () => { };
|
|
6
|
+
const entry = (c) => c.toMiddleware();
|
|
7
|
+
function fakeApi() {
|
|
8
|
+
const sent = [];
|
|
9
|
+
const api = {
|
|
10
|
+
sendMessage(p) {
|
|
11
|
+
sent.push(String(p.text));
|
|
12
|
+
return Promise.resolve({ message_id: 1 });
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
return { api, sent };
|
|
16
|
+
}
|
|
17
|
+
const msgCtx = (api, text, chatId) => new Context({
|
|
18
|
+
api,
|
|
19
|
+
update: {
|
|
20
|
+
update_id: 1,
|
|
21
|
+
message: {
|
|
22
|
+
message_id: 1,
|
|
23
|
+
date: 0,
|
|
24
|
+
chat: { id: chatId, type: "private" },
|
|
25
|
+
from: { id: chatId, is_bot: false, first_name: "u" },
|
|
26
|
+
text,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
updateType: "message",
|
|
30
|
+
});
|
|
31
|
+
test("prompt runs its handler on the next message and consumes it", async () => {
|
|
32
|
+
const { api } = fakeApi();
|
|
33
|
+
let answer = "";
|
|
34
|
+
let fellThrough = 0;
|
|
35
|
+
const mw = entry(new Composer()
|
|
36
|
+
.install(prompt())
|
|
37
|
+
.command("ask", (ctx) => ctx.prompt("name?", (c) => {
|
|
38
|
+
answer = c.text ?? "";
|
|
39
|
+
}))
|
|
40
|
+
.on("message:text", () => {
|
|
41
|
+
fellThrough++;
|
|
42
|
+
}));
|
|
43
|
+
await mw(msgCtx(api, "/ask", 1), noop); // asks
|
|
44
|
+
await mw(msgCtx(api, "Bob", 1), noop); // answers
|
|
45
|
+
assert.equal(answer, "Bob");
|
|
46
|
+
assert.equal(fellThrough, 0); // the answer was consumed
|
|
47
|
+
});
|
|
48
|
+
test("prompts chain", async () => {
|
|
49
|
+
const { api, sent } = fakeApi();
|
|
50
|
+
const got = [];
|
|
51
|
+
const mw = entry(new Composer().install(prompt()).command("ask", (ctx) => ctx.prompt("first?", (c1) => {
|
|
52
|
+
got.push(c1.text ?? "");
|
|
53
|
+
return c1.prompt("second?", (c2) => {
|
|
54
|
+
got.push(c2.text ?? "");
|
|
55
|
+
});
|
|
56
|
+
})));
|
|
57
|
+
await mw(msgCtx(api, "/ask", 1), noop);
|
|
58
|
+
await mw(msgCtx(api, "a", 1), noop);
|
|
59
|
+
await mw(msgCtx(api, "b", 1), noop);
|
|
60
|
+
assert.deepEqual(got, ["a", "b"]);
|
|
61
|
+
assert.deepEqual(sent, ["first?", "second?"]);
|
|
62
|
+
});
|
|
63
|
+
test("an unrelated message with no pending prompt falls through", async () => {
|
|
64
|
+
const { api } = fakeApi();
|
|
65
|
+
let seen = "";
|
|
66
|
+
const mw = entry(new Composer().install(prompt()).on("message:text", (ctx) => {
|
|
67
|
+
seen = ctx.text;
|
|
68
|
+
}));
|
|
69
|
+
await mw(msgCtx(api, "hello", 2), noop);
|
|
70
|
+
assert.equal(seen, "hello");
|
|
71
|
+
});
|
|
72
|
+
//# sourceMappingURL=index.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAmB,MAAM,cAAc,CAAC;AAClE,OAAO,EAAsB,MAAM,EAAE,MAAM,YAAY,CAAC;AAExD,MAAM,IAAI,GAAG,KAAK,IAAI,EAAE,GAAE,CAAC,CAAC;AAC5B,MAAM,KAAK,GAAG,CAAoB,CAAc,EAAE,EAAE,CACnD,CAAC,CAAC,YAAY,EAAoC,CAAC;AAEpD,SAAS,OAAO;IACf,MAAM,IAAI,GAAa,EAAE,CAAC;IAC1B,MAAM,GAAG,GAAG;QACX,WAAW,CAAC,CAA0B;YACrC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;YAC1B,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC,CAAC;QAC3C,CAAC;KACQ,CAAC;IAEX,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC;AACtB,CAAC;AAED,MAAM,MAAM,GAAG,CAAC,GAAU,EAAE,IAAY,EAAE,MAAc,EAAE,EAAE,CAC3D,IAAI,OAAO,CAAC;IACX,GAAG;IACH,MAAM,EAAE;QACP,SAAS,EAAE,CAAC;QACZ,OAAO,EAAE;YACR,UAAU,EAAE,CAAC;YACb,IAAI,EAAE,CAAC;YACP,IAAI,EAAE,EAAE,EAAE,EAAE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AAC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,CAAC,cAAc,EAAE,CAAC,GAAG,EAAE,EAAE;QACpE,IAAI,GAAG,GAAG,CAAC,IAAI,CAAC;IACjB,CAAC,CAAC,CACF,CAAC;IACF,MAAM,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;IAExC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AAC7B,CAAC,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yaebal/prompt",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "yaebal prompt plugin — ask a question and handle the next message.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./lib/index.js",
|
|
7
|
+
"types": "./lib/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/index.d.ts",
|
|
11
|
+
"import": "./lib/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"lib",
|
|
16
|
+
"src"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@yaebal/core": "0.0.1"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/node": "latest"
|
|
23
|
+
},
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=20"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"telegram",
|
|
29
|
+
"telegram-bot",
|
|
30
|
+
"yaebal",
|
|
31
|
+
"prompt"
|
|
32
|
+
],
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "https://github.com/neverlane/yaebal",
|
|
37
|
+
"directory": "packages/prompt"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsc -p tsconfig.json",
|
|
44
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
45
|
+
"test": "node --test lib"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { Composer, Context, type Middleware } from "@yaebal/core";
|
|
4
|
+
import { type PromptControl, prompt } from "./index.js";
|
|
5
|
+
|
|
6
|
+
const noop = async () => {};
|
|
7
|
+
const entry = <C extends Context>(c: Composer<C>) =>
|
|
8
|
+
c.toMiddleware() as unknown as Middleware<Context>;
|
|
9
|
+
|
|
10
|
+
function fakeApi() {
|
|
11
|
+
const sent: string[] = [];
|
|
12
|
+
const api = {
|
|
13
|
+
sendMessage(p: Record<string, unknown>) {
|
|
14
|
+
sent.push(String(p.text));
|
|
15
|
+
return Promise.resolve({ message_id: 1 });
|
|
16
|
+
},
|
|
17
|
+
} as never;
|
|
18
|
+
|
|
19
|
+
return { api, sent };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const msgCtx = (api: never, text: string, chatId: number) =>
|
|
23
|
+
new Context({
|
|
24
|
+
api,
|
|
25
|
+
update: {
|
|
26
|
+
update_id: 1,
|
|
27
|
+
message: {
|
|
28
|
+
message_id: 1,
|
|
29
|
+
date: 0,
|
|
30
|
+
chat: { id: chatId, type: "private" },
|
|
31
|
+
from: { id: chatId, is_bot: false, first_name: "u" },
|
|
32
|
+
text,
|
|
33
|
+
},
|
|
34
|
+
} as never,
|
|
35
|
+
updateType: "message",
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
type Ctx = Context & PromptControl;
|
|
39
|
+
|
|
40
|
+
test("prompt runs its handler on the next message and consumes it", async () => {
|
|
41
|
+
const { api } = fakeApi();
|
|
42
|
+
|
|
43
|
+
let answer = "";
|
|
44
|
+
let fellThrough = 0;
|
|
45
|
+
|
|
46
|
+
const mw = entry(
|
|
47
|
+
new Composer<Context>()
|
|
48
|
+
.install(prompt())
|
|
49
|
+
.command("ask", (ctx) =>
|
|
50
|
+
(ctx as Ctx).prompt("name?", (c) => {
|
|
51
|
+
answer = c.text ?? "";
|
|
52
|
+
}),
|
|
53
|
+
)
|
|
54
|
+
.on("message:text", () => {
|
|
55
|
+
fellThrough++;
|
|
56
|
+
}),
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
await mw(msgCtx(api, "/ask", 1), noop); // asks
|
|
60
|
+
await mw(msgCtx(api, "Bob", 1), noop); // answers
|
|
61
|
+
|
|
62
|
+
assert.equal(answer, "Bob");
|
|
63
|
+
assert.equal(fellThrough, 0); // the answer was consumed
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("prompts chain", async () => {
|
|
67
|
+
const { api, sent } = fakeApi();
|
|
68
|
+
|
|
69
|
+
const got: string[] = [];
|
|
70
|
+
const mw = entry(
|
|
71
|
+
new Composer<Context>().install(prompt()).command("ask", (ctx) =>
|
|
72
|
+
(ctx as Ctx).prompt("first?", (c1) => {
|
|
73
|
+
got.push(c1.text ?? "");
|
|
74
|
+
|
|
75
|
+
return (c1 as Ctx).prompt("second?", (c2) => {
|
|
76
|
+
got.push(c2.text ?? "");
|
|
77
|
+
});
|
|
78
|
+
}),
|
|
79
|
+
),
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
await mw(msgCtx(api, "/ask", 1), noop);
|
|
83
|
+
await mw(msgCtx(api, "a", 1), noop);
|
|
84
|
+
await mw(msgCtx(api, "b", 1), noop);
|
|
85
|
+
|
|
86
|
+
assert.deepEqual(got, ["a", "b"]);
|
|
87
|
+
assert.deepEqual(sent, ["first?", "second?"]);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("an unrelated message with no pending prompt falls through", async () => {
|
|
91
|
+
const { api } = fakeApi();
|
|
92
|
+
|
|
93
|
+
let seen = "";
|
|
94
|
+
|
|
95
|
+
const mw = entry(
|
|
96
|
+
new Composer<Context>().install(prompt()).on("message:text", (ctx) => {
|
|
97
|
+
seen = ctx.text;
|
|
98
|
+
}),
|
|
99
|
+
);
|
|
100
|
+
await mw(msgCtx(api, "hello", 2), noop);
|
|
101
|
+
|
|
102
|
+
assert.equal(seen, "hello");
|
|
103
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { Context, FormatResult, Message, Plugin } from "@yaebal/core";
|
|
2
|
+
|
|
3
|
+
/** handles the message that answers a prompt. may call `ctx.prompt` again to chain. */
|
|
4
|
+
export type PromptHandler = (ctx: Context) => unknown | Promise<unknown>;
|
|
5
|
+
|
|
6
|
+
export interface PromptControl {
|
|
7
|
+
/** send `question`, then run `handler` on the next message in this chat. */
|
|
8
|
+
prompt(
|
|
9
|
+
question: string | FormatResult,
|
|
10
|
+
handler: PromptHandler,
|
|
11
|
+
extra?: Record<string, unknown>,
|
|
12
|
+
): Promise<Message>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface PromptOptions {
|
|
16
|
+
/** prompt key for an update. defaults to per-chat (`ctx.chat.id`). */
|
|
17
|
+
getKey?: (ctx: Context) => string | undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* prompt plugin: `ctx.prompt(question, handler)` sends a question and runs the
|
|
22
|
+
* handler on the next message — no suspended promise, so it is safe under the
|
|
23
|
+
* sequential update loop. pending handlers are in-memory (lost on restart).
|
|
24
|
+
*
|
|
25
|
+
* the answering message is consumed, so a user typing `/menu` instead of an
|
|
26
|
+
* answer has it eaten by the pending handler — check for an escape in the handler
|
|
27
|
+
* if that matters.
|
|
28
|
+
*/
|
|
29
|
+
export function prompt(options: PromptOptions = {}): Plugin<Context, PromptControl> {
|
|
30
|
+
const pending = new Map<string, PromptHandler>();
|
|
31
|
+
const getKey = options.getKey ?? ((ctx: Context) => ctx.chat?.id?.toString());
|
|
32
|
+
|
|
33
|
+
const plugin: Plugin<Context, PromptControl> = (composer) =>
|
|
34
|
+
composer
|
|
35
|
+
.derive((ctx) => ({
|
|
36
|
+
prompt: (
|
|
37
|
+
question: string | FormatResult,
|
|
38
|
+
handler: PromptHandler,
|
|
39
|
+
extra: Record<string, unknown> = {},
|
|
40
|
+
) => {
|
|
41
|
+
const key = getKey(ctx);
|
|
42
|
+
if (key !== undefined) pending.set(key, handler);
|
|
43
|
+
|
|
44
|
+
return ctx.send(question, extra);
|
|
45
|
+
},
|
|
46
|
+
}))
|
|
47
|
+
.use(async (ctx, next) => {
|
|
48
|
+
const key = getKey(ctx);
|
|
49
|
+
|
|
50
|
+
if (key !== undefined && ctx.message) {
|
|
51
|
+
const handler = pending.get(key);
|
|
52
|
+
|
|
53
|
+
if (handler) {
|
|
54
|
+
pending.delete(key);
|
|
55
|
+
await handler(ctx);
|
|
56
|
+
|
|
57
|
+
return; // consumed
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
await next();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
return plugin;
|
|
64
|
+
}
|