Created
January 30, 2019 10:42
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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