Skip to main content

Built and signed on GitHub Actions

Works with
This package works with Cloudflare Workers, Node.js, DenoIt is unknown whether this package works with Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score76%
Downloads1/wk
Published2 years ago (1.1.4)

Prompt plugin for GramIO.

@gramio/prompt

npm JSR JSR Score

A plugin for GramIO that provides Prompt and Wait methods

Read about Plugin options

Usage

import { Bot, format, bold } from "gramio";
import { prompt } from "@gramio/prompt";

const bot = new Bot(process.env.TOKEN as string)
    .extend(prompt())
    .command("start", async (context) => {
        const answer = await context.prompt(
            "message",
            format`What's your ${bold`name`}?`
        );

        return context.send(`✨ Your name is ${answer.text}`);
    })
    .onStart(console.log);

bot.start();

Prompt

Prompt with text + params

const answer = await context.prompt("What's your name?");
// or with SendMessageParams
const answer = await context.prompt("True or false?", {
    reply_markup: new Keyboard().text("true").row().text("false"),
});

answer is MessageContext or CallbackQueryContext

Prompt with text + params and the specified event

const answer = await context.prompt("message", "What's your name?");

const answer = await context.prompt("callback_query", "True or false?", {
    reply_markup: new InlineKeyboard()
        .text("true", "true")
        .row()
        .text("false", "false"),
});

answer is CallbackQueryContext

Validation

You can define a handler in params to validate the user's answer. If handler returns false, the message will be repeated.

const answer = await context.prompt(
    "message",
    "Enter a string that contains russian letter",
    {
        validate: (context) => /[а-яА-Я]/.test(context.text),
        //... and some SendMessageParams
    }
);

Transform

const name = await context.prompt(
    "message",
    format`What's your ${bold`name`}?`,
    {
        transform: (context) => context.text || context.caption || "",
    }
);

name is string

Wait

Wait for the next event from the user

const answer = await context.wait();

answer is MessageContext or CallbackQueryContext

Wait for the next event from the user ignoring events not listed

const answer = await context.wait("message");

answer is CallbackQueryContext

Wait for the next event from the user ignoring non validated answers

You can define a handler in params to validate the user's answer. If handler return false, the message will be ignored

const answer = await context.wait((context) => /[а-яА-Я]/.test(context.text));
// or combine with event
const answer = await context.wait("message", (context) =>
    /[а-яА-Я]/.test(context.text)
);

Wait for the next event from the user ignoring non validated answers with transformer

You can define a handler in params to transform the user's answer.

const answer = await context.wait((context) => /[а-яА-Я]/.test(context.text));
// or combine with event
const answer = await context.wait("message", {
    validate: (context) => /[а-яА-Я]/.test(context.text),
    transform: (context) => c.text || "",
});

answer is string

Built and signed on
GitHub Actions

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.