grammY 对话
GrammY对话插件让您轻松地创建出强大的对话界面.
这是插件的第二版 . 第二版是从零开始完全重写. 如果您仍在使用1.x版本, 请检查 迁移指南。 。 。 . (旧文件不再更新,但仍可以找到) 这里. ) (中文(简体) )
你应该看看 正式文件 的插件,但这里是一个快速启动,您可以站起来运行.
快速启动
运行 npm i @grammyjs/conversations 并粘贴以下代码:
import { Bot, type Context } from "grammy";
import {
type Conversation,
type ConversationFlavor,
conversations,
createConversation,
} from "@grammyjs/conversations";
type MyContext = ConversationFlavor<Context>;
type MyConversationContext = Context;
type MyConversation = Conversation<MyContext, MyConversationContext>;
const bot = new Bot<MyContext>("");
/** Defines the conversation */
async function greeting(
conversation: MyConversation,
ctx: MyConversationContext,
) {
await ctx.reply("Hi there! What is your name?");
const { message } = await conversation.wait();
await ctx.reply(`Welcome to the chat, ${message.text}!`);
}
bot.use(conversations());
bot.use(createConversation(greeting));
bot.command("enter", async (ctx) => {
await ctx.reply("Entering conversation!");
// enter the function "greeting" you declared
await ctx.conversation.enter("greeting");
});
bot.command("start", (ctx) => ctx.reply("Hi! Send /enter"));
bot.use((ctx) => ctx.reply("What a nice update."));
bot.start();
尼夫特!