Skip to content

Instantly share code, notes, and snippets.

@JaniKibichi
Last active October 5, 2024 12:31
Show Gist options
  • Save JaniKibichi/a6939a64e9cdb9f761f6a24526d69e5e to your computer and use it in GitHub Desktop.
Save JaniKibichi/a6939a64e9cdb9f761f6a24526d69e5e to your computer and use it in GitHub Desktop.
USSD on c#
/*
A few things to note about USSD:
USSD is session driven. Every request we send you will contain a sessionId, and this will be maintained until that session is completed
You will need to let the Mobile Service Provider know whether the session is complete or not. If the session is ongoing, please begin your response with CON. If this is the last response for that session, begin your response with END.
If we get a HTTP error response (Code 40X) from your script, or a malformed response (does not begin with CON or END, we will terminate the USSD session gracefully.
using visual studio 2013 or Greater
go to file > new > project
in the projects window choose web under C#
select ASP.NET Web Application and name your project as you like
click next
in the template window select Empty and ensure the web api checkbox is selected and click ok
after your project has been set up, locate the Controllers folder in your solution explorer
right click on the Controllers folder, select Add > Controller
in the Add Scaffold window, select Web API 2 Controller - Empty and click add
Give it a name, we will call it MobileController
paste this code in the MobileController.cs file.
*/
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace Controllers
{
// you need to explicitly declare the parameters that the server will send to your app
// the server returns phoneNumber, text, sessionId and serviceCode
// this class acts as complex type to facilitate parameter binding
// make getters and setters for the values you expect from the server
public class ServerResponse
{
public string text { get; set; }
public string phoneNumber { get; set; }
public string sessionId { get; set; }
public string serviceCode { get; set; }
}
[RoutePrefix("api/mobile")]
public class MobileController : ApiController
{
[Route("ussd")]
// specify the actual route, your url will look like... localhost:8080/api/mobile/ussd...
[HttpPost, ActionName("ussd")]
// state that the method you intend to create is a POST
public HttpResponseMessage ussd([FromBody]ServerResponse ServerResponse)
{
// declare a complex type as input parameter
HttpResponseMessage rs;
string response;
if (ServerResponse.text == null) {
ServerResponse.text = "";
}
// loop through the server's text value to determine the next cause of action
if (ServerResponse.text.Equals("", StringComparison.Ordinal)) {
// always include a 'CON' in your first statements
response = "CON This is AfricasTalking \n"
response+= "1. Get your phone number";
}else if (ServerResponse.text.Equals("1", StringComparison.Ordinal)) {
response= "END Your phone number is "+ServerResponse.phoneNumber;
//the last response starts with an 'END' so that the server understands that it's the final response
} else {
response = "END invalid option";
}
rs = Request.CreateResponse(HttpStatusCode.Created, response);
// append your response to the HttpResponseMessage and set content type to text/plain, exactly what the server expects
rs.Content = new StringContent(response, Encoding.UTF8, "text/plain");
// finally return your response to the server
return rs;
}
}
}
/*Build and run your project.
When a user dials your ussd code, the AfricasTalking server will send a post request to your registered callback url. Use any localhost tunneling tool like ‘ngrok’.
*/
@Stein02
Copy link

Stein02 commented May 24, 2022

How can you test this in postman, plz help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment