📃
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
  • Load user data
  • Browser example with localStorage

Was this helpful?

  1. Use NewBot Framework JS

Save the user's progress

If the user has already started the scenario, it would be nice not to have him start the conversation again from scratch.

Take the following scenario:

@Event('start')
start() {
    > Hello, what is your name ?
    Prompt()
    > Ok, { :text }. And your age ?
    Prompt()
    > Thanks !
}

With the help of a middleware, we can recover the progress of the user

const { Newbot } = require('newbot')

const converse = new Newbot()

converse.use({ 
    finished(input, { user, data }) {
       const json = user.toJson()
    } 
})

Here, when the user arrives at the request of a text input (that is, when the user arrives at Prompt() in the scenario), his progress is saved in the constant json

We can save the content in a database like MongoDB, CouchDB, etc.

Load user data

It is good to load a set of users. Suppose each db.users() below is a call to the database to retrieve a collection of data

const retUsers = db.users()
converse.loadUsers(retUsers)

Browser example with localStorage

Assuming that path/dist/browser.js is the path to the generated file after doing newbot build

<script src="https://unpkg.com/newbot@latest/dist/newbot.min.js"></script>
<script src="path/dist/browser.js"></script>

<script>
const converse = new NewBot(mainSkill)

const progress = localStorage.getItem('progress')

if (progress) {
    converse.loadUsers([
        JSON.parse(progress)
    ])
}

converse.use({
    finished(input, { user }) {
       const json = JSON.stringify(user.toJson())
       localStorage.setItem('progress', json)
    }
})

converse.exec('Hey', (output, done) => {
    console.log(output)
    done()
})
</script>

Here we notice: 1. We load the user initially if, of course, we have a record in localStorage 2. We use the finished middleware to register the current user in localStorage

::: tip User ID On the server side, let us indicate the username of the user when using the .exec() method.

converse.exec('input text', 'USER ID', (output, done) => {
    done()
})
PreviousThe middlewaresNextUser object

Last updated 3 years ago

Was this helpful?