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";
const bot = new Bot<Context & AutoChatActionFlavor>("");
bot.use(autoChatAction());
Usage
import { Bot, Context } from "grammy";
import {
autoChatAction,
AutoChatActionFlavor,
} from "@grammyjs/auto-chat-action";
type MyContext = Context & AutoChatActionFlavor;
const bot = new Bot<MyContext>("token");
bot.use(autoChatAction());
bot.command("start", (ctx) => {
ctx.chatAction = "typing";
return ctx.reply("42!");
});
bot.command("photo", (ctx) => {
return ctx.replyWithPhoto(
new InputFile("/tmp/picture.jpg"),
);
});
bot.start();
Automatic Action Sending
Automatic sending of a chat action starts under the following conditions:
- Request method from the list:
- sendPhoto
- sendAudio
- sendDocument
- sendVideo
- sendAnimation
- sendVoice
- sendVideoNote
- sendSticker
- sendMediaGroup
- Request payload contains InputFile
- Request payload contains a chat ID.
Sending of a chat action stops under one of the following conditions:
- Update processing is complete.
- Request which requires the chat action is complete.
sendChatAction
request caused an error.
Manual Action Sending
Sending chat action with context
ctx.chatAction = "typing";
ctx.chatAction = null;
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!");
await ctx.replyWithPhoto(
new InputFile("/tmp/kitten.png"),
);
Sending chat action with middleware
import { chatAction } from "@grammyjs/auto-chat-action";
bot.command("start", chatAction("typing"), (ctx) => {
return ctx.reply("42!");
});
You need to pass bot.api
explicitly to use the plugin with conversations.
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}!`);
}