Skip to content

Commit

Permalink
fix: input_processor and options_processor
Browse files Browse the repository at this point in the history
  • Loading branch information
benchambule committed Aug 26, 2024
1 parent 0fa8120 commit ae4a385
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 13 deletions.
75 changes: 75 additions & 0 deletions examples/sessions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { lottus, form_processor, InMemorySessionManager, process_w_session, format_message } from "../index.js";

(async function () {
const bot = create_bot();
const session_manager = new InMemorySessionManager();

let input = ["", "1", "0", "2"];

for(const i of input){
const identifier = "841234567";

console.log(await process_request(bot, session_manager, identifier, {prompt: i}));
}

input = ["", "2", "0", "1", "1", "1", "0"];

for(const i of input){
const identifier = "841234568";

console.log(await process_request(bot, session_manager, identifier, {prompt: i}));
}
})();

async function process_request(bot, session_manager, msisdn, request){
try{
const session = await process_w_session(bot, session_manager, msisdn, request);

return format_message(session.message);
}catch(e){
console.log(e);
}

return null;
}

function get_main(req, res){

res.title = "Main";
res.addAutoOption({label: "Home", next: "home"});
res.addAutoOption({label: "About", next: "about"});

return res;
}


function get_about(req, res){

res.title = "About";
res.addOption({label: "Back to main", next:"main", key: 0});
res.body = "You selected about";

return res;
}

function get_home(req, res){

res.title = "Home";
res.addOption({label: "Back to main", next:"main", key: 0});
res.body = "You selected home";

return res;
}


function create_bot(){
const bot = lottus();

bot.get("main", get_main);
bot.post("main", form_processor);

bot.at("home", get_home, form_processor);
bot.at("about", get_about, form_processor);

return bot;
}
60 changes: 47 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export {
process_w_session
}


/**
*
* @param {object} options
Expand All @@ -36,6 +37,9 @@ function lottus(options){
if(options.description){
bot.description = options.description;
}
if(options.bot){
bot.name = options.bot;
}
}

return bot;
Expand All @@ -49,6 +53,7 @@ class __lottus {
debug = false;
entrypoint = "main";
description = "";
name = "__lottus__";

/**
*
Expand Down Expand Up @@ -432,6 +437,7 @@ class Message {
body = "";
footer = "";
title = "";
parameters = {};

constructor(name){
this.name = name;
Expand All @@ -453,7 +459,7 @@ class Message {
}

if(!this.form.input){
this.form = {input: {}}
this.form.input = {};
}

if(options.name){
Expand Down Expand Up @@ -539,7 +545,7 @@ async function options_processor(req, res){
const options = req.form?.options;

if(options){
const option = options.get(req.prompt.toString());
const option = options.get(req.prompt);

if(option){
req.selected_option = option;
Expand All @@ -549,10 +555,18 @@ async function options_processor(req, res){
next = req.selected_option.next;
}

if(!option.parameters){
option.parameters = {};
}

req.parameters = {...req.parameters, ...option.parameters};

return await redirect(next, req);
} else {
res.error = "You selected an invalid option";
}
} else {
res.error = "The form has no input nor options";
}

return res;
Expand All @@ -568,11 +582,29 @@ async function input_processor(req, res){
const input = req.form?.input;

if(input){
req.input = input;
// let next = req.form?.next;
return await redirect(input.next, req);
} else {
res.error = "You selected an invalid option";
const name = input.name;
const value = req.prompt;

if(input.type === "number"){
if(!parseFloat(value)){
res.error = "Please provide a number";

return res;
}
}

let parameter = {}
parameter[name] = value;

req.parameters = {...req.parameters, ...parameter};

let next = input?.next;

return await redirect(next, req);
}

if(!res.error){
res.error = "The form has no input form";
}

return res;
Expand All @@ -599,10 +631,12 @@ async function form_processor(req, res){
next = req.selected_option.next;
}

if(option.parameters){
req.parameters = {...req.parameters, ...option.parameters};
if(!option.parameters){
option.parameters = {};
}

req.parameters = {...req.parameters, ...option.parameters};

return await redirect(next, req);
} else {
res.error = "You selected an invalid option";
Expand Down Expand Up @@ -689,7 +723,7 @@ class InMemorySessionManager {
* @param {string} identifier
* @returns {object}
*/
get_session(identifier){
get(identifier){
return this.#sessions.get(identifier);
}

Expand All @@ -699,7 +733,7 @@ class InMemorySessionManager {
* @param {string} message
* @param {object} parameters
*/
save_session(identifier, message, parameters){
save(identifier, message, parameters){
this.#sessions.set(identifier, {message: message, parameters: parameters});
}

Expand All @@ -726,7 +760,7 @@ class InMemorySessionManager {
* @returns {object}
*/
async function process_w_session(bot, session_manager, identifier, request){
let session = session_manager.get_session(identifier);
let session = session_manager.get(identifier);

if(!session){
session = {message: null, parameters: null};
Expand All @@ -738,7 +772,7 @@ async function process_w_session(bot, session_manager, identifier, request){

session.message = await bot.process(request, session.message);
session.parameters = {...session.parameters, ...session.message.parameters};
session_manager.save_session(identifier, session.message, session.parameters);
session_manager.save(identifier, session.message, session.parameters);

if(session.message.close){
session_manager.close(identifier);
Expand Down

0 comments on commit ae4a385

Please sign in to comment.