@grammyjs/auto-chat-action

中文文档
0.1.1发布于 2023/03/27MITTypeScriptESM + CJS零依赖近 30 天下载 5542

自动发送聊天动作的插件

中文使用文档
文档目录7 节
  1. 安装
  2. 节点
  3. 德诺
  4. 安装插件
  5. 使用量
  6. 自动动作发送
  7. 手动动作发送

GrammY 自动聊天动作插件

此插件提供了一个自动发送合适的中间软件 聊天动作。 。 。 .
例如,当 sendVideo 名为.

安装

节点

npm i @grammyjs/auto-chat-action

德诺

import { autoChatAction } from "https://deno.land/x/grammy_auto_chat_action/mod.ts";

安装插件

import {
  autoChatAction,
  AutoChatActionFlavor,
} from "@grammyjs/auto-chat-action";

// Extend the context
const bot = new Bot<Context & AutoChatActionFlavor>("");

// Install the plugin
bot.use(autoChatAction());

使用量

import { Bot, Context } from "grammy";
import {
  autoChatAction,
  AutoChatActionFlavor,
} from "@grammyjs/auto-chat-action";

type MyContext = Context & AutoChatActionFlavor;

// Create a bot.
const bot = new Bot<MyContext>("token");

// Install the plugin
bot.use(autoChatAction());

bot.command("start", (ctx) => {
  // Starts sending "typing" chat action in a loop
  ctx.chatAction = "typing";

  // Some long-running operations...

  return ctx.reply("42!");
});

bot.command("photo", (ctx) => {
  // Sending the "upload_photo" chat action until the media is uploaded
  return ctx.replyWithPhoto(
    new InputFile("/tmp/picture.jpg"),
  );
});

bot.start();

自动动作发送

自动发送聊天动作在下列条件下开始:

  1. 从列表中请求方法 :
    • 发送图片
    • 发送奥迪奥
    • 发送文档
    • 发送视频
    • 发送动画
    • 发送声音
    • 发送视频注释
    • 发送Sticker
    • 发送媒体组
  2. 请求有效载荷包含 输入文件
  3. 请求有效载荷包含聊天ID.

发送聊天动作在下列条件下之一停止:

  1. 更新处理完成 .
  2. 需要聊天动作的请求完成 .
  3. sendChatAction 请求导致错误.

手动动作发送

以上下文发送聊天动作

// Set the action to be sent until the update is processed
ctx.chatAction = "typing";

// To stop sending, simply set the chat action to null
ctx.chatAction = null;

// You can change the chat action during update processing
ctx.chatAction = "choose_sticker";

发送需要发送聊天动作的其他请求将中断 发送当前聊天动作 .

ctx.chatAction = "typing";

await ctx.reply("Hi!");

// Sends "upload_photo" while file is uploading
await ctx.replyWithPhoto(
  new InputFile("/tmp/kitten.png"),
);

// Now there is no sending chat action
// ctx.chatAction is null

用中间软件发送聊天动作

import { chatAction } from "@grammyjs/auto-chat-action";

bot.command("start", chatAction("typing"), (ctx) => {
  // Some long-running operations...

  return ctx.reply("42!");
});

使用方式 对话

你得通过 bot.api 明确使用带有对话的插件.

// Pass API instance to the plugin
bot.use(autoChatAction(bot.api));

async function greeting(conversation: Conversation<Context>, ctx: Context) {
  await ctx.reply("Hi there! What is your name?");

  const { message } = await conversation.wait();
  ctx.chatAction = "typing";
  await conversation.sleep(1000);
  await ctx.reply(`Welcome to the chat, ${message.text}!`);
}

版本与发布记录

@grammyjs/auto-chat-action 共发布 3 个版本,首次发布于 2023/02/08;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.1latest2023/03/273 年前
0.1.02023/03/273 年前
0.0.12023/02/083 年前

关于 @grammyjs/auto-chat-action 的常见问题

@grammyjs/auto-chat-action 是什么?

自动发送聊天动作的插件

如何安装 @grammyjs/auto-chat-action?

在项目目录运行 npm install @grammyjs/auto-chat-action,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:grammy(^1.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@grammyjs/auto-chat-action 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@grammyjs/auto-chat-action 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@grammyjs/auto-chat-action 可以商用吗?

@grammyjs/auto-chat-action 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@grammyjs/auto-chat-action 的下载量有多少?

根据 npm 官方下载统计,@grammyjs/auto-chat-action 在 2026/10/17 – 2026/11/15 这 30 天内被下载 5,542 次,日均约 185 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@grammyjs/auto-chat-action 还在维护吗?

最新版本 0.1.1 发布于 2023/03/27(3 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/02/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。