npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@grammyjs/stateless-question

v4.0.0

Published

Create stateless questions to Telegram users working in privacy mode

Downloads

1,767

Readme

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.