diff --git a/examples/sessions.js b/examples/sessions.js new file mode 100644 index 0000000..f495eae --- /dev/null +++ b/examples/sessions.js @@ -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; +} \ No newline at end of file diff --git a/index.js b/index.js index be9cc3c..1221107 100644 --- a/index.js +++ b/index.js @@ -20,6 +20,7 @@ export { process_w_session } + /** * * @param {object} options @@ -36,6 +37,9 @@ function lottus(options){ if(options.description){ bot.description = options.description; } + if(options.bot){ + bot.name = options.bot; + } } return bot; @@ -49,6 +53,7 @@ class __lottus { debug = false; entrypoint = "main"; description = ""; + name = "__lottus__"; /** * @@ -432,6 +437,7 @@ class Message { body = ""; footer = ""; title = ""; + parameters = {}; constructor(name){ this.name = name; @@ -453,7 +459,7 @@ class Message { } if(!this.form.input){ - this.form = {input: {}} + this.form.input = {}; } if(options.name){ @@ -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; @@ -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; @@ -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; @@ -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"; @@ -689,7 +723,7 @@ class InMemorySessionManager { * @param {string} identifier * @returns {object} */ - get_session(identifier){ + get(identifier){ return this.#sessions.get(identifier); } @@ -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}); } @@ -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}; @@ -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);