📃
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

Was this helpful?

  1. Use NewBot Framework JS

Set up an NLP system

Let's use the nlp property:

export default {
    nlp: {

    }
}

Each key represents an NLP system. We have already had a first look at it with regular expressions under [/ docs / trigger / intent]. This time, we want to call a third-party API. Here is a complete example:

import rp from 'request-promise'
import code from './main.converse'

export default {
    code,
    nlp: {
        async myApi(text, userId, converse) {
            const user = converse.users.get(userId)
            const languageCode = user.getLang()
            const ret = await rp({
                url: '<YOUR URL>',
                method: 'POST',
                body: {
                    text,
                    languageCode
                },
                json: true
            })
            return {
                [ret.intentName]() {
                    return true
                }
            }
        }
    }
}
  1. We create a callback named myApi

  2. We can retrieve user information using userId and instance converse

  3. We make a request to the third-party API by sending data (here, the text and the current language of the user)

  4. We return a list of intentions following the same process as seen in chapter [/ docs / trigger / intent]

If ret.intentName is welcome, then we can trigger the intent in ConverseScript:

@Intent('welcome')
welcome() {
    > Welcome !
}

Share an NLP system with other skills

Here is the project structure:

  • bot

    • skills

      • skill-a

        • skill-a.js

      • skill-b

        • skill-b.js

    • nlp

      my-api.js

    • main.js

    • main.converse

As we can see, we created an nlp folder containing the my-api.js file. This file must return the NLP system:

import rp from 'request-promise'

export default async function myApi(text, userId, converse) {
    // request here or other code
}

In each skill, let's add the NLP system:

In skill-a.js, we have:

import myApi from '../../nlp/my-api'
import code from './skill-a.converse'

export default {
    code,
    nlp: {
        myApi
    }
}

In skill-b.js, we have:

import myApi from '../../nlp/my-api'
import code from './skill-b.converse'

export default {
    code,
    nlp: {
        myApi
    }
}

At first glance, we find that the NLP system will be called twice: once in the skill-a skill and another time in the skill-b skill. If the NLP system makes a request, it is not wise to do the same request twice. Do not worry, NewBot has been optimized. He caches the NLP system and calls it only once.

Spread the NLP system

Although the previous method is the most recommended (because we can do unit tests per skill), we can also use the propagateNlp property on a parent skill to propagate the NLP system to child skills:

In the main.js file:

import myApi from './nlp/my-api'
import code from './main.converse'

export default {
    code,
    nlp: {
        myApi
    },
    propagateNlp: true
}

In this way, all the child skills will have the NLP system. We do not need to put in each child's skill

Propagate only certain NLP system

You can only propagate one or more systems:

import myApi from './nlp/my-api'
import code from './main.converse'

export default {
    code,
    nlp: {
        myApi
    },
    propagateNlp: 'myApi'
    // or propagateNlp: ['myApi', 'otherNlp', ...]
}
PreviousInternationalization (i18n)NextShare the NLP system

Last updated 3 years ago

Was this helpful?