private static string ForwardRequest(ussdmessage ussdmessage) { UssdRequest ussdRequest = new UssdRequest(); ussdRequest.msisdn = ussdmessage.MOBILE_NUMBER; ussdRequest.sessionid = ussdmessage.SESSION_ID; ussdRequest.shortcode = ussdmessage.SERVICE_KEY; ussdRequest.inputparams = ussdmessage.USSD_BODY; string returnText = string.Empty; try { WriteMessageLog("Forwarding ussd message " + JsonConvert.SerializeObject(ussdRequest) + "\n Endpoint " + Program.menuEndPoint); HttpWebRequest ussdAPIRequest = (HttpWebRequest)WebRequest.Create(Program.menuEndPoint); // edit and put an endpoint ussdAPIRequest.Method = "POST"; ussdAPIRequest.ContentType = "application/json"; byte[] requestBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(ussdRequest)); ussdAPIRequest.ContentLength = requestBytes.Length; Stream requestStream = ussdAPIRequest.GetRequestStream(); requestStream.Write(requestBytes, 0, requestBytes.Length); requestStream.Close(); WebResponse response = ussdAPIRequest.GetResponse(); string statusDescription = ((HttpWebResponse)response).StatusDescription; requestStream = response.GetResponseStream(); StreamReader streamReader2 = new StreamReader(requestStream); string ussdResultString = streamReader2.ReadToEnd(); WriteMessageLog("Response from menu server: " + ussdResultString); return(ussdResultString); } catch (Exception se) { WriteMessageLog("An error forwarding airtel request: " + se.Message); return("ENDSomething went wrong"); } }
public virtual void RequestReceived(object sender, MessageReceivedEventArgs e) { _logger.LogInfo("USSD Request Receiver $$$$$"); HttpResponse httpResponse = new HttpResponse(); if (UssdRequestReceived != null) { HttpRequest httpReq = (HttpRequest)e.Request; if (httpReq.Body == null) { _logger.LogInfo("Request body invalid or missing!!!"); httpResponse.Body = "ERROR"; } else { _logger.LogInfo("smsMessage " + httpReq.Body); // _logger.LogInfo("MessageReceivedEventArgs", new { e }); Dictionary <string, string> requestParameters = getRequestParameters(httpReq.Body); requestParameters.TryGetValue("sourceNumber", out string sourceNumber); requestParameters.TryGetValue("destinationNumber", out string destNumber); requestParameters.TryGetValue("content", out string content); requestParameters.TryGetValue("eventTime", out string eventTime); _logger.LogInfo("message details ", new { sourceNumber, destNumber, content, eventTime }); UssdRequest request = new UssdRequest(sourceNumber ?? string.Empty, destNumber ?? string.Empty, content ?? string.Empty, "en", "1211"); //request.Parameters["localEventTime"] = eventTime; UssdRequestReceivedEventArgs ussdRequestReceivedEventArgs = new UssdRequestReceivedEventArgs(request); UssdRequestReceived(this, ussdRequestReceivedEventArgs); _logger.LogInfo("ussdRequestReceivedEventArgs " + ussdRequestReceivedEventArgs.Response.ToString()); string responseMessageBody = // "<ussdResponse sourceNumber=\"155\" destinationNumber=\""+ destNumber + "\" content=\""+ // ussdRequestReceivedEventArgs.Response.ResponseContent.ToString()+ "\" isNISession=\"false\" closeSession=\"true\" />"; new XDocument( new XElement("ussdResponse", new XAttribute("sourceNumber", request.SourceNumber), new XAttribute("destinationNumber", request.DestinationNumber), new XAttribute("isNISession", "true"), new XAttribute("closeSession", "true"), new XAttribute("content", ussdRequestReceivedEventArgs.Response.ResponseContent))).ToString(); httpResponse.Body = responseMessageBody; } e.Response = httpResponse; return; } throw new InvalidOperationException("Please try again later."); }
private async Task <IActionResult> ProcessFinalState(UssdRequest request, string message, string option) { var inputs = request.USERDATA.Split(" "); foreach (var input in inputs) { if (!int.TryParse(input, out int result)) { return(Ok(new UssdResponse { USERID = userid, MSISDN = request.MSISDN, MSG = "Input value is not in the rigth format", MSGTYPE = true })); } if (result > 90 || result < 1) { return(Ok(new UssdResponse { USERID = userid, MSISDN = request.MSISDN, MSG = "Entere value between 1 - 90", MSGTYPE = true })); } } PreviousState.TryRemove(request.MSISDN, out UserState tt); var transaction = new UssdTransaction { Amount = 200, WinningAmount = 0, OptionName = option, OptionValue = request.USERDATA, PhoneNumber = request.MSISDN }; _context.Add(transaction); await _context.SaveChangesAsync(); var contact = new string(request.MSISDN.ToCharArray().Where(c => char.IsDigit(c)).ToArray()); var _message = $"{message}:{transaction.Id}"; var endPoint = $"https://apps.mnotify.net/smsapi?key=TOdkRPCFwgfCbusbKpMqyYnSn&to={contact}&msg={_message}&sender_id=VAG-OBIRI"; using HttpClient client = new HttpClient(); var response = await client.GetAsync(endPoint); return(Ok(new UssdResponse { USERID = userid, MSISDN = request.MSISDN, MSG = "Success", MSGTYPE = false })); }
public async Task <IHttpActionResult> Index(UssdRequest request) { var redisConnection = await ConnectionManager.Instance.GetRedisDbConnectionLayer().RedisConnection(); if (!redisConnection.IsConnected) { await redisConnection.ConfigureAsync(); } //intentionally skipped mongodb logging return(Ok(await Ussd.Process(new RedisStore(redisConnection), request, "Main", "Start", null, null))); }
private IActionResult ProcessMenu(UssdRequest request, string message) { var _state = new UserState { CurrentState = "", PreviousData = request.USERDATA }; PreviousState.TryAdd(request.MSISDN, _state); return(Ok(new UssdResponse { USERID = userid, MSISDN = request.MSISDN, MSG = message, MSGTYPE = true })); }
public IActionResult TNMDirect(string ussdrequest) { if (string.IsNullOrEmpty(ussdrequest)) { string resp = "ENDInvalid parameters passed"; return(Ok(resp)); } if (!ussdrequest.Contains('&')) { string resp = "ENDInvalid parameters passed"; return(Ok(resp)); } string[] UssdData = ussdrequest.Split('&'); UssdRequest ussdRequest = new UssdRequest(); if (UssdData.Length < 4) { string resp = "ENDInvalid parameters passed"; return(Ok(resp)); } else if (UssdData.Length < 6) { ussdRequest.sessionid = UssdData[0]; ussdRequest.msisdn = UssdData[1]; ussdRequest.shortcode = UssdData[2]; ussdRequest.inputparams = UssdData[3]; } else { ussdRequest.sessionid = UssdData[0]; ussdRequest.msisdn = UssdData[1]; ussdRequest.shortcode = UssdData[2]; ussdRequest.inputparams = UssdData[3]; ussdRequest.imsi = UssdData[4]; ussdRequest.longtitude = UssdData[5]; ussdRequest.latitude = UssdData[6]; } ussdRequest.sessionid = UssdData[0]; ussdRequest.msisdn = UssdData[1]; ussdRequest.shortcode = UssdData[2]; ussdRequest.inputparams = UssdData[3]; ussdRequest.imsi = UssdData[4]; ussdRequest.longtitude = UssdData[5]; ussdRequest.latitude = UssdData[6]; try { HttpWebRequest ussdAPIRequest = (HttpWebRequest)WebRequest.Create(Program.menuEndPoint); // edit and put an endpoint ussdAPIRequest.Method = "POST"; ussdAPIRequest.ContentType = "application/json"; byte[] requestBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(ussdRequest)); ussdAPIRequest.ContentLength = requestBytes.Length; Stream requestStream = ussdAPIRequest.GetRequestStream(); requestStream.Write(requestBytes, 0, requestBytes.Length); requestStream.Close(); WebResponse response = ussdAPIRequest.GetResponse(); string statusDescription = ((HttpWebResponse)response).StatusDescription; requestStream = response.GetResponseStream(); StreamReader streamReader2 = new StreamReader(requestStream); string ussdResultString = streamReader2.ReadToEnd(); return(Ok(ussdResultString)); } catch (Exception ss) { WriteMessageLog("An error occured: " + ss.Message); return(Ok("ENDSomething went wrong...")); } }
public async Task <IHttpActionResult> Index(UssdRequest request) { //todo: properly configure redis before proceeding.... return(Ok(await Ussd.Process(new RedisStore(), request, "Main", "Start"))); }
public async Task <IActionResult> Index([FromBody] UssdRequest request) { var duration = await _context.UssdLock.FirstOrDefaultAsync(); if (DateTime.Now.Hour >= duration.EndTime) { return(Ok(new UssdResponse { USERID = userid, MSISDN = request.MSISDN, MSG = "Sorry Draw Has Ended", MSGTYPE = false })); } if (DateTime.Now.Hour < duration.StartTime) { return(Ok(new UssdResponse { USERID = userid, MSISDN = request.MSISDN, MSG = "Sorry Draw Has Ended", MSGTYPE = false })); } if (PreviousState.TryGetValue(request.MSISDN, out state)) { PreviousState.TryRemove(request.MSISDN, out UserState tt); var _state = tt with { CurrentState = tt.CurrentState + "1" }; PreviousState.TryAdd(request.MSISDN, _state); PreviousState.TryGetValue(request.MSISDN, out state); } if ((string.IsNullOrWhiteSpace(request?.USERDATA) || request?.USERDATA == "*920*79") && state is null) { var day = DaysOfTheWeek[DateTime.Now.DayOfWeek.ToString()]; Options.TryGetValue(day, out string option); var message = $"{option}\n1.Direct-1\n2.Direct-2\n3.Direct-3\n4.Direct-4\n5.Direct-5\n6.Perm 2\n7.Perm-3\n"; return(ProcessMenu(request, message)); } else if (state?.CurrentState?.Length >= 2) { var mainMenuItem = DaysOfTheWeek[DateTime.Now.DayOfWeek.ToString()]; Options.TryGetValue(mainMenuItem, out string optionName); var m = GetFinalStates(state.PreviousData, optionName, request.USERDATA); return(await ProcessFinalState(request, m.Message, m.Option)); } else if (!string.IsNullOrWhiteSpace(request?.USERDATA) && !string.IsNullOrWhiteSpace(state?.CurrentState)) { //var mainMenuItem = state.CurrentState.AsSpan().Slice(0, 1).ToString(); var day = DaysOfTheWeek[DateTime.Now.DayOfWeek.ToString()]; Options.TryGetValue(day, out string option); if (!int.TryParse(request.USERDATA, out int result)) { var _state = new UserState { CurrentState = "", PreviousData = request.USERDATA }; PreviousState.TryRemove(request.MSISDN, out UserState tt); PreviousState.TryAdd(request.MSISDN, _state); return(Ok(new UssdResponse { USERID = userid, MSISDN = request.MSISDN, MSG = "Input value is not in the rigth format", MSGTYPE = true })); } if (result > 7 || result < 1) { var _state = new UserState { CurrentState = "", PreviousData = request.USERDATA }; PreviousState.TryRemove(request.MSISDN, out UserState tt); PreviousState.TryAdd(request.MSISDN, _state); return(Ok(new UssdResponse { USERID = userid, MSISDN = request.MSISDN, MSG = "Entere value between 1 - 7", MSGTYPE = true })); } //previousData+Userdata+CurrentState var key = request.USERDATA + state.CurrentState; var message = GetSubmenus(key, option, request.USERDATA); return(ProcessSubMenu(request, message)); } return(Ok(new UssdResponse { USERID = userid, MSISDN = request.MSISDN, MSG = "Welcome, VAG-OBIRI Lotteries:\n 1)Pioneer\n 2)Vag East\n3)Vag East\n4)African Lotto\n5)Obiri Special\n6)Old Soldier\n7)Sunday-Special", MSGTYPE = true })); }
public async Task <IHttpActionResult> Index(UssdRequest request) { return(Ok(await Ussd.Process(new RedisStore(), request, "Main", "Start", null, new MongoDbLoggingStore("mongodb://localhost", "demoussd")))); }
public async Task <IHttpActionResult> Index(UssdRequest ussdRequest) { var session = new Session(ref _setup, ref ussdRequest); return(Ok(await session.AutoSetupAsync())); }
public async Task<IHttpActionResult> Index(UssdRequest request) { return Ok(await Ussd.Process(new RedisStore(), request, "Main", "Start", null, new MongoDbLoggingStore("mongodb://localhost", "demoussd"))); }
public async Task<IHttpActionResult> Index(UssdRequest ussdRequest) { var session = new Session(ref _setup, ref ussdRequest); return Ok(await session.AutoSetupAsync()); }
public async Task <IHttpActionResult> Index(UssdRequest request) { return(Ok(await Ussd.Process(new RedisStore(), request, "Main", "Menu"))); }