# 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:

```typescript
@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

```javascript
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

```javascript
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`

```markup
<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.

```javascript
converse.exec('input text', 'USER ID', (output, done) => {
    done()
})
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.newbot.io/use-newbot-framework-js/save-the-users-progress.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
