Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/benchambule/lottus.js
https://github.com/benchambule/lottus.js
bot bot-api bot-framework bot-js botjs node-bot nodebot nodebots nodejs ussd-applications ussd-menu whatsapp-web whatsapp-web-js whatsappbot wwebjs
Last synced: 25 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/benchambule/lottus.js
- Owner: benchambule
- License: apache-2.0
- Created: 2024-02-18T21:27:01.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-09-29T16:05:38.000Z (about 1 month ago)
- Last Synced: 2024-09-30T07:40:56.686Z (about 1 month ago)
- Topics: bot, bot-api, bot-framework, bot-js, botjs, node-bot, nodebot, nodebots, nodejs, ussd-applications, ussd-menu, whatsapp-web, whatsapp-web-js, whatsappbot, wwebjs
- Language: JavaScript
- Homepage:
- Size: 172 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lottus.js
=====================### Installation
```bash
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```javascript
'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.
```javascript
'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.
```javascript
'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
```javascript
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...