Botnoi Dev Platform

Botnoi Dev Platform is a programatical alternative to the visual chatbot builder provided by https://botnoi.ai . It can be used to build and train chatbots for various platforms such as Facebook Messenger and LINE.

This official library provides a Dart implementation of Botnoi Dev Platform's API in order to simplify the process of building chatbots. It also provides various data types related to chatbots such as 'BotIntent', 'BotObject' and lots of helper functions to work with them.

This suits best for developers who want to build chatbots programmatically or to automate the process of chatbot training when an action is performed by the applications' users.

Installation

In your project folder, run this command in the terminal

flutter pub add botnoi_dev_platform

then, import the package

import 'package:botnoi_dev_platform/botnoi_dev_platform.dart';

note: you have to have an API key to use this package. You can get one from https://botnoi.ai

Usage

Set up

First, initialize the BotnoiChatbot server with your API key

BotnoiChatbot.initializeClient(key: 'your_api_key');

Then, you can create a BotnoiChatbot server instance

BotnoiChatbot server = BotnoiChatbot.serverInstance;

(Optional but Recommended) You can also initialize the collector. The collector is a very useful tool for collecting chatbot assets such as bot, intents, and objects, so you can use them later without having to reload them from the server or passing their instances around your appllication.

BotnoiChatbot.initializeCollector();

note: for more information about the collector, see the 'Using the collector' section below.

Create a bot

Creating a bot is simple. You just have to create a bot instance and add it to the server

// create a new Bot instance
Bot myBot = Bot(
  botName: "myBot",
  sex: "male",
  age: 20,
  owner: "newOwner",
  botAvatar: "https://example.com/avatar.png",
  businessType: "Education",
);

// add the new Bot instance to the server
await server.createBot(bot: myBot);

// reload the bot from the server
// this is required because the bot's id is generated after adding it to the server
await myBot.reload();

Or find an existing bot by name

Bot? myBot = await server.findBotWithName("myBot");

Create and train an intent

You can create an intent in side of your bot by creating a new BotIntent instance and add it to the bot

// create a new BotIntent instance
BotIntent myIntent = BotIntent(name: "myIntent");

// add the new BotIntent instance to the bot
await myBot.opt.createIntent(intent: myIntent);

// reload the intent from the server
// this is required because the intent's id is generated after adding it to the bot
await myIntent.reload();

Or find an existing one by name

BotIntent? myIntent = await myBot.opt.findIntentWithName("myIntent");

notice that the 'opt' property is used when you want to do something with the bot that is related to Botnoi Dev Platform (eg. createIntent, findIntents, etc.). This is the standard for all data types that can directly interact with the server (eg. Bot, BotIntent, etc.).

You can train the intent by adding some keywords(inputs) and reactions(outputs) to it

await myIntent.opt!.trainKeyword(keyword: BotIntentKeyword(keyword: "Hello"));
await myIntent.opt!.trainReaction(reaction: BotIntentReaction(actions: ["Hello", "How are you?"]));

Or train the reaction with more complex actions like this

myIntent.opt!.trainReaction(
  reaction: BotIntentReaction(actions: [
    BotAction.createParameter("user_name", BotAction.keyword()), //create a user_name parameter containing the last respond from the user.
    BotAction.image("greeting_image"), //show greeting_image object. To learn more about objects, see the 'Create an object' section below.
    BotAction.text("Hello, ${BotAction.parameter("user_name")}"), //get the user_name parameter and use it in the text respond.
  ]),
);

Create an object

Creating objects is similar to creating intents. First, create a BotObject instance with typed objects in its objects property (eg. BotApiObject, BotImageObject, BotButtonObject etc.). Every typed object instances must be of the same type. Then, you can add the BotObject instance to the bot.

// create a new BotObject instance with a BotApiObject instance in its objects property
BotObject myApiObject = BotObject(
  objectName: "myApiObject",
  objects: [
    BotApiObject(
      url: "https://api_example.com",
      method: "post",
      header: {"Content-Type": "application/json"},
      body: "{'data': 'example_data'}",
    )
  ],
);

// add the new BotObject instance to the bot
await myBot.opt.createObject(object: myApiObject);

Or find an existing one by name

BotObject? myApiObject = await myBot.opt.findObjectWithName(name: "myApiObject", type: BotObjectType.api);

The collector is a very useful tool for collecting chatbot assets such as bot, intents, and objects, so you can use them later without having to reload them from the server or passing their instances around your appllication.

These are 2 ways of how to use the collector.

Example 1 : Collect assets manually

  1. initialize the collector
BotnoiChatbot.initializeCollector();
  1. create a bot as normal then collect it.
Bot myBot = Bot(
    botName: "myBot",
    sex: "male",
    age: 20,
    owner: "newOwner",
    botAvatar: "https://new.com/avatar.png",
    businessType: "education",
  );
await BotnoiChatbot.serverInstance.createBot(bot: myBot);
await myBot.reload();
BotnoiChatbot.collector.collectBot(myBot);
  1. Get the bot from the collector, create an intent in it and collect the intent.
await BotnoiChatbot.collector.getBot().then((bot) async {
  BotIntent myIntent = BotIntent(name: "myIntent");
  await bot?.opt.createIntent(intent: myIntent);
  await myIntent.reload();
  BotnoiChatbot.collector.collectIntent(myIntent);
});
  1. Get the collected intent from the collector and train it.
await BotnoiChatbot.collector.getIntent("myIntent").then((intent) async {
  await intent?.opt?.trainKeyword(keyword: BotIntentKeyword(keyword: "Hello"));
  await intent?.opt?.trainReaction(reaction: BotIntentReaction(actions: ["Hello", "How are you?"]));
});

Example 2 : Let the collector collect assets automatically

  1. initialize the collector with implicitCollecting set to true
BotnoiChatbot.initializeCollector(implicitCollecting: true);
  1. create a bot as normal except you don't have to keep its instance anymore
await BotnoiChatbot.serverInstance.createBot(
  bot: Bot(
    botName: "myBot",
    sex: "male",
    age: 20,
    owner: "newOwner",
    botAvatar: "https://new.com/avatar.png",
    businessType: "education",
  ),
);
  1. Get the bot from the collector and create an intent in it. Notice that you don't have to explicitly collect the bot. If the bot with the specified name is not collected, the collector will automatically load and collect it.
await BotnoiChatbot.collector.getBot("myBot").then((bot) async {
  BotIntent newIntent = BotIntent(name: "myIntent");
  await bot?.opt?.createIntent(intent: newIntent);
});
  1. Train the intent. Just like the bot, you don't have to explicitly collect the intent before getting it.
await BotnoiChatbot.collector.getIntent("myIntent").then((intent) async {
  await intent?.opt?.trainKeyword(keyword: BotIntentKeyword(keyword: "Hello"));
  await intent?.opt?.trainReaction(reaction: BotIntentReaction(actions: ["Hello", "How are you?"]));
});
  1. Keep in mind that the collected assets will be stored on local and will not be automatically updated or deleted when you update or delete them from the server. To update or delete the collected assets, you have to do it manually.
//reload an intent after updating it on the server
BotnoiChatbot.collector.reloadIntent("myIntent");

//delete an intent from the collector
BotnoiChatbot.collector.trashIntent("myIntent");

note: since every step only depends on BotnoiChatbot and not on the instances of the assets, they could be from anywhere in your application. For example, you can create a bot from one screen, create an intent in that bot from another screen, then train the intent from yet another screen, without having to pass the bot or intent instances around your application.

Handling errors

By default, this library will raise errors when they occur. To handle these errors, you can set the callback function to be called when an error occurs to stop it from being raised.

BotnoiChatbot.setErrorCallback((error) {
  // handle the error here
  print(error);
});

Or if you just want to check if the last function called was successful, you can use this

bool status = BotnoiChatbot.didFinishSuccesfully