Responder

Instance of responder is passed as second parameter of handler (res)

Kind: global class

 

responder.newState

The empty object, which is filled with res.setState() method and saved (with Object.assign) at the end of event processing into the conversation state.

Kind: instance property of Responder
Properties

Type
Object
 

responder.finalMessageSent

Is true, when a final message (the quick replies by default) has been sent

Kind: instance property of Responder
Properties

Type
boolean
 

responder.data : Object

Kind: instance property of Responder

 

responder.run(blockName) ⇒ Promise

Run a code block defined by a plugin

Kind: instance method of Responder
Params

  • blockName string

Properties

Type
function
 

responder.setBookmark([action]) ⇒ this

Stores current action to be able to all it again

Kind: instance method of Responder
Params

  • [action] string

Example

bot.use(['action-name', /keyword/], (req, res) => {
    if (req.action() !== res.currentAction()) {
        // only for routes with action name (action-name)
        res.setBookmark();
        return Router.BREAK;
    }
    res.text('Keyword reaction');
});

// check out the res.runBookmark() method
 

responder.bookmark() ⇒ string | null

Returns the action of bookmark

Kind: instance method of Responder

 

responder.runBookmark(postBack, [data]) ⇒ Promise.<(null|boolean)>

Kind: instance method of Responder
Params

  • postBack function - the postback func
  • [data] Object - data for bookmark action

Example

// there should be a named intent intent matcher (ai.match() and 'action-name')

bot.use('action', (req, res) => {
    res.text('tell me your name');
    res.expected('onName');
});

bot.use('onName', (req, res, postBack) => {
    if (res.bookmark()) {
         await res.runBookmark(postBack);

         res.text('But I'll need your name')
             .expected('onName');
         return;
    }

    res.text(`Your name is: ${res.text()}`);
})
 

responder.setMessgingType(messagingType, [tag]) ⇒ this

Kind: instance method of Responder
Params

  • messagingType string
  • [tag] string = null
 

responder.isResponseType() ⇒ boolean

Returns true, when responder is not sending an update (notification) message

Kind: instance method of Responder

 

responder.setData(data) ⇒ this

Set temporary data to responder, which are persisted through single event

Kind: instance method of Responder
Params

  • data Object

Example

bot.use('foo', (req, res, postBack) => {
    res.setData({ a: 1 });
    postBack('bar');
});

bot.use('bar', (req, res) => {
    res.data.a; // === 1 from postback
});
 

responder.text(text, [...quickReplies]) ⇒ this

Send text as a response

Kind: instance method of Responder
Params

  • text string - text to send to user, can contain placeholders (%s)
  • [...quickReplies] ...Object.<string, string> | Array.<Object> - quick replies object

Example

// simply
res.text('Hello %s', name, {
    action: 'Quick reply',
    another: 'Another quick reply'
});

// complex
res.text('Hello %s', name, [
    { action: 'action', title: 'Quick reply' },
    {
        action: 'complexAction', // required
        title: 'Another quick reply', // required
        match: 'string' || /regexp/, // optional
        someData: 'Will be included in payload data' // optional
    }
]);
 

responder.setState(object) ⇒ this

Sets new attributes to state (with Object.assign())

Kind: instance method of Responder
Params

  • object Object

Example

res.setState({ visited: true });
 

responder.addQuickReply(action, title, [data], [prepend])

Appends quick reply, to be sent with following text method

Kind: instance method of Responder
Params

  • action string - relative or absolute action
  • title string - quick reply title
  • [data] Object - additional data
  • [prepend] boolean = false - set true to add reply at the beginning

Example

bot.use((req, res) => {
    res.addQuickReply('barAction', 'last action');

    res.addQuickReply('theAction', 'first action', {}, true);

    res.text('Text', {
        fooAction: 'goto foo'
    }); // will be merged and sent with previously added quick replies
});
 

responder.expected(action, data) ⇒ this

When user writes some text as reply, it will be processed as action

Kind: instance method of Responder
Params

  • action string - desired action
  • data Object - desired action data
 

responder.toAbsoluteAction(action) ⇒ string

Converts relative action to absolute action path

Kind: instance method of Responder
Returns: string - absolute action path
Params

  • action string - relative action to covert to absolute
 

responder.currentAction() ⇒ string

Returns current action path

Kind: instance method of Responder

 

responder.image(imageUrl, [reusable]) ⇒ this

Sends image as response. Requires appUrl option to send images from server

Kind: instance method of Responder
Params

  • imageUrl string - relative or absolute url
  • [reusable] boolean = false - force facebook to cache image

Example

// image on same server (appUrl option)
res.image('/img/foo.png');

// image at url
res.image('https://google.com/img/foo.png');
 

responder.video(videoUrl, [reusable]) ⇒ this

Sends video as response. Requires appUrl option to send videos from server

Kind: instance method of Responder
Params

  • videoUrl string - relative or absolute url
  • [reusable] boolean = false - force facebook to cache asset

Example

// file on same server (appUrl option)
res.video('/img/foo.mp4');

// file at url
res.video('https://google.com/img/foo.mp4');
 

responder.file(fileUrl, [reusable]) ⇒ this

Sends file as response. Requires appUrl option to send files from server

Kind: instance method of Responder
Params

  • fileUrl string - relative or absolute url
  • [reusable] boolean = false - force facebook to cache asset

Example

// file on same server (appUrl option)
res.file('/img/foo.pdf');

// file at url
res.file('https://google.com/img/foo.pdf');
 

responder.wait([ms]) ⇒ this

Sets delay between two responses

Kind: instance method of Responder
Params

  • [ms] number = 600
 

responder.typingOn() ⇒ this

Sends "typing..." information

Kind: instance method of Responder

 

responder.typingOff() ⇒ this

Stops "typing..." information

Kind: instance method of Responder

 

responder.seen() ⇒ this

Reports last message from user as seen

Kind: instance method of Responder

 

responder.passThread(targetAppId, [data]) ⇒ this

Pass thread to another app

Kind: instance method of Responder
Params

  • targetAppId string
  • [data] string | Object = null
 

responder.requestThread([data]) ⇒ this

Request thread from Primary Receiver app

Kind: instance method of Responder
Params

  • [data] string | Object = null
 

responder.takeThead([data]) ⇒ this

Take thread from another app

Kind: instance method of Responder
Params

  • [data] string | Object = null
 

responder.receipt(recipientName, [paymentMethod], [currency], [uniqueCode]) ⇒ ReceiptTemplate

Sends Receipt template

Kind: instance method of Responder
Params

  • recipientName string
  • [paymentMethod] string = "'Cash'" - should not contain more then 4 numbers
  • [currency] string = "'USD'" - sets right currency
  • [uniqueCode] string = null - when omitted, will be generated randomly

Example

res.receipt('Name', 'Cash', 'CZK', '1')
    .addElement('Element name', 1, 2, '/inside.png', 'text')
    .send();
 

responder.button(text) ⇒ ButtonTemplate

Sends nice button template. It can redirect user to server with token in url

Kind: instance method of Responder
Params

  • text string

Example

res.button('Hello')
    .postBackButton('Text', 'action')
    .urlButton('Url button', '/internal', true) // opens webview with token
    .urlButton('Other button', 'https://goo.gl') // opens in internal browser
    .send();
 

responder.genericTemplate([shareable], [isSquare]) ⇒ GenericTemplate

Creates a generic template

Kind: instance method of Responder
Params

  • [shareable] boolean = false - ability to share template
  • [isSquare] boolean = false - use square aspect ratio for images

Example

res.genericTemplate()
    .addElement('title', 'subtitle')
        .setElementImage('/local.png')
        .setElementAction('https://www.seznam.cz')
        .postBackButton('Button title', 'action', { actionData: 1 })
    .addElement('another', 'subtitle')
        .setElementImage('https://goo.gl/image.png')
        .setElementActionPostback('action', { actionData: 1 })
        .urlButton('Local link with extension', '/local/path', true, 'compact')
    .send();
 

responder.list([topElementStyle]) ⇒ ListTemplate

Creates a generic template

Kind: instance method of Responder
Params

  • [topElementStyle] 'large' | 'compact' = 'large'

Example

res.list('compact')
    .postBackButton('Main button', 'action', { actionData: 1 })
    .addElement('title', 'subtitle')
        .setElementImage('/local.png')
        .setElementUrl('https://www.seznam.cz')
        .postBackButton('Button title', 'action', { actionData: 1 })
    .addElement('another', 'subtitle')
        .setElementImage('https://goo.gl/image.png')
        .setElementAction('action', { actionData: 1 })
        .urlButton('Local link with extension', '/local/path', true, 'compact')
    .send();

results matching ""

    No results matching ""