📃
NewBot
  • NewBot, what is it?
  • ConverseScript Syntax
    • Variables
    • Arithmetic
    • Boolean
    • Array and object
    • Entering text and output
    • Magic variables
    • Condition
    • Loop
    • Functions
    • The decorators
      • Use @Condition
      • Use @Action
      • Use @Format
      • Use @Intent
      • Use @Event
  • Prebuilt Formats for Widget
    • Quick Replies
    • Multi Cards
    • Articles
    • Form
  • Pre-built function for the widget
    • Request()
    • Map()
    • _ (lodash)
  • Get Started with Framework
    • Install
    • Main Skill
    • Write the conversational script
  • Use NewBot Framework JS
    • Create a skill (more details)
      • Relationship between skills
      • Use the functions of a child competency
      • Conditional
      • Control access to a skill
    • Create a JavaScript function and trigger it in ConverseScript
    • The constants
    • Write formats
      • Write and use multi-formats
      • Share formats
    • Internationalization (i18n)
    • Set up an NLP system
      • Share the NLP system
      • Use DialogFlow
    • Deploy on the browser
    • Run the chatbot on NodeJS
    • Send data when running the chatbot
    • The middlewares
    • Save the user's progress
    • User object
  • Unit Tests
    • Test a scenario
    • Test variables
    • Spy a function
    • Create a mock
    • Test the events
    • Test the actions
Powered by GitBook
On this page
  • Retrieve data
  • Required for the newbot-formats module
  • In unit tests

Was this helpful?

  1. Use NewBot Framework JS

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

PreviousRun the chatbot on NodeJSNextThe middlewares

Last updated 3 years ago

Was this helpful?