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();
自动动作发送
自动发送聊天动作在下列条件下开始:
- 从列表中请求方法 :
- 发送图片
- 发送奥迪奥
- 发送文档
- 发送视频
- 发送动画
- 发送声音
- 发送视频注释
- 发送Sticker
- 发送媒体组
- 请求有效载荷包含 输入文件
- 请求有效载荷包含聊天ID.
发送聊天动作在下列条件下之一停止:
- 更新处理完成 .
- 需要聊天动作的请求完成 .
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}!`);
}