@grammyjs/auto-chat-action

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

A plugin for automatic sending a chat action

原始使用文档
文档目录7 节
  1. Installation
  2. Node
  3. Deno
  4. Install plugin
  5. Usage
  6. Automatic Action Sending
  7. Manual Action Sending

Auto Chat Action plugin for grammY

This plugin provides a middleware to automatically send an appropriate chat action.
For example sends the "sending video" chat action when sendVideo is called.

Installation

Node

npm i @grammyjs/auto-chat-action

Deno

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

Install plugin

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

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

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

Usage

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();

Automatic Action Sending

Automatic sending of a chat action starts under the following conditions:

  1. Request method from the list:
    • sendPhoto
    • sendAudio
    • sendDocument
    • sendVideo
    • sendAnimation
    • sendVoice
    • sendVideoNote
    • sendSticker
    • sendMediaGroup
  2. Request payload contains InputFile
  3. Request payload contains a chat ID.

Sending of a chat action stops under one of the following conditions:

  1. Update processing is complete.
  2. Request which requires the chat action is complete.
  3. sendChatAction request caused an error.

Manual Action Sending

Sending chat action with context

// 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";

Sending other requests that require sending a chat action will interrupt the sending of the current chat action.

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

Sending chat action with middleware

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

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

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

Using with Conversations

You need to pass bot.api explicitly to use the plugin with conversations.

// 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 是什么?

A plugin for automatic sending a 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 和提交记录判断。