Skip to content

Add fanciness: forms #5

@GochoMugo

Description

@GochoMugo

Forms can allow a more complex chain of queries between the bot and the user, without requiring the programmer to define explicitly the interactions between them.

The original inspiration for this feature was from https://dev.albertocoscia.me/nodeogram/quickstart.html#forms.

While it is possible to implement this entirely in tgfancy, I believe it will be better to have a more generic solution, i.e. a form engine, that can be used across multiple platforms, other than just Telegram, using "adapters'.

A favorable API can be adapted from Inquirer.js.

The expected code would look something like: (this is hypothetical!)

// npm-installed module
const FormSet = require('forms');
const TgformAdapter = require('forms-telegram');
const ConsoleformAdapter = require('forms-console');


// module variables
const formset = new FormSet();
const tgformAdapter = new TgformAdapter(bot);
const consoleformAdapter = new ConsoleformAdapter();


// adding the adapters
formset.addAdapter('telegram', tgformAdapter);
formset.addAdapter('console', consoleformAdapter);


// adding a form, used for building a user's profile i.e name, age, etc.
formset.addForm('profile', [
    // name
    {
        name: "name",
        message: "What is your name?",
        validate(answer) { /* ... validate ... */ },
        when(answers) { /* ... do we ask ... */ },
    },
    // ... more questions ...
]);


// let's ask for the details from the user, on the command '/start'
bot.onText(/^\/start$/, function(msg, match) {
    formset.send('profile', 'telegram', function(error, answers) {
        // ... handle error ...
        // 'answers' is an object with the user's answers
        console.log(answers);
    });
});

tgfancy would offer a formset by default, for more implicit solutions!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions