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', ...]
}

Last updated