-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
84 lines (81 loc) · 3.75 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// dependencies
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const http = require('https');
var unirest = require("unirest");
let errorResposne = {
results: []
};
var port = process.env.PORT || 8080;
// create serve and configure it.
const server = express();
server.use(bodyParser.json());
server.post('/webhook', function (req, res) {
if(req.body.queryResult.intent.displayName == "LihatNilaiAkademik") {
if(req.body.queryResult.action == "LihatNilaiAkademik.LihatNilaiAkademik-custom" && req.body.queryResult.parameters.nim != null
&& req.body.queryResult.parameters.semester != null) {
var request = unirest("GET", " https://sister.yudharta.ac.id/rest/hasil_studi/nilai");
request.headers({
"SISTER_API_KEY": "1DB01956C3FDE2B6FB39AA275E22F1"
});
request.query({
"nim": req.body.queryResult.parameters.nim,
"smt": req.body.queryResult.parameters.semester
});
request.send("{}");
request.end(function(response) {
if(response.error) {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({
"fulfillmentText" : "Maaf ",
}));
} else if(response.body.data.length > 0) {
let result = response.body.data;
let output = '';
for(let i = 0; i<result.length;i++) {
output += i+1 + ". " + result[i].mk_nama + " : " + result[i].nilai_huruf;
output+="\n"
}
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({
"fulfillmentText" : output,
}));
}
});
}
} else if(req.body.queryResult.intent.displayName == "LihatKalenderAkademik") {
if(req.body.queryResult.action == "LihatKalenderAkademik" && req.body.queryResult.parameters.tahun != null) {
var request = unirest("GET", "https://sister.yudharta.ac.id/rest/kalender/index");
request.headers({
"SISTER_API_KEY": "1DB01956C3FDE2B6FB39AA275E22F1B2"
});
request.query({
"id_smtr": req.body.queryResult.parameters.tahun
});
request.send("{}");
request.end(function(response) {
if(response.error) {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({
"fulfillmentText" : "Error. Can you try it again ? ",
}));
} else if(response.body.data.length > 0) {
let result = response.body.data;
let output = '';
for(let i = 0; i<result.length;i++) {
output += i+1 + ". " + result[i];
output+="\n"
}
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({
"fulfillmentText" : output,
}));
}
});
}
}
});
server.listen(port, function () {
console.log("Server is up and running...");
});