Created
November 25, 2018 15:19
-
-
Save ekumachidi/5a36594b9233f6be870de5928f5fa60d to your computer and use it in GitHub Desktop.
firebase functions for virtual banker
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
'use strict'; | |
var https = require('https'); | |
const functions = require('firebase-functions'); | |
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => { | |
const action = request.body.result.action; | |
response.setHeader('Content-Type','application/json'); | |
const parameters = request.body.result.parameters; | |
if (action == 'input.getStockPrice') { | |
var companyName = parameters['company_name']; | |
var priceType = parameters['price_type']; | |
var date = parameters['date']; | |
getStockPrice(companyName, priceType, date, response); | |
} | |
if (action == 'input.getAccountBalance') { | |
var accountNumber = parameters['accountNumber']; | |
getAccountBalance(accountNumber, response); | |
} | |
if (action == 'input.getAccountStatement') { | |
var accountNumber = parameters['accountNumber']; | |
var startDate = parameters['startDate']; | |
var endDate = parameters['endDate']; | |
getAccountStatement(accountNumber, startDate, endDate, response); | |
} | |
}); | |
function buildChatResponse(chat){ | |
return JSON.stringify({"speech": chat, "displayText":chat}); | |
} | |
function getStockPrice(companyName, priceType, date, cloudFnResponse) { | |
console.log('In function getStockPrice'); | |
console.log("Company Name: "+companyName); | |
console.log("Price Type: "+priceType); | |
console.log("Date: "+date); | |
var tickerMap = { | |
"apple": "AAPL", | |
"microsoft": "MSFT", | |
"ibm": "IBM", | |
"google": "GOOG", | |
"amazon": "AMZN", | |
"facebook": "FB" | |
}; | |
var priceMap = { | |
"opening": "open_price", | |
"closing": "close_price", | |
"maximum": "high_price", | |
"high": "high_price", | |
"low": "low_price", | |
"minimum": "low_price" | |
}; | |
var stockTicker = tickerMap[companyName.toLowerCase()]; | |
var priceTypeCode = priceMap[priceType.toLowerCase()]; | |
var pathString = "/historical_data?ticker="+ stockTicker +"&item="+ priceTypeCode +"&start_date="+ date +"&end_date="+ date; | |
console.log('Path string: '+ pathString); | |
var username = "3b62edfd026c10f0c0ee2cc8baa7ea8f"; | |
var password = "6bc9a43ec7ede6cec26a7d9ac5f7fb61"; | |
var auth = "Basic "+ new Buffer(username + ":" + password).toString('base64'); | |
var request = https.get({ | |
host: "api.intrinio.com", | |
path: pathString, | |
headers: { | |
"Authorization": auth | |
} | |
}, function (response) { | |
var json = ""; | |
response.on('data', function (chunk) { | |
console.log("Received json response: " + chunk); | |
json += chunk; | |
}); | |
response.on('end',function () { | |
var jsonData = JSON.parse(json); | |
var stockPrice = jsonData.data[0].value; | |
console.log("The stock price received is: " + stockPrice); | |
var chat = "The " + priceType + " price for " +companyName+ " on " +date+ " was " +stockPrice; | |
cloudFnResponse.send(buildChatResponse(chat)); | |
}); | |
}); | |
} | |
function getAccountBalance(accountNumber, cloudFnResponse) { | |
console.log('In function getAccountBalance'); | |
console.log('Account Number: '+ accountNumber); | |
var options = { | |
method: "POST", | |
hostname: "gateway.ubaopenbanking.com", | |
path: "/balanceInquiry/v1.0.0/balanceInquiry", | |
headers: { | |
"Content-Type": "application/json", | |
"Authorization": "Bearer 48138651-d06e-3c18-b110-cf6773ffa31c" | |
} | |
}; | |
console.log(JSON.stringify(options)) | |
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; | |
var req = https.request(options, function (res) { | |
var json = ""; | |
res.on("data", function (chunk) { | |
console.log("Received json response: " + chunk); | |
json += chunk; | |
}); | |
res.on("end", function () { | |
var jsonData = JSON.parse(json); | |
console.log("Recieved reponse:"+jsonData); | |
var accountBalance = parseInt(jsonData["sendTransactionResponse"]["return"]["C24TRANRES"]["AVAILABLE_BALANCE"]); | |
var accountCurrency = jsonData["sendTransactionResponse"]["return"]["C24TRANRES"]["BALANCE_CURRENCY"] | |
console.log("The account balance received is: " + accountBalance); | |
console.log("The account Currency received is: " + accountCurrency); | |
var chat = "Your available balance is "+accountBalance+" "+accountCurrency; | |
cloudFnResponse.send(buildChatResponse(chat)); | |
}); | |
}); | |
req.write(JSON.stringify({ "sendTransaction": | |
{ "arg0": | |
{ "C24TRANREQ": | |
{ "PROCESSING_CODE": '31', | |
"COUNTRY_CODE": 'NGA', | |
"DR_ACCT_NUM": '"'+accountNumber+'"' } } } })); | |
req.end(); | |
} | |
function getAccountStatement(accountNumber, startDate, endDate, cloudFnResponse) { | |
console.log('In function getAccountStatement'); | |
console.log('Account Number: '+ accountNumber); | |
con | |
console.log('start date: '+ startDate); | |
console.log('end date: '+ endDate); | |
var options = { | |
method: "POST", | |
hostname: "gateway.ubaopenbanking.com", | |
path: "/statement/1.0/transactions", | |
headers: { | |
"Content-Type": "application/json", | |
"Authorization": "Bearer a7858a17-e92a-36a1-a5a5-c4f2d146bb66" | |
} | |
}; | |
console.log('Options: '+ JSON.stringify(options)); | |
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; | |
var req = https.request(options, function (res) { | |
var json = ""; | |
res.on("data", function (chunk) { | |
console.log("Received json response: " + chunk); | |
json += chunk; | |
}); | |
res.on("end", function () { | |
var jsonData = JSON.parse(json); | |
console.log("Recieved reponse:"+jsonData); | |
var accountBalance = parseInt(jsonData["sendTransactionResponse"]["return"]["C24TRANRES"]["AVAILABLE_BALANCE"]); | |
console.log("The account balance received is: " + accountBalance); | |
var accountCurrency = jsonData["sendTransactionResponse"]["return"]["C24TRANRES"]["BALANCE_CURRENCY"]; | |
console.log("The account Currency received is: " + accountCurrency); | |
var accountTransactions = jsonData["sendTransactionResponse"]["return"]["C24TRANRES"]["TRANS_INFO"]["TRAN"]; | |
console.log("The account transactions received: " + accountTransactions); | |
var chat = "Your available balance is "+accountBalance+" "+accountCurrency; | |
cloudFnResponse.send(buildChatResponse(chat)); | |
}); | |
}); | |
req.write(JSON.stringify({ "sendTransaction": { | |
"arg0": { | |
"C24TRANREQ": { | |
"PROCESSING_CODE": "93", | |
"TRAN_CRNCY_CODE": "NGN", | |
"COUNTRY_CODE": "NGA", | |
"DR_ACCT_NUM": accountNumber, | |
"STMT_QUERY_PARAMS": { | |
"START_DATE": '"'+startDate.replace(/-/g,'')+'"', | |
"END_DATE": endDate.replace(/-/g,''), | |
"NUM_TRANS": "5" } } } } })); | |
req.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment