Skip to content

Instantly share code, notes, and snippets.

@thedumbtechguy
Created January 30, 2019 10:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thedumbtechguy/43485b3aec840d7296afd406ef31d42e to your computer and use it in GitHub Desktop.
Save thedumbtechguy/43485b3aec840d7296afd406ef31d42e to your computer and use it in GitHub Desktop.
A simple USSD simulator using fetch, prompt and alert. Can be easily adapted to any spec.
<input type="text" id="phone" placeholder="0200672215" />
<button class="btn" id="sendBtn">Send</button>
<script>
var input = document.getElementById('phone');
var makeRequest = function(messageType, sessionId, message) {
fetch("/ussd", {
method: "POST",
headers: {
},
body: JSON.stringify({
messageType: messageType,
sessionId: sessionId,
msisdn: input.value,
message: message
}),
})
.then(response => response.json())
.then(response => {
if(response.responseType == "continue") {
var msg = prompt(response.response);
makeRequest(msg !== null ? "continue" : "end", sessionId, msg);
}
else {
alert(response.response);
}
})
.catch(err => alert("Something went wrong"));
}
function makeid(len) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < len; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
var btn = document.getElementById('sendBtn');
btn.onclick = function() {
var sessionId = makeid(10);
makeRequest("begin", sessionId);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment