grammY 流线
向Telegram流出长长的短信 . 在发送消息前, 将 LLM 输出作为动画信件草稿显示 .
快速启动
给您一个快速的启动,但请检查一下 网站的真实文档。 。 。 .
运行 npm i grammy @grammyjs/stream @grammyjs/auto-retry 并粘贴以下代码:
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() {
// emulate slow text generation
yield "This i";
await new Promise((r) => setTimeout(r, 1000));
yield "s some sl";
await new Promise((r) => setTimeout(r, 1000));
yield "owly gene";
await new Promise((r) => setTimeout(r, 1000));
yield "rated text";
}
bot.command("stream", async (ctx) => {
await ctx.replyWithStream(slowText());
});
bot.command("start", (ctx) => ctx.reply("Hi! Send /stream"));
bot.use((ctx) => ctx.reply("What a nice update."));
bot.start();
例如,如果使用 AI SDK 软件你的人工智能设置可能是这样的:
import { streamText } from "ai";
import { google } from "@ai-sdk/google";
bot.command("credits", async (ctx) => {
// Send prompt to LLM:
const { textStream } = streamText({
model: google("gemini-2.5-flash"),
prompt: "How cool are grammY bots?",
});
// Automatically stream response with grammY:
await ctx.replyWithMarkdownStream(textStream);
});
考虑纳入关于Telegram的信息 丰富的消息格式化选项 如果您的 LLM 需要一些指导, 请在您的提示中进行 .