Send data when running the chatbot

Sometimes you have to send data to find out where the user is coming from or other information.

const NewBot = require('newbot')
const mainSkill = require('./myskill')

const converse = new NewBot(mainSkill)
converse.exec('Hey', 'user id', {
    output(text, done) => {
        console.log(text)
        done()
    },
    data: {

    }
})

The third parameter is an object. It contains the data property that can have any type of data.

Retrieve data

You can find the data in the functions of the skill, the writing of formats, the decorator @Condition, etc. For example, when you write a function, you have the data property:

import code from './main.converse'

export default {
    code,
    functions: {
        foo() {
            const { data } = this.converse
            console.log(data)
        }
    }
}

In the case of a format, data is a parameter:

import code from './main.converse'

export default {
    code,
    formats: {
        foo(text, params, data) {
            console.log(data)
        }
    }
}

Required for the newbot-formats module

When you run a skill using the newbot-formats module, you must execute the skill by sending asession object:

const NewBot = require('newbot')
const mainSkill = require('./myskill')

const converse = new NewBot(mainSkill)
converse.exec('Hey', 'user id', {
    output(text, done) => {
        console.log(text)
        done()
    },
    data: {
        session: {
            message: {
                source: 'messenger',
                agent: 'bottender'
            }
        }
    }
})

These data indicate the source and the agent. This will know where the user comes from and send him the answer on the right platform.

To simplify and standardize the writing of the session object, thenewbot-formats package contains Session type objects already developed by platform and agent (ex: newbot-formats/session/bottender) . See chapter Use with Bottender

In unit tests

The unit test can also send data:

import { ConverseTesting } from 'newbot/testing'
import mainSkill from './main'

describe('My own test', () => {
    let converse, userConverse

    beforeEach(() => {
        converse = new ConverseTesting(mainSkill)
        userConverse = converse.createUser({
            session: {
                message: {
                    source: 'website'
                }
            }
        })
    })
})

The first parameter of the createUser method contains the object corresponding todata

Last updated