Conditional

It happens several times that one wishes to execute a different skill according to the platform or certain information of the user. For example, we could have a following skill:

profile() {
    Messenger.getProfile() // custom function to get Messenger profile
    > Your name is { : response.data.name }
}

We can think that this method allows to recover the user profile on Messenger

However, we want to achieve a universal scenario and have the same on Line:

profile() {
    Line.getProfile() // custom function to get Line profile
    > Your name is { : response.data.displayName }
}

We will need to use a conditional skill to switch from one to another depending on the platform:

import formats from "newbot-formats";
import code from "./main.converse";

import profileMessengerSkill from "./skills/profile/messenger/profile";
import profileLineSkill from "./skills/profile/line/profile";

export default {
  code,
  skills: {
    formats,
    profileSkill: [
      {
        skill: profileMessengerSkill,
        condition(data, user) {
          return data.session.platform == "messenger";
        }
      },
      {
        skill: profileLineSkill,
        condition(data, user) {
          return data.session.platform == "line";
        }
      }
    ]
  }
};

Note several points:

  1. The name of the skill is always the same profileSkill

  2. We use the condition property that returns a Boolean

Call in the main script

In the main script main.converse, we can call the skill using its name:

@Event('start')
start() {
    profileSkill.profile()
}

The right skill will be triggered according to the platform!

Property condition

Settings

</api-table>

Return value

Boolean or Promise

We can delay the conditional test with a promise.

condition(data, user) {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve(data.session.platform == "line")
        }, 2000)
    })
}

Last updated