📃
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
  • Use ConverseScript parameters
  • Create a mock of the function

Was this helpful?

  1. Use NewBot Framework JS

Create a JavaScript function and trigger it in ConverseScript

Let's use the functions property:

import code from './main.converse'

export default {
    code,
    functions: {
        nameFunctionJs() {

        }
    }
}

The ConverseScript script uses the function:

nameFunctionJs()

If the function has asynchronous values, let's return a promise:

import code from './main.converse'

export default {
    code,
    functions: {
        nameFunctionJs() {
            return new Promise((resolve, reject) => {
                // any code
                resolve()
            })
        }
    }
}

Use ConverseScript parameters

Often, information is essential for the proper functioning of the function. For example, retrieve the user information or retrieve the user collection.

Using a magic variable

import code from './main.converse'

export default {
    code,
    functions: {
        nameFunctionJs() {
            const { user } = this.converse
            // any code
            user.setMagicVariable('foo', 'hello')
        }
    }
}

The script is as follows:

start() {
    nameFunctionJs()
    > { :foo }
}

Property converse

The converse property is an object:

{
    users: Map<User>,
    user: User,
    data: any,
    execution: Execution,
    level: String,
    ins: Object
}

Return of a function

import code from './main.converse'

export default {
    code,
    functions: {
        nameFunctionJs() {
            return 'hello'
        }
    }
}

The script is as follows:

start() {
    str = nameFunctionJs()
    > { str }
}

Use an object

It is possible to use an object with several functions:

import code from './main.converse'

export default {
    code,
    functions: {
        myObj: {
            nameFunction() {

            }
        }
    }
}

In ConverseScript

start() {
    myObj.nameFunction()
}

With specific parameters:

import code from './main.converse'

export default {
    code,
    functions: {
         myObj: {
            nameFunction() {
                const { user } = this.converse
            }
        }
    }
}

Create a mock of the function

It is very useful to mock a function. Even though each function can be mocked in each unit test, it is also possible to change the behavior in a global way by associating the mock with a skill, which will be triggered during unit tests.

import code from './main.converse'

export default {
    code,
    functions: {
         nameFunctionJs: {
            $call() {
                const { user } = this.converse
                return 'hello'
            },
            $mock() {
                const { user } = this.converse
                return 'fake'
            }
        }
    }
}
PreviousControl access to a skillNextThe constants

Last updated 3 years ago

Was this helpful?