Skip to content

Commit

Permalink
update: README
Browse files Browse the repository at this point in the history
  • Loading branch information
benchambule committed Aug 25, 2024
1 parent a7d4e6f commit 58e881a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 197 deletions.
276 changes: 83 additions & 193 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,238 +13,128 @@ 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.

```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.

```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);
```
6 changes: 3 additions & 3 deletions examples/options.js
Original file line number Diff line number Diff line change
@@ -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"};
Expand Down Expand Up @@ -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));
console.log(message);
2 changes: 1 addition & 1 deletion examples/reverse_with_params.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down

0 comments on commit 58e881a

Please sign in to comment.