流式消息草稿(stream)
这个插件可以让你将长文本消息以流式方式发送到 Telegram。 任何能够输出字符串片段的 迭代器 都可以直接流式发送到任意私聊中。
例如,你可以在 LLM 生成回复时让输出能够 逐步显示。
快速开始
这个插件会在 上下文对象 上安装这三个方法:
ctx: 流式传输普通消息.reply With Stream ctx: 流式传输 Markdown(推荐).reply With Markdown Stream ctx: 流式传输 HTML.reply With Html Stream
流式传输普通消息(第一个选项)用于发送常规文本消息。 其他两种方法使用 Telegram 的 富媒体消息 (rich message),在大多数情况下我们推荐这种方法。
流式发送消息会非常频繁地发起大量 API 调用。 强烈建议将 auto
-retry 插件 与 stream 插件搭配使用。
ts
import { Bot, type Context } from "grammy";
import { autoRetry } from "@grammyjs/auto-retry";
import { stream, type StreamFlavor } from "@grammyjs/stream";
type MyContext = StreamFlavor<Context>;
const bot = new Bot<MyContext>("");
bot.api.config.use(autoRetry()); // 强烈推荐!
bot.use(stream());
async function* slowText() {
// 模拟缓慢生成文本
yield "这是一";
await new Promise((r) => setTimeout(r, 2000));
yield "些生成很";
await new Promise((r) => setTimeout(r, 2000));
yield "慢的文本";
}
// Telegram 仅支持在私聊中流式发送。
bot.chatType("private")
.command("stream", async (ctx) => {
// 流式发送消息!
await ctx.replyWithStream(slowText());
});
bot.start();1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
js
const { Bot } = require("grammy");
const { autoRetry } = require("@grammyjs/auto-retry");
const { stream } = require("@grammyjs/stream");
const bot = new Bot("");
bot.api.config.use(autoRetry()); // 强烈推荐!
bot.use(stream());
async function* slowText() {
// 模拟缓慢生成文本
yield "这是一";
await new Promise((r) => setTimeout(r, 2000));
yield "些生成很";
await new Promise((r) => setTimeout(r, 2000));
yield "慢的文本";
}
// Telegram 仅支持在私聊中流式发送。
bot.chatType("private")
.command("stream", async (ctx) => {
// 流式发送消息!
await ctx.replyWithStream(slowText());
});
bot.start();1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
ts
import { Bot, type Context } from "https://deno.land/x/grammy@v1.44.0/mod.ts";
import { autoRetry } from "https://deno.land/x/grammy_auto_retry@v2.0.2/mod.ts";
import {
stream,
type StreamFlavor,
} from "https://deno.land/x/grammy_stream@v1.1.0/mod.ts";
type MyContext = StreamFlavor<Context>;
const bot = new Bot<MyContext>("");
bot.api.config.use(autoRetry()); // 强烈推荐!
bot.use(stream());
async function* slowText() {
// 模拟缓慢生成文本
yield "这是一";
await new Promise((r) => setTimeout(r, 2000));
yield "些生成很";
await new Promise((r) => setTimeout(r, 2000));
yield "慢的文本";
}
// Telegram 仅支持在私聊中流式发送。
bot.chatType("private")
.command("stream", async (ctx) => {
// 流式发送消息!
await ctx.replyWithStream(slowText());
});
bot.start();1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
就这么简单!
LLM 集成
大多数 LLM 集成都支持在生成输出时进行流式传输。 你可以使用这个插件,让 LLM 的输出在任意私聊中逐步显示出来。
例如,如果你使用 AI SDK,你的代码会像这样:
ts
import { streamText } from "ai";
import { google } from "@ai-sdk/google";
bot.chatType("private")
.command("credits", async (ctx) => {
// 向 LLM 发送提示词:
const { textStream } = streamText({
model: google("gemini-2.5-flash"),
prompt: "GrammY 机器人有多酷?",
});
// 使用 grammY 自动流式发送回复:
await ctx.replyWithMarkdownStream(textStream);
});1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
ts
import { streamText } from "npm:ai";
import { google } from "npm:@ai-sdk/google";
bot.chatType("private")
.command("credits", async (ctx) => {
// 向 LLM 发送提示词:
const { textStream } = streamText({
model: google("gemini-2.5-flash"),
prompt: "GrammY 机器人有多酷?",
});
// 使用 grammY 自动流式发送回复:
await ctx.replyWithMarkdownStream(textStream);
});1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
记得把 gemini 替换为当前最新最热的模型。