示例#1
0
        /// <summary>
        /// Render a form (series of inputs).
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        public async Task <UssdResponse> RenderForm(UssdForm form)
        {
            var json = JsonConvert.SerializeObject(form);
            await DataBag.Set(FormProcessorDataKey, json);

            return(Redirect("FormInputDisplay"));
        }
        /// <summary>
        /// Render USSD menu and redirect to appropriate user's choice.
        /// </summary>
        /// <param name="ussdMenu"></param>
        /// <returns></returns>
        public async Task <ProgrammableServiceResponse> RenderMenu(Menu ussdMenu, List <ProgrammableServicesResponseData> dataItems = null, ProgrammableServicesResponseCartData cartItem = null, string label = null, string dataType = null, bool persistAsFavoriteStep = false)
        {
            var json = JsonConvert.SerializeObject(ussdMenu);
            await DataBag.Set(MenuProcessorDataKey, json);

            return(Render(ussdMenu.Render(), dataItems, cartItem, label, dataType, "", "", persistAsFavoriteStep, nameof(MenuProcessor)));
        }
示例#3
0
        /// <summary>
        /// Render USSD menu and redirect to appropriate user's choice.
        /// </summary>
        /// <param name="ussdMenu"></param>
        /// <returns></returns>
        public async Task <UssdResponse> RenderMenu(UssdMenu ussdMenu)
        {
            var json = JsonConvert.SerializeObject(ussdMenu);
            await DataBag.Set(MenuProcessorDataKey, json);

            return(Render(ussdMenu.Render(), "MenuProcessor"));
        }
示例#4
0
        public async Task <ProgrammableServiceResponse> Start()
        {
            _logger.LogDebug("Initial request: {request}", Request);
            try
            {
                if (Request.IsUssd())
                {
                    var header = "Buy my awesome product";

                    var item1 = "Buy for myself";
                    var item2 = "Buy for a different number";

                    var menu = Menu.New(header)
                               .AddItem(item1, $"{nameof(EnterAmountForm)}")
                               .AddItem(item2, $"{nameof(RecipientForm)}");

                    await DataBag.Set("mobileNumber", Request.Mobile);

                    // setup rich ux for web and mobile
                    return(await RenderMenu(menu, new List <ProgrammableServicesResponseData>
                    {
                        new ProgrammableServicesResponseData(item1, "1", decimal.Zero),
                        new ProgrammableServicesResponseData(item2, "2", decimal.Zero),
                    }, null, header, ProgrammableServiceDataTypes.Menu));
                }

                return(Redirect($"{nameof(RecipientForm)}"));
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(await Error());
            }
        }
        /// <summary>
        /// Render a form (series of inputs).
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        public async Task <ProgrammableServiceResponse> RenderForm(Form form, List <ProgrammableServicesResponseData> dataItems = null, ProgrammableServicesResponseCartData cartItem = null, string label = null, string dataType = null, string fieldType = "text", string fieldName = "", bool persistAsFavoriteStep = false)
        {
            var json = JsonConvert.SerializeObject(form);
            await DataBag.Set(FormProcessorDataKey, json);

            //return Redirect("FormInputDisplay");
            return(await RichFormInputDisplay(dataItems, cartItem, label, dataType, fieldType, fieldName, persistAsFavoriteStep));
        }
示例#6
0
        public async Task <ProgrammableServiceResponse> Confirmation()
        {
            try
            {
                var mobileNumber = await DataBag.Get("mobileNumber");

                var amountStr = FormData["amount"]; // you may want to validate amount

                if (!decimal.TryParse(amountStr, out var amount))
                {
                    return(await RenderResponse("Amount is not valid"));
                }

                if (amount <= decimal.Zero)
                {
                    return(await RenderResponse($"Amount is not valid"));
                }


                if (amount < 1)
                {
                    return(await RenderResponse($"Sorry, minimum amount is GHS 1.00"));
                }

                if (amount > 100)
                {
                    return(await RenderResponse($"Sorry, maximum amount is GHS 100.00"));
                }


                await DataBag.Set("amount", amount.ToString());

                var header = $"Confirmation\nService: MTN Airtime for {mobileNumber}\nAmount: {amount}";

                var menu = Menu.New(header)
                           .AddItem("Confirm", $"{nameof(ProcessAddToCart)}")
                           .AddZeroItem("Cancel", $"{nameof(Exit)}");

                var dataItems = new List <ProgrammableServicesResponseData>
                {
                    new ProgrammableServicesResponseData("Confirm", "1")
                    ,
                    new ProgrammableServicesResponseData("Cancel", "0")
                };

                if (Request.IsUssd())
                {
                    return(await RenderMenu(menu, dataItems, null, header, ProgrammableServiceDataTypes.Confirm));
                }

                return(await ProcessAddToCart());
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(await Error());
            }
        }
        /// <summary>
        /// Must be used only for USSD.
        /// </summary>
        /// <param name="collection">Must be valid and not null. Dictionary's key should be the display text.</param>
        /// <param name="paginationOptions"></param>
        /// <returns></returns>
        public async Task <ProgrammableServiceResponse> PaginateForUssd(Dictionary <string, string> collection,
                                                                        PaginationOptions paginationOptions)
        {
            if (collection == null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

            if (paginationOptions == null)
            {
                throw new ArgumentNullException(nameof(paginationOptions));
            }

            if (string.IsNullOrEmpty(paginationOptions.NextPageKey))
            {
                throw new ArgumentNullException(nameof(paginationOptions.NextPageKey));
            }
            if (string.IsNullOrEmpty(paginationOptions.PreviousPageKey))
            {
                throw new ArgumentNullException(nameof(paginationOptions.PreviousPageKey));
            }

            if (paginationOptions.PreviousPageKey.Equals(paginationOptions.NextPageKey))
            {
                throw new Exception($"{nameof(paginationOptions.NextPageKey)} should not be the same as {nameof(paginationOptions.PreviousPageKey)}");
            }

            if (FormData == null)
            {
                FormData = new Dictionary <string, string>();
            }

            if (!Request.IsUssd())
            {
                throw new InvalidOperationException("this method is meant for USSD context only :|");
            }
            await DataBag.Set(PaginationOptionsKey, JsonConvert.SerializeObject(paginationOptions));

            await DataBag.Set(PaginationCollectionKey, JsonConvert.SerializeObject(collection));

            await DataBag.Set(OnPaginationItemSelectedName, OnPaginationItemSelectedValue);

            var form = (await PaginateCollection(collection, paginationOptions))[0];

            form.Data = FormData;

            return(await RenderForm(form));
        }
示例#8
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"));
        }
示例#9
0
        public async Task <ProgrammableServiceResponse> ProcessRecipientForm()
        {
            try
            {
                var mobileNumber = FormData["mobileNumber"];

                if (string.IsNullOrEmpty(mobileNumber)) // check if the input phone number is invalid
                {
                    return(Redirect($"{nameof(RecipientForm)}"));
                }

                await DataBag.Set("mobileNumber", mobileNumber);

                return(Redirect($"{nameof(EnterAmountForm)}"));
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(await Error());
            }
        }
        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)));
        }