A library to make simple chatbots faster
const Bot = new ChatBot();
Bot.to("greet").replyWith("Hello there!");
Bot.readInput("greet").then(console.log);Via CDN:
<script src="https://cdn.jsdelivr.net/npm/@darudlingilien/chatbot@1.0.0/cdn/chatbot.min.js"></script>Via NPM:
npm install @darudlingilien/chatbotTo create a chatbot you just need to create an instance of the ChatBot class, you can pass some options if you want:
const Bot = new ChatBot({
//Time to resolve replies
delay: 1000,
//All inputs must begin with this
answerPrefix: "./",
//Error messages
errors: {
onUnknow: "Unknow input",
onBadPrefix: "Invalid input prefix",
}
});to(input: Input)
To set some answers to your bot, you must call the to() method:
Bot.to("greet")to() method returns the Reply object, with which you can set the reply (or replies) that you want to receive
Bot.to("greet").replyWith("Hello world");to() method accepts string, RegExp and string[]
Bot.to("greet").replyWith("Hello world");
Bot.to(/^greet$/).replyWith("Hello world");
Bot.to(["greet", "greet me"]).replyWith("Hello world");Reply object has 4 methods:
replyWith(expr: string)-> set a basic reply
Bot.to("greet").replyWith("Hello world");fetchReply(callback: () => Promise<string>)-> Get a reply for a remote source
Bot.to("greet").fetchReply(async() => {
return await fetch("source")
.then((response) => response.json())
.then((reply) => reply);
});chooseReply(replies: string[])-> Choose a random reply from an array (on each response reply is random)
Bot.to("greet").chooseReply(["Hello world", "How are you?"]);chanceOfReply(replies: [string, number][])Choose a replie by chance
Bot.to("greet").chooseReply([["Hello world", 0.6], ["How are you?", 0.5]]);readInput(expr: string)
Read an string from a source, and try to resolve a proper reply to it. It returns a Promise<AnswerResponse>
Bot.readInput("greet").then(console.log);AnswerResponse interface:
interface AnswerResponse {
message: string,
state: "error"|"success"
}Chatbot is actually really simple, we'll keep it updated with new features of course, if you wanna contribute...
- Clone this repo locally
npm install- Code...
npm run test- Make a pull request