grammY 命令
这个插件为您的 grammY bot 提供了定义和管理命令的方便方式. 它简化了设置有范围及本地化的指令的过程.
安装
npm i @grammyjs/commands
使用量
请检查一下 正式文件。 。 。 .
此插件的主要功能是定义您的命令, 将其本地化, 并为每个命令提供处理器 范围,像这样:
import { Bot } from "grammy";
import { CommandGroup } from "@grammyjs/commands";
const bot = new Bot("<telegram token>");
const myCommands = new CommandGroup();
myCommands.command("start", "Initializes bot configuration")
.localize("pt", "start", "Inicializa as configurações do bot")
.addToScope(
{ type: "all_private_chats" },
(ctx) => ctx.reply(`Hello, ${ctx.chat.first_name}!`),
)
.addToScope(
{ type: "all_group_chats" },
(ctx) => ctx.reply(`Hello, members of ${ctx.chat.title}!`),
);
// Calls `setMyCommands`
await myCommands.setCommands(bot);
// Registers the command handlers
bot.use(myCommands);
bot.start();
你打电话来很重要 bot.use 用你的例子 Commands 课。 否则,命令处理者
您的机器人不会响应这些命令.
上下文快捷键
此插件为当前聊天设置命令提供了快捷方式 。 要使用它,需要安装 命令风味和插件本身,例如:
import { Bot, Context } from "grammy";
import { CommandGroup, commands, CommandsFlavor } from "@grammyjs/commands";
type BotContext = CommandsFlavor;
const bot = new Bot<BotContext>("<telegram_token>");
bot.use(commands());
bot.on("message", async (ctx) => {
const cmds = new CommandGroup();
cmds.command("start", "Initializes bot configuration")
.localize("pt", "start", "Inicializa as configurações do bot");
await ctx.setMyCommands(cmds);
return ctx.reply("Commands set!");
});
bot.start();