This repository has been archived by the owner on Oct 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
84 lines (71 loc) · 2.57 KB
/
server.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
var restify = require('restify');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/hackathon')
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'))
db.once('open', function() {
console.log('Connection established !')
})
var server = restify.createServer()
server.use(restify.bodyParser({ mapParams: false }))
var fakenewsSchema = mongoose.Schema({
speech: String,
link: String,
status: String,
date: Date,
meta: [],
source: String,
score: Number
})
var Fakenews = mongoose.model('Fakenews', fakenewsSchema)
server.post('/fakenews', function create(req, res, next) {
Fakenews.findOne({ 'link': req.body.result.parameters.url }, function (err, fakenews) {
if (err) return handleError(err)
if (fakenews !== null) {
fakenews.score = fakenews.score + 1
fakenews.save(function (err, updateNews) {
if (err) return handleError(err)
res.send(200, `{
"speech": "Barack Hussein Obama II is the 44th and current President of the United States.",
"displayText": "Barack Hussein Obama II is the 44th and current President of the United States, and the first African American to hold the office. Born in Honolulu, Hawaii, Obama is a graduate of Columbia University and Harvard Law School, where ",
"data": {...},
"contextOut": [...],
"source": "DuckDuckGo"
}`)
return next()
})
} else {
var news = new Fakenews({
speech: req.body.result.speech,
link: req.body.result.parameters.url,
status: 'new',
date: req.body.timestamp,
meta: [],
source: null,
score: 0
})
news.save(function (err, news) {
if (err) return console.error(err)
var resBody = {
speech: "Nous n'navons pas d'informations sur ce sujet, nos journalistes vont traiter votre demande",
displayText: "myMessage",
source: "webhook"
}
res.contentType = 'json'
res.send(200, resBody)
return next()
})
}
});
/*var resBody = `{
"speech": "Barack Hussein Obama II is the 44th and current President of the United States.",
"displayText": "Barack Hussein Obama II is the 44th and current President of the United States, and the first African American to hold the office. Born in Honolulu, Hawaii, Obama is a graduate of Columbia University and Harvard Law School, where ",
"data": {...},
"contextOut": [...],
"source": "DuckDuckGo"
}`)
return next()*/
})
server.listen(8080, function() {
console.log('%s listening at %s', server.name, server.url);
});