Skip to content

benchambule/lottus.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lottus.js

=====================

Installation

npm i 'github:benchambule/lottus.js#main'  

Examples


Let's build a bot that reverses a string, and it is triggered by the @reverse keyword. The bot will receive a sentence like @reverse hello world and return dlrow olleh as a response. This bot does not need a session because there's no follow-up requests

'use strict';

import { lottus } from "lottus.js";

let bot = lottus();

bot.info("main", async (req, res) => {
    res.body = req.sentence.split("").reverse().join("");

    return res;
});

console.log(await bot.process({sentence: "hello world"}));

Let's update our previous bot to be respond accordind to the language. We will be listening to request.lang to check if it's set to 'pt' or 'en'. If the request.lang is not set, we will default to 'en'. If request.lang is other than 'en' and 'pt' we will respond with a message informing the customer that the provided language is unknown or not implemented yet.

'use strict';

import { lottus } from "lottus.js";

let reverse = lottus();

reverse.info("main", async (req, res) => {
    res.body = "Unkonw language";
    const reversed = req.sentence.split("").reverse().join("");

    if(req.parameters.lang === 'pt'){
        res.body = "Frase invertida: " + reversed
    }else if(req.parameters.lang === 'en'){
        res.body = "Reversed: " + reversed
    }
    
    return res;
});

console.log((await reverse.process({sentence: "hello world", parameters:{lang: "pt"}})));
console.log((await reverse.process({sentence: "hello world", parameters:{lang: "en"}})));
console.log((await reverse.process({sentence: "hello world", parameters:{lang: "es"}})));

Let's build another bot, this time it will have 3 messages and the user can navigate back and forth between between the messages. For this bot, we will need a session manager/storage implementation that the bot will use.

'use strict';

import { lottus } from "lottus.js";

let options = lottus();

options.info("main", async (_, msg) => {
    msg.form = {input: "options"};
    msg.body = "Please select an option";
    msg.body += "\n1 - Name";
    msg.body += "\n2 - Profession";

    return msg;
});

options.form("main", async (req, msg) => {
    msg.form = {input: "options"};
    msg.body += "\nUnknow option";

    if(req.prompt.trim() === "1"){
        msg = redirect("name");
    }

    if(req.prompt.trim() === "2"){
        msg = redirect("profession");
    }

    return msg;
});

options.info("name", async (_, msg) => {
    msg.form = {input: "options"};

    msg.body = "You selected 1 - Name";
    msg.body += "\n0 - Back to main";

    return msg;
});

options.form("name", async (req, msg) => {
    msg.form = {input: "options"};
    msg.body += "\nUnknow option";

    if(req.prompt.trim() === "0"){
        msg = redirect("main");
    }

    return msg;
});

options.info("profession", async (_, msg) => {
    msg.form = {input: "options"};
    msg.body = "You selected 2 - Profession";
    msg.body += "\n0 - Back to main";

    return msg;
});

options.form("profession", async (req, msg) => {
    msg.form = {input: "options"};
    msg.body += "\nUnknow option";

    if(req.prompt.trim() === "0"){
        msg = redirect("main");
    }

    return msg;
});

let message = await options.process({prompt:"hello"});
console.log(message);

message = await options.process({prompt:"1"}, message);
console.log(message);

message = await options.process({prompt:"0"}, message);
console.log(message);

message = await options.process({prompt:"2"}, message);
console.log(message);

Let's update the code to use lottus.js built-in features

import { form_processor, format_message, lottus } from "../index.js";

let options = lottus();

options.info("main", async (_, msg) => {
    msg.body = "Please select an option";

    msg.addAutoOption({label: "Name", next:"name"});
    msg.addAutoOption({label: "Profession", next:"profession"});

    return msg;
});

options.form("main", form_processor);

options.info("name", async (_, msg) => {
    msg.body = "You selected 1 - Name";
    msg.addOption({label: "Back", key: 0, next: "main"});

    return msg;
});

options.form("name", form_processor);

options.info("profession", async (_, msg) => {
    msg.body = "You selected 2 - Profession";
    msg.addOption({label: "Back", key: 0, next: "main"});

    return msg;
});

options.form("profession", form_processor);

let message = await options.process({prompt:"hello"});
console.log(format_message(message));

message = await options.process({prompt:"1"}, message);
console.log(format_message(message));

message = await options.process({prompt:"0"}, message);
console.log(format_message(message));

message = await options.process({prompt:"2"}, message);
console.log(format_message(message));

message = await options.process({prompt:"0"}, message);
console.log(format_message(message));

features

  • Session management - with InMemorySessionManager. SQLiteSessionManager coming soon...
  • Automatically processing options and input. Input validations coming soon...