public async Task <ProgrammableServiceResponse> MenuProcessor()
        {
            var json = await DataBag.Get(MenuProcessorDataKey);

            var      menu = JsonConvert.DeserializeObject <Menu>(json);
            MenuItem item;

            try
            {
                var choice = Convert.ToInt16(Request.TrimmedMessage);
                if (choice == 0 && menu.ZeroItem != null)
                {
                    return(Redirect(menu.ZeroItem.Action, menu.ZeroItem.Controller));
                }

                item = menu.Items[choice - 1];
            }
            catch (Exception ex)
            {
                return(Render(string.Format("Menu choice {0} does not exist.",
                                            Request.TrimmedMessage), null, null));
            }

            await DataBag.Delete(MenuProcessorDataKey);

            return(Redirect(item.Action, item.Controller));
        }
예제 #2
0
        public async Task <UssdResponse> FormInputProcessor()
        {
            var form = await GetForm();

            var    input = form.Inputs[form.ProcessingPosition];
            var    key   = input.Name;
            string value = null;

            if (!input.HasOptions)
            {
                value = Request.TrimmedMessage;
            }
            else
            {
                try
                {
                    var choice = Convert.ToInt16(Request.TrimmedMessage);
                    value = input.Options[choice - 1].Value;
                }
                catch (Exception exception)
                {
                    return(Render(string.Format("Option {0} does not exist.",
                                                Request.TrimmedMessage)));
                }
            }
            form.Data.Add(key, value);
            if (form.ProcessingPosition == (form.Inputs.Count - 1))
            {
                await DataBag.Delete(FormProcessorDataKey);

                var jsonData = JsonConvert.SerializeObject(form.Data);
                await DataBag.Set(FormDataKey, jsonData);

                return(Redirect(form.Action, form.Controller));
            }
            ++form.ProcessingPosition;
            var json = JsonConvert.SerializeObject(form);
            await DataBag.Set(FormProcessorDataKey, json);

            return(Redirect("FormInputDisplay"));
        }
        internal async Task <ProgrammableServiceResponse> FormInputProcessor()
        {
            var form = await GetForm();

            var    input = form.Inputs[form.ProcessingPosition];
            var    key   = input.Name;
            string value = null;

            if (!input.HasOptions)
            {
                value = Request.TrimmedMessage;
            }
            else
            {
                try
                {
                    var requestValue = Request.TrimmedMessage;

                    var inputOption = input.Options.FirstOrDefault(i => i.Index == requestValue);

                    if (inputOption == null)

                    {
                        var choice = Convert.ToInt16(requestValue);
                        inputOption = input.Options[choice - 1];


                        if (inputOption == null || !string.IsNullOrEmpty(inputOption.Index))
                        {
                            return(Render($"Option {Request.TrimmedMessage} does not exist.", null, null));
                        }
                    }


                    value = inputOption.Value;
                    if (string.IsNullOrEmpty(value))
                    {
                        return(Render($"Option {Request.TrimmedMessage} does not exist.", null,
                                      null));
                    }
                }
                catch (Exception ex)
                {
                    return(Render($"Option {Request.TrimmedMessage} does not exist.", null, null));
                }
            }


            form.Data[key] = value;
            if (form.ProcessingPosition == (form.Inputs.Count - 1))
            {
                await DataBag.Delete(FormProcessorDataKey);

                var jsonData = JsonConvert.SerializeObject(form.Data);
                await DataBag.Set(FormDataKey, jsonData);

                return(Redirect(form.Action, form.Controller));
            }

            ++form.ProcessingPosition;
            var json = JsonConvert.SerializeObject(form);
            await DataBag.Set(FormProcessorDataKey, json);

            return(Redirect(nameof(FormInputDisplay)));
        }