Example #1
0
        /// <summary>
        /// Automatically setup a session based on
        /// <param name="initialScreenAddress">initial screen address</param>.
        /// </summary>
        /// <param name="initialScreenAddress">Initial Screen address</param>
        /// <returns>USSD response.</returns>
        public async Task <UssdResponse> AutoSetupAsync(string initialScreenAddress)
        {
            try
            {
                switch (UssdRequest.Type)
                {
                case "Initiation":
                    var exists = await ExistsAndTimedOutAsync();

                    if (exists)
                    {
                        await ResumeAsync();
                    }
                    else
                    {
                        await StartAsync(initialScreenAddress);
                    }
                    break;

                case "Response":
                    await ContinueAsync();

                    break;

                case "Release":
                    await EndAsync();

                    break;

                case "Timeout":
                    await TimeoutAsync();

                    break;

                default:
                    return(UssdResponse.Release("Failed to setup session. Check the Type parameter of USSD request."));
                }
                return(await ResponseAsync());
            }
            catch (Exception ex)
            {
                return(UssdResponse.Release(ex.Message));
            }
        }
Example #2
0
        /// <summary>
        /// Return appropriate response for input.
        /// </summary>
        /// <param name="position">Input's position.</param>
        /// <returns></returns>
        public UssdResponse InputResponse(int position)
        {
            var input   = Inputs[position];
            var message = new StringBuilder();

            message.Append(Title + Environment.NewLine);
            if (input.HasOptions)
            {
                message.AppendFormat("Choose {0}:" + Environment.NewLine, input.DisplayName);
                var options = input.Options;
                for (var i = 0; i < options.Count; i++)
                {
                    message.AppendFormat("{0}. {1}" + Environment.NewLine
                                         , i + 1, options[i].DisplayValue);
                }
            }
            else
            {
                message.AppendFormat("Enter {0}:" + Environment.NewLine, input.DisplayName);
            }
            return(UssdResponse.Response(message.ToString()));
        }
Example #3
0
        /// <summary>
        /// Send appropriate response for USSD session.
        /// </summary>
        /// <returns>USSD response</returns>
        public async Task <UssdResponse> ResponseAsync()
        {
            UssdScreen   screen;
            const string noScreenMessage = "Failed to get appropriate response. Please try again.";

            switch (State)
            {
            case UssdRequestTypes.Release:
                return(UssdResponse.Release(String.Format("Thank you for using {0}.", AppName)));

            case UssdRequestTypes.Timeout:
                return(UssdResponse.Release("Session timed out. Try again."));

            case UssdRequestTypes.Initiation:
                if (!UssdScreens.ContainsKey(Screen))
                {
                    return(UssdResponse.Release(noScreenMessage));
                }
                screen = UssdScreens[Screen];
                if (screen.Type == UssdScreenTypes.Input)
                {
                    return(screen.InputResponse(0));
                }
                return(await screen.ResponseAsync(this));

            case UssdRequestTypes.Response:
                if (!UssdScreens.ContainsKey(Screen))
                {
                    return(UssdResponse.Release(noScreenMessage));
                }
                screen = UssdScreens[Screen];
                switch (screen.Type)
                {
                case UssdScreenTypes.Menu:
                    var screenList = Screen.Split(new[] { "." }
                                                  , StringSplitOptions.RemoveEmptyEntries).ToList();
                    if (screenList.Count > 1 && UssdRequest.Message == "0")
                    {
                        screenList.RemoveAt(screenList.Count - 1);
                    }
                    else
                    {
                        screenList.Add(UssdRequest.Message);
                    }
                    var screenAddress = string.Join(".", screenList);
                    if (!UssdScreens.ContainsKey(screenAddress))
                    {
                        return(UssdResponse.Release(noScreenMessage));
                    }
                    screen = UssdScreens[screenAddress];
                    await Redis.HashSetAsync(Mobile, "Screen", screenAddress);

                    Screen = screenAddress;
                    if (screen.Type == UssdScreenTypes.Input)
                    {
                        return(screen.InputResponse(0));
                    }
                    break;

                case UssdScreenTypes.Input:
                    var inputMetaExists = await Redis.KeyExistsAsync(InputMetaHash);

                    if (!inputMetaExists ||
                        Screen != await Redis.HashGetAsync(InputMetaHash, "Screen"))
                    {
                        await ResetInputAsync(screen);
                    }
                    var length   = screen.Inputs.Count;
                    var position = Convert.ToInt32(await Redis.HashGetAsync(InputMetaHash
                                                                            , "Position"));
                    if (position < length - 1)
                    {
                        await ResetInputExpiryAsync();

                        return(await screen.ReceiveInputAndRespondAsync(this, position));
                    }
                    await screen.ReceiveInputAsync(this, position);

                    await screen.PrepareInputDataAsync(this);
                    await DeleteInput();

                    return(await screen.InputProcessorAsync(this, screen.InputData));
                }
                return(await screen.ResponseAsync(this));

            default:
                return(UssdResponse.Release(noScreenMessage));
            }
        }