botnoi_dev_platform 0.0.20
botnoi_dev_platform: ^0.0.20 copied to clipboard
A flutter package for creating and training chatbot on botnoi.ai using botnoi developer platform api.
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 'BotIntents', 'BotObjects' 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 #
first, add the package to your pubspec.yaml file
dependencies:
botnoi_dev_platform: ^latest_version
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 #
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;
Create or get 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 by the server
// alternatively, you can find the bot by name using server.findBotWithName("myBot")
await myBot.reload();
Or find an existing bot by name
Bot myBot = (await server.findBotWithName("myBot"))!;
Create and train intents #
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 by the server
// alternatively, you can find the intent by name using myBot.opt.findIntentWithName("myIntent")
await myIntent.reload();
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 will be the standard for all data types related to Botnoi Dev Platform (eg. Bot, BotIntent, etc.)
You can train the intent by adding some keywords(inputs) and messages(outputs) to it
await myIntent.opt!.trainKeyword(keyword: "Hello");
await myIntent.opt!.trainMessage(message: "Hello! how are you?");
Create objects #
Creating objects is similar to creating intents (but a little harder). First, you have to create an object instance of your preferred type (eg. BotApiObject, BotImageObject, etc.) then wrap it within a BotObject instance and add the BotObject instance to the bot.
// create a new BotApiObject instance
BotApiObject newAPI = BotApiObject(
url: "https://api_example.com",
method: "POST",
header: {"Content-Type": "application/json"},
body: "{'data': 'example_data'}",
);
// create a new BotObject instance with the BotApiObject instance in its objects property
BotObject myObject = BotObject(
objectName: "newObject",
objectType: "api",
objects: [newAPI.toJson()], //toJson() is required to convert the BotApiObject instance to a Map
);
// add the new BotObject instance to the bot
await myBot.opt.createObject(object: myObject);
Handling errors #
Botnoi Dev Platform is designed not to break the application when errors occur. Instead, it will stream the error messages. To handle these errors, you can set the callback function to be called when an error occurs (by default, it will print the error message).
BotnoiChatbot.setErrorCallback((error) {
// handle the error here
print(error);
});
Or if you just want to check if the function call was successful, you can use this
// This will return the status of the last function called in this package.
bool status = BotnoiChatbot.didFinishSuccesfully