/*
* Telekom Tropo Call Sample
*/
// This file is special to me, make your own ;-)
var secrets=require("./telekom-tropo-app-secret.js");
require("tropo-webapi");
var express = require("express");
var app = express();
var https = require("https");
var secretAccessCode;
//This triggers Telekom Tropo to call back
function triggerTelekomTropo() {
https.get("https://tropo.developergarden.com/api/sessions?action=create&token="+secrets.telekom_tropo_token_outboundVoice,
function(res) {
console.log("Session creation requested...");
}).on("error", function(err) { console.log("Got error: " + e.message); });
};
// Use this to access elements of http body
app.configure(function() {
app.use(express.bodyParser());
});
//This is were Telekom Tropo gets the secret message
app.post("/",function(req, res) {
var tropo = new TropoWebAPI();
tropo.call(secrets.landlineNumber);
tropo.say("The secret access code is " + String(secretAccessCode));
res.end(TropoJSON(tropo));
console.log("Message send...");
});
// Entry form - triggers authentication process
app.get("/two_way_authenticate", function(req, res) {
var body='<html><head><meta http_equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>';
body += '<form action="/input_code" method="get">'+
'If you want to get access code, please press the button.' +
'<input type="submit" value="Send access code"/>' +
'</form></body></html>';
res.status(200);
res.set({"Content-Type": "text/html"});
res.write(body);
res.end();
});
// Form to send / enter secret code
app.get("/input_code", function(req, res) {
secretAccessCode = Math.floor(Math.random()*800)+100;
triggerTelekomTropo();
var body='<html><head><meta http_equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>';
body += '<form action="/check_code" method="post">'+
'Please enter code.' +
'<textarea name="code"></textarea>' +
'<input type="submit" value="Check access code"/>' +
'</form></body></html>';
res.status(200);
res.set({"Content-Type": "text/html"});
res.write(body);
res.end();
});
//Form that checks the secret code and grants access
app.post("/check_code", function(req,res) {
var body='<html><head><meta http_equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>';
if(Number(req.param('code', null)) == secretAccessCode)
body+="Access granted";
else
body+="Access denied";
body+='</body></html>';
res.status(200);
res.set({"Content-Type": "text/html"});
res.write(body);
res.end();
});
app.listen(8000);
console.log("Server started / listing on port 8000");