callbackMiddleware ⇒ function

Creates callback middleware, which allows to get user back to previous context

Example

const { Router, callbackMiddleware, ai } = require('botnaut');

const bot = new Router();

bot.use(callbackMiddleware());

bot.use(['fooRoute', /^foo$/], (req, res) => {
    if (!req.isFromCallback()) {
        // is hidden, when user is just comming back
        res.text('This is your FOO response');
    }
    if (!req.hasCallback()) {
        res.setCallback('fooRoute');
    }

    // ability get back to previous content
    res.addCallbackQuickReply('Go back');

    res.text('So, what you want?', {
        barRoute: 'Go to bar'
    });
 })

 bot.use(['barRoute', /^bar$/], (req, res) => {
    if (!req.isFromCallback()) {
        res.text('This is your BAR response');
    }
    if (!req.hasCallback()) {
        res.setCallback('barRoute');
    }

    if (!res.proceedCallback()) {
        res.text('So, what\'s next?', {
            fooRoute: 'Go to foo'
        });
    }
 });
 

setCallback(action, [callbackContext], [callbackText]) ⇒ this

Sets action, where to go back, when user responds with text

Kind: Exported function

Param Type Default Description
action string relative or absolute action (usualy current action)
[callbackContext] string \ null null context of callback
[callbackText] string \ null null custom text response

Example

bot.use('myAction', (req, res) => {
    res.setCallback('myAction'); // return back
});
 

hasCallback([callbackContext]) ⇒ boolean

Returns true, when callback has been prevously set. It's usefull, when you don't want to bouce back the methods.

Kind: Exported function

Param Type Default
[callbackContext] string null

Example

bot.use(['fooRoute', /^foo$/], (req, res) => {
    // set callback, only when this request does not have one
    if (!req.hasCallback()) {
        res.setCallback('fooRoute');
    }
});
 

isFromCallback([callbackContext]) ⇒ boolean

Returns true, when user is comming back from callback Comeback is initialised with res.proceedCallback() or quick reply Usefull for hidding the text, user has already seen

Kind: Exported function

Param Type Default
[callbackContext] string null

Example

bot.use(['fooRoute', /^foo$/], (req, res) => {
    // set callback, only when this request does not have one
    if (!req.isFromCallback()) {
        res.text('this is the response, you dont want to read again');
    }
});
 

proceedCallback([callbackContext]) ⇒ boolean

Proceed a callback, when exists (go to action, where the callback has been previously set) Returns true, when postback will occur

Kind: Exported function

Param Type Description
[callbackContext] string the context

Example

bot.use(['fooRoute', /^foo$/], (req, res) => {
    // set callback, only when this request does not have one
    if (!res.proceedCallback()) {
        res.text('this is the followup question', {
            followupAction: 'Continue'
        });
    }
});
 

addCallbackQuickReply(replyText) ⇒ this

Adds "back" quick reply to other replies (alternative to proceedCallback())

Kind: Exported function

Param Type Description
replyText string the default text

Example

bot.use(['fooRoute', /^foo$/], (req, res) => {
    // ability get back to previous context
    res.addCallbackQuickReply('Go back');

    res.text('So, what you want?', {
        barRoute: 'Go to bar'
    });
});

results matching ""

    No results matching ""