-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (48 loc) · 1.68 KB
/
index.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
"use strict";
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const Birthday = require("./models/birthday");
const restService = express();
const mongoConnectionUrl = "mongodb://localhost:27017/birthdayApp";
restService.use(
bodyParser.urlencoded({
extended: true
})
);
mongoose.connect(mongoConnectionUrl);
restService.use(bodyParser.json());
restService.post("/", function(request, response) {
var speech, displayText, source;
if(request.body.result.metadata.intentName === 'GetBirthdayIntent') {
var givenName = request.body.result.parameters.givenName;
Birthday.findOne({"name" : { $regex : new RegExp(givenName, "i") } }).then(function(data) {
speech = givenName + '\'s birthday is on ' + data.birthDate;
displayText = givenName + '\'s birthday is on ' + data.birthDate;
return response.json({
speech: speech,
displayText: displayText,
source: "webhook-echo-sample"
});
});
}
else if (request.body.result.metadata.intentName === 'SaveBirthdayIntent') {
var givenName = request.body.result.parameters.givenName;
var birthDate = request.body.result.parameters.birthDate;
var person = new Birthday();
person.name = givenName;
person.birthDate = birthDate;
Birthday.create(person).then(function() {
speech = 'Okay, I got that';
displayText = 'Okay, I got that';
return response.json({
speech: speech,
displayText: displayText,
source: "webhook-echo-sample"
});
});
}
});
restService.listen(process.env.PORT || 8080, function() {
console.log("Server up and listening on port");
});