Skip to content

Commit

Permalink
Fix: always return session\nUpdate: README.MD
Browse files Browse the repository at this point in the history
  • Loading branch information
benchambule committed Jul 27, 2024
1 parent d40b0c1 commit 65009b6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 23 deletions.
71 changes: 50 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ 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"
description: "Reverses a provided string",
}
);

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

Expand All @@ -37,10 +36,19 @@ reverse.at('main', async function(request, context){
};
});

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"}));
(async () => {
let result = await reverse.process({msisdn: 123, prompt:"@reverse", sentence: "giberish"}, null);
console.log(result.menu);

result = await reverse.process({msisdn: 123, prompt:"@reverse", sentence: "bonjour le monde"});
console.log(result.menu);

result = await reverse.process({msisdn: 123, prompt:"@reverse", sentence: "ola mundo"});
console.log(result.menu);

result = await reverse.process({msisdn: 123, prompt:"@reverse", sentence: "hello world"});
console.log(result.menu);
})();
```

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.
Expand All @@ -55,7 +63,6 @@ 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"
}
);
Expand All @@ -79,9 +86,9 @@ reverse.intercept('main', async (request) => {
}
});

reverse.at('main', async function(request, context){
reverse.at('main', async function(request){
const txt = request.lang == 'pt'? 'Frase invertida' : 'Reversed sentence';
const prompt = request.prompt.replace(context.bot.keyword, "").trim();
const prompt = request.sentence;
const menu = {
title: txt,
text: prompt.split("").reverse().join(""),
Expand All @@ -104,7 +111,7 @@ Let's build another bot, this time it will have 3 menus and the user can navigat
```javascript
'use strict';

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

const menus = [
{
Expand Down Expand Up @@ -141,24 +148,33 @@ 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"}));
(async () => {
let session = await bot.process({msisdn:123, prompt:"@bot"});
console.log(session.menu);

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

session = await bot.process({msisdn:123, prompt:"0"}, session);
console.log(session.menu);

session = await bot.process({msisdn:123, prompt:"2"}, session);
console.log(session.menu);
})();
```

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

```javascript
'use strict';

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

const menus = [
{
Expand Down Expand Up @@ -195,9 +211,7 @@ 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.
}
);

Expand All @@ -215,5 +229,20 @@ bot.intercept('*', async function(request){
}
});

console.log(await bot.process({msisdn:123, prompt:"@bot"}));
(async () => {
let session = await bot.process({msisdn:1234, prompt:"@bot"});
console.log(session.menu);

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

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

session = await bot.process({msisdn:123, prompt:"0"}, session);
console.log(session.menu);

session = await bot.process({msisdn:123, prompt:"2"}, session);
console.log(session.menu);
})();
```
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ async function inner_processor(bot, request, session){
}
}

if(result && result.menu && !result.menu.final){
if(result && result.menu){
if(bot.debug){
console.log("Initializing session");
}
Expand Down Expand Up @@ -371,7 +371,7 @@ async function defaultProcessor(bot, request, session){
}

if(bot.debug){
console.log("Result from processing request:", request, 'is menu:', result.menu.name);
console.log("Result from processing request:", request, 'is menu:', result);
}

return result.session;
Expand Down

0 comments on commit 65009b6

Please sign in to comment.