@puregram/prompt
Basic prompt system implemetation for puregram
package
Introduction
@puregram/prompt
can ask user to send some message and will keep that message in PromptAnswer.context
variable, which you can access and do whatever you want
Example
const { Telegram } = require('puregram');
const { PromptManager } = require('@puregram/prompt');
const telegram = new Telegram({
token: process.env.TOKEN
});
const promptManager = new PromptManager();
telegram.updates.use(promptManager.middleware);
telegram.updates.on('message', (context) => {
let nameAnswer;
while (!nameAnswer || !nameAnswer.text) {
nameAnswer = await context.prompt('Hey! Please, enter your name.')
}
const name = nameAnswer.text; // also accessible via `nameAnswer.context.text`
let ageAnswer;
while (!ageAnswer || !ageAnswer.text || Number.isNaN(+ageAnswer.text)) {
ageAnswer = await context.prompt('Cool! Now please enter your age.');
}
const age = Number.parseInt(ageAnswer.text);
return context.send(`You're welcome, ${name} ${age} y.o.!`);
});
telegram.updates.startPolling();
Installation
$ yarn add @puregram/prompt
$ npm i -S @puregram/prompt
TypeScript usage
You can extend context's properties by passing PromptContext
into Updates.on<T>
:
telegram.updates.on<PromptContext>('message', (context) => {
/* Now you have access to `prompt` and `promptReply` methods via types! */
});