createIntent method

Future<void> createIntent({
  1. required BotIntent intent,
  2. bool derived = false,
  3. BotIntent? source,
  4. Map<String, String> mapping = const {},
})

Create a new intent for this bot.

  • {intent} is the intent that you want to create.

  • {derived} is a boolean that indicate whether you want to derive this intent from an existing intent. If this is true, {source} must also be provided.

  • {source} is the intent that you want to derive from. This is only required if {derived} is true.

  • {mapping} is used to replace placeholders in the keywords and reactions when deriving from an existing intent. For example, if you have a keyword "I want to go to {place}" and you want to replace {place} with "Bangkok", you can do this:

bot.opt.createIntent(
  intent: BotIntent(name: "example" ),
  derived: true,
  source: destinationIntent,
  mapping: { "place": "Bangkok" },
);

notice that placeholders must be surrounded by curly braces.

Implementation

Future<void> createIntent({
  required BotIntent intent,
  bool derived = false,
  BotIntent? source,
  Map<String, String> mapping = const {},
}) async {
  try {
    intent.belongToBot(_bot);
    Uri url = Uri.parse(
        "${GetIt.I<BotnoiClient>().endpoint}/developer/platform-api/intent");
    http.Response response = await http.post(
      url,
      headers: _getHeader,
      body: jsonEncode(intent.toJson()),
    );
    if (response.statusCode == 201 || response.statusCode == 200) {
      GetIt.I<BotnoiClient>().finishedSuccessfully();
      if (derived) {
        if (source == null) {
          GetIt.I<BotnoiClient>().finishedFailed();
          GetIt.I<BotnoiClient>().error.add(
              "[createIntent] : If derived is true, source must be provided");
          return;
        }
        if (source.id == null) {
          GetIt.I<BotnoiClient>().finishedFailed();
          GetIt.I<BotnoiClient>()
              .error
              .add("[createIntent] : source intent must have an id");
          return;
        }
        BotIntent? newIntent = await findIntentWithName(intent.name);
        if (newIntent == null) return;
        await newIntent.opt
            ?.deriveFromIntent(source: source, mapping: mapping);
      }
      return;
    }
    GetIt.I<BotnoiClient>().finishedFailed();
    GetIt.I<BotnoiClient>()
        .error
        .add("[createIntent] : ${response.reasonPhrase ?? "ERROR"}");
  } catch (e) {
    GetIt.I<BotnoiClient>().finishedFailed();
    GetIt.I<BotnoiClient>().error.add("[createIntent] : $e");
  }
}