From 58e881a99a61d1f3a98b4af97c7b99e50e7e886f Mon Sep 17 00:00:00 2001 From: Ben Chambule Date: Sun, 25 Aug 2024 13:52:04 +0200 Subject: [PATCH] update: README --- README.md | 276 ++++++++++---------------------- examples/options.js | 6 +- examples/reverse_with_params.js | 2 +- 3 files changed, 87 insertions(+), 197 deletions(-) diff --git a/README.md b/README.md index d5eaf58..13f9efe 100644 --- a/README.md +++ b/README.md @@ -13,42 +13,18 @@ Let's build a bot that reverses a string, and it is triggered by the _@reverse_ ```javascript 'use strict'; -const {Bot} = require('lottus.js'); - -var reverse = new Bot( - { - name: "reverse", - entrypoint: 'main', - keyword: "@reverse", - description: "Reverses a provided string", - } -); - -reverse.at('main', async function(request){ - const sentence = request.sentence; - const message = { - body: '👉 ' + sentence.split("").reverse().join(""), - final: true - } +import { lottus } from "lottus.js"; - return { - message: message - }; -}); +let bot = lottus(); -(async () => { - let result = await reverse.process({msisdn: 123, prompt:"@reverse", sentence: "giberish"}); - console.log(result.message.body); +bot.info("main", async (req, res) => { + res.body = req.sentence.split("").reverse().join(""); - result = await reverse.process({msisdn: 123, prompt:"@reverse", sentence: "bonjour le monde"}); - console.log(result.message.body); + return res; +}); - result = await reverse.process({msisdn: 123, prompt:"@reverse", sentence: "ola mundo"}); - console.log(result.message.body); +console.log(await bot.process({sentence: "hello world"})); - result = await reverse.process({msisdn: 123, prompt:"@reverse", sentence: "hello world"}); - console.log(result.message.body); -})(); ``` 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. @@ -56,56 +32,26 @@ Let's update our previous bot to be respond accordind to the language. We will b ```javascript 'use strict'; -const {Bot} = require('lottus.js'); +import { lottus } from "lottus.js"; -var reverse = new Bot( - { - name: "reverse", - entrypoint: 'main', - keyword: "@reverse", - description: "Reverses a provided string" - } -); - -reverse.intercept('main', async (request) => { - if(!request.lang){ - request.lang = 'en'; - return { - request: request - } - } +let reverse = lottus(); - if(request.lang != 'pt' && request.lang != 'en'){ - const message = { - body: 'lang cannot be ' + "'" + request.lang+ "'" + ". lang should be 'en' or 'pt'", - final: true - } - return { - message: message - }; - } -}); +reverse.info("main", async (req, res) => { + res.body = "Unkonw language"; + const reversed = req.sentence.split("").reverse().join(""); -reverse.at('main', async function(request){ - const txt = request.lang == 'pt'? 'Frase invertida' : 'Reversed sentence'; - const prompt = request.sentence; - const message = { - title: txt, - body: prompt.split("").reverse().join(""), - final: true + if(req.parameters.lang === 'pt'){ + res.body = "Frase invertida: " + reversed + }else if(req.parameters.lang === 'en'){ + res.body = "Reversed: " + reversed } - - return { - message: message - }; + + return res; }); -(async () => { - console.log((await reverse.process({msisdn: 123, prompt: "@reverse", sentence: "giberish"})).message.body); - console.log((await reverse.process({msisdn: 123, prompt:"@reverse", sentence: "bonjour le monde", lang:"fr"})).message.body); - console.log((await reverse.process({msisdn: 123, prompt:"@reverse", sentence: "ola mundo", lang:"pt"})).message.body); - console.log((await reverse.process({msisdn: 123, prompt:"@reverse", sentence: "hello world", lang:"en"})).message.body); -})(); +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. @@ -113,138 +59,82 @@ Let's build another bot, this time it will have 3 messages and the user can navi ```javascript 'use strict'; -const {Bot} = require('lottus.js'); - -const messages = [ - { - name: "welcome", - title: "Welcome", - message: "Language | Idioma", - options: [ - {key:1, label:"English", message:"english"}, - {key:2, label:"Portugues", message:"portuguese"}, - ] - }, - - { - name: "english", - title: "English", - message: "This message is in english", - options: [ - {key:0, label:"Back", message:"welcome"} - ] - }, - - { - name: "portuguese", - title: "Portugues", - message: "Este message está em portugues", - options: [ - {key:0, label:"Voltar", message:"welcome"} - ] +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"); } -] - -var bot = new Bot( - { - name: "bot", - entrypoint: 'welcome', - keyword: "@bot", - description: "This is an enquiry bot", + + if(req.prompt.trim() === "2"){ + msg = redirect("profession"); } -); -bot.addMessages(messages); + return msg; +}); -(async () => { - let session = await bot.process({msisdn:123, prompt:"@bot"}); - console.log(session.message); +options.info("name", async (_, msg) => { + msg.form = {input: "options"}; - session = await bot.process({msisdn:123, prompt:"1"}, session); - console.log(session.message); + msg.body = "You selected 1 - Name"; + msg.body += "\n0 - Back to main"; - session = await bot.process({msisdn:123, prompt:"0"}, session); - console.log(session.message); + return msg; +}); - session = await bot.process({msisdn:123, prompt:"2"}, session); - console.log(session.message); -})(); -``` +options.form("name", async (req, msg) => { + msg.form = {input: "options"}; + msg.body += "\nUnknow option"; -Let's now ensure that only the msisdn 123 can use the above bot + if(req.prompt.trim() === "0"){ + msg = redirect("main"); + } -```javascript -'use strict'; + return msg; +}); -const {Bot} = require('lottus.js'); - -const messages = [ - { - name: "welcome", - title: "Welcome", - message: "Language | Idioma", - options: [ - {key:1, label:"English", message:"english"}, - {key:2, label:"Portugues", message:"portuguese"}, - ] - }, - - { - name: "english", - title: "English", - message: "This message is in english", - options: [ - {key:0, label:"Back", message:"welcome"} - ] - }, - - { - name: "portuguese", - title: "Portugues", - message: "Este message está em portugues", - options: [ - {key:0, label:"Voltar", message:"welcome"} - ] - } -] - -var bot = new Bot( - { - name: "bot", - entrypoint: 'welcome', - keyword: "@bot", - description: "This is an enquiry bot", - } -); - -bot.addMessages(messages); - -bot.intercept('*', async function(request){ - if(request.msisdn.toString() !== '123'){ - const message = { - title: "This bot is only available for the msisdn 123", - final: true - }; - return { - message: message - } - } +options.info("profession", async (_, msg) => { + msg.form = {input: "options"}; + msg.body = "You selected 2 - Profession"; + msg.body += "\n0 - Back to main"; + + return msg; }); -(async () => { - let session = await bot.process({msisdn:1234, prompt:"@bot"}); - console.log(session.message); +options.form("profession", async (req, msg) => { + msg.form = {input: "options"}; + msg.body += "\nUnknow option"; + + if(req.prompt.trim() === "0"){ + msg = redirect("main"); + } + + return msg; +}); - session = await bot.process({msisdn:123, prompt:"@bot"}); - console.log(session.message); +let message = await options.process({prompt:"hello"}); +console.log(message); - session = await bot.process({msisdn:123, prompt:"1"}, session); - console.log(session.message); +message = await options.process({prompt:"1"}, message); +console.log(message); - session = await bot.process({msisdn:123, prompt:"0"}, session); - console.log(session.message); +message = await options.process({prompt:"0"}, message); +console.log(message); - session = await bot.process({msisdn:123, prompt:"2"}, session); - console.log(session.message); -})(); +message = await options.process({prompt:"2"}, message); +console.log(message); ``` \ No newline at end of file diff --git a/examples/options.js b/examples/options.js index 18f2473..dfb3870 100644 --- a/examples/options.js +++ b/examples/options.js @@ -1,6 +1,6 @@ -import { format_message, lottus, redirect } from "../index.js"; +import { lottus, redirect } from "../index.js"; -let options = new lottus(); +let options = lottus(); options.info("main", async (_, msg) => { msg.form = {input: "options"}; @@ -75,4 +75,4 @@ message = await options.process({prompt:"0"}, message); console.log(message); message = await options.process({prompt:"2"}, message); -console.log(format_message(message)); \ No newline at end of file +console.log(message); \ No newline at end of file diff --git a/examples/reverse_with_params.js b/examples/reverse_with_params.js index 0d5ce0d..5a5e903 100644 --- a/examples/reverse_with_params.js +++ b/examples/reverse_with_params.js @@ -1,6 +1,6 @@ import { lottus } from "../index.js"; -let reverse = new lottus(); +let reverse = lottus(); reverse.info("main", async (req, res) => { res.body = "Unkonw language";