/telegraf-stateless-question

Create stateless questions to Telegram users working in privacy mode

Primary LanguageTypeScriptMIT LicenseMIT

telegraf-stateless-question

NPM Version node Build Status Dependency Status Peer Dependency Status Dev Dependency Status

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 dont 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 different strings for the same questions like 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 telegraf telegraf-stateless-question

Usage

const TelegrafStatelessQuestion = require('telegraf-stateless-question');

const bot = new Telegraf(token);

const unicornQuestion = new TelegrafStatelessQuestion('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 Markdown or HTML and forceReply!)
bot.command('unicorn', async ctx => ctx.replyWithMarkdown('What are unicorns doing?' + unicornQuestion.messageSuffixMarkdown(), Extra.markdown().markup(Markup.forceReply()))
bot.command('unicorn', async ctx => ctx.replyWithHTML(    'What are unicorns doing?' + unicornQuestion.messageSuffixHTML(),     Extra.markdown().markup(Markup.forceReply()))

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 TelegrafStatelessQuestion('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')
})