GithubHelp home page GithubHelp logo

benchambule / lottus.js Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 213 KB

License: Apache License 2.0

JavaScript 100.00%
bot bot-js botjs node-bot nodebot nodebots nodejs whatsapp-web whatsapp-web-js whatsappbot

lottus.js's Introduction

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';

const {Bot} = require('lottus.js');

var reverse = new Bot(
    {
        name: "reverse", 
        entrypoint: 'main', 
        keyword: "@reverse", 
        inline: true, // This bot is inline, so it won't create a session
        description: "Reverses a provided string" 
    }
);

reverse.at('main', async function(request, context){
    const prompt = request.prompt.replace(context.bot.keyword, "").trim();
    const menu = {
        text: '👉 ' + prompt.split("").reverse().join(""), 
        final: true
    }

    return {
        menu: menu
    };
});

console.log(reverse.process({msisdn: 123, await prompt:"@reverse giberish"}));
console.log(reverse.process({msisdn: 123, await prompt:"@reverse bonjour le monde"}));
console.log(reverse.process({msisdn: 123, await prompt:"@reverse ola mundo"}));
console.log(reverse.process({msisdn: 123, await prompt:"@reverse 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';

const {Bot} = require('lottus.js');

var reverse = new Bot(
    {
        name: "reverse", 
        entrypoint: 'main', 
        keyword: "@reverse", 
        inline: true, // This bot is inline, so it won't create a session
        description: "Reverses a provided string" 
    }
);

reverse.intercept('main', async (request) => {
    if(!request.lang){
        request.lang = 'en';
        return {
            request: request
        }
    }

    if(request.lang != 'pt'  && request.lang != 'en'){
        const menu = {
            title: 'lang cannot be ' +  "'" + request.lang+ "'" + ". lang should be 'en' or 'pt'",
            final: true
        }
        return {
            menu: menu
        };
    }
});

reverse.at('main', async function(request, context){
    const txt = request.lang == 'pt'? 'Frase invertida' : 'Reversed sentence';
    const prompt = request.prompt.replace(context.bot.keyword, "").trim();
    const menu = {
        title: txt,
        text: prompt.split("").reverse().join(""), 
        final: true
    }

    return {
        menu: menu
    };
});

console.log(await reverse.process({msisdn: 123, prompt:"@reverse giberish"}));
console.log(await reverse.process({msisdn: 123, prompt:"@reverse bonjour le monde", lang:"fr"}));
console.log(await reverse.process({msisdn: 123, prompt:"@reverse ola mundo", lang:"pt"}));
console.log(await reverse.process({msisdn: 123, prompt:"@reverse hello world", lang:"en"}));

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

'use strict';

const {Bot, InMemorySessionManager} = require('lottus.js');

const menus = [
    {
        name: "welcome",
        title: "Welcome",
        message: "Language | Idioma",
        options: [
            {key:1, label:"English", menu:"english"},
            {key:2, label:"Portugues", menu:"portuguese"},
        ]
    },

    {
        name: "english",
        title: "English",
        message: "This menu is in english",
        options: [
            {key:0, label:"Back", menu:"welcome"}
        ]
    },

    {
        name: "portuguese",
        title: "Portugues",
        message: "Este menu está em portugues",
        options: [
            {key:0, label:"Voltar", menu:"welcome"}
        ]
    }
]

var bot = new Bot(
    {
        name: "bot", 
        entrypoint: 'welcome', 
        keyword: "@bot", 
        inline: false, 
        description: "This is an enquiry bot",
        sessionManager: new InMemorySessionManager(), // This is where the session will be stored, an alternative implementation can be provided, i.e., file based or database based, or something else.
    }
);

bot.addMenus(menus);

console.log(await bot.process({msisdn:123, prompt:"@bot"}));
console.log(await bot.process({msisdn:123, prompt:"1"}));

Let's now ensure that only the msisdn 123 can use the above bot

'use strict';

const {Bot, InMemorySessionManager} = require('lottus.js');

const menus = [
    {
        name: "welcome",
        title: "Welcome",
        message: "Language | Idioma",
        options: [
            {key:1, label:"English", menu:"english"},
            {key:2, label:"Portugues", menu:"portuguese"},
        ]
    },

    {
        name: "english",
        title: "English",
        message: "This menu is in english",
        options: [
            {key:0, label:"Back", menu:"welcome"}
        ]
    },

    {
        name: "portuguese",
        title: "Portugues",
        message: "Este menu está em portugues",
        options: [
            {key:0, label:"Voltar", menu:"welcome"}
        ]
    }
]

var bot = new Bot(
    {
        name: "bot", 
        entrypoint: 'welcome', 
        keyword: "@bot", 
        inline: false, 
        description: "This is an enquiry bot",
        sessionManager: new InMemorySessionManager(), // This is where the session will be stored, an alternative implementation can be provided, i.e., file based or database based, or something else.
    }
);

bot.addMenus(menus);

bot.intercept('*', async function(request){
    if(request.msisdn.toString() !== '123'){
        const menu = {
            title: "This bot is only available for the msisdn 123",
            final: true
        };
        return {
            menu: menu
        }
    }
});

console.log(await bot.process({msisdn:123, prompt:"@bot"}));

lottus.js's People

Contributors

benchambule avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.