@grammyjs/stateless-question

中文文档
4.0.0发布于 2024/05/24MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 1.3万

Create stateless questions to Telegram users working in privacy mode

原始使用文档
文档目录7 节
  1. Install
  2. Usage
  3. Additional State
  4. selective ForceReply
  5. Known Issues
  6. The ForceReply question is always shown in some Telegram Cl…
  7. Sending an abort/back (inline) button with the question mes…

Stateless Question

Create stateless questions to Telegram users working in privacy mode

You want to keep the privacy of the user with Telegrams privacy mode enabled (by default), send users translated questions in their language and don't save the state what users are currently doing?

This library wants to solve this problem.

The basic idea is to send your question with a special text at the end. This text is invisible to the user but still visible for your bot. When the user replies to a message the message is checked. If it is containing this special text at the end it is an answer to the question. This way you can have many strings for the same questions as when having translations. You only have to make sure the uniqueIdentifier is unique within your bot.

Special thanks to @Ramin-Bateni and take a look on his explanation how this works if you like.

Install

npm install grammy @grammyjs/stateless-question

Usage

import {StatelessQuestion} from '@grammyjs/stateless-question';

const bot = new Bot(token);

const unicornQuestion = new StatelessQuestion('unicorns', ctx => {
    console.log('User thinks unicorns are doing:', ctx.message)
})

// Dont forget to use the middleware
bot.use(unicornQuestion.middleware())

bot.command('rainbows', async ctx => {
    let text
    if (ctx.session.language === 'de') {
        text = 'Was machen Einhörner?'
    } else {
        text = 'What are unicorns doing?'
    }

    return unicornQuestion.replyWithMarkdown(ctx, text)
})

// Or send your question manually (make sure to use a parse_mode and force_reply!)
bot.command('unicorn', async ctx => ctx.replyWithMarkdown('What are unicorns doing?' + unicornQuestion.messageSuffixMarkdown(), {parse_mode: 'Markdown', reply_markup: {force_reply: true}})
bot.command('unicorn', async ctx => ctx.replyWithHTML(    'What are unicorns doing?' + unicornQuestion.messageSuffixHTML(),     {parse_mode: 'HTML',     reply_markup: {force_reply: true}})

Additional State

When your question is specific for a certain topic then you can use the additionalState to remember that stateless with the message. For example when you want to know in which room an event is happening you can set the event as additionalState. This also helpful when working with telegraf-inline-menu to store the path to return the menu to.

const locationQuestion = new StatelessQuestion(
  "target",
  (ctx, additionalState) => {
    console.log("Location of", additionalState, "is", ctx.message.text);
    saveHeroLocation(additionalState, ctx.message.text);
  },
);

// Dont forget to use the middleware
bot.use(locationQuestion.middleware());

bot.command("batman", async (ctx) => {
  return locationQuestion.replyWithMarkdown(ctx, "Where is Batman?", "batman");
});

bot.command("superman", async (ctx) => {
  return locationQuestion.replyWithMarkdown(
    ctx,
    "Where is superman?",
    "superman",
  );
});

selective ForceReply

When using in groups in might be nice to only request a reply from the user starting the question. Telegram provides selected as an option for the ForceReply.

In order to make selected work you need to reply to a message (reply_to_message_id) or need to mention the user(s) which should reply to this.

await ctx.replyWithHTML(
  "What are unicorns doing?" + unicornQuestion.messageSuffixHTML(),
  {
    parse_mode: "HTML",
    reply_to_message_id: ctx.message.message_id,
    reply_markup: {
      force_reply: true,
      selective: true,
    },
  },
);

Known Issues

The ForceReply question is always shown in some Telegram Clients even long after the question has already answered

see #4

As of the design choices Telegram made to create ForceReply it's hard for a Telegram Client to know if a question is still relevant or not. As a workaround we can send ReplyKeyboardRemove as reply_markup with messages that do not require any other reply_markup (only one is possible for a message).

const unicornQuestion = new StatelessQuestion("unicorns", async (ctx) => {
  console.log("User thinks unicorns are doing:", ctx.message);
  await ctx.reply("if you think so...", {
    reply_markup: { remove_keyboard: true },
  });
});

Sending an abort/back (inline) button with the question message

see #1

Telegram allows only one reply_markup at a time. As this library relies on replies to a message ForceReply is already used as the reply_markup. In order to send an (inline) button with the same message it would require an (inline) keyboard markup. That would be a second reply_markup which Telegram sadly does not support.

版本与发布记录

@grammyjs/stateless-question 共发布 4 个版本,首次发布于 2021/09/16;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.0.0latest2024/05/242 年前
3.0.12021/10/205 年前
3.0.02021/09/235 年前
3.0.0-beta.12021/09/165 年前

关于 @grammyjs/stateless-question 的常见问题

@grammyjs/stateless-question 是什么?

Create stateless questions to Telegram users working in privacy mode

如何安装 @grammyjs/stateless-question?

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

@grammyjs/stateless-question 支持 TypeScript 吗?

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

@grammyjs/stateless-question 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@grammyjs/stateless-question 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=14。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@grammyjs/stateless-question 可以商用吗?

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

@grammyjs/stateless-question 的下载量有多少?

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

@grammyjs/stateless-question 还在维护吗?

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