Giter Club home page Giter Club logo

telegraf-stateless-question's Introduction

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

telegraf-stateless-question's People

Contributors

edjopato avatar phils-hub avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.