@makeavish/

wikibot-start

Express

Start Building WikiBot!

fork
loading
Files
  • index.js
  • views
  • package-lock.json
  • package.json
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
var express = require('express');
var app = express();
var request = require('request');
const bodyParser = require('body-parser');


const accountSid = "AC8a148964a343f627fa6a8c467cb0ece6";
const authToken = "82fc5cc175b50c23b7f5b3ea689b7173";

const client = require('twilio')(accountSid, authToken);
const MessagingResponse = require('twilio').twiml.MessagingResponse;

app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));


app.post('/', (req, res) => {
	const twiml = new MessagingResponse();
	var base = 'https://api.duckduckgo.com/?skip_disambig=1&format=json&pretty=1&q=';
	var query = req.body.Body;
	
	request(base + query, function (error, response, body) {
	    body = JSON.parse(body)  
  
	    if(body["Abstract"] == ""){
		    body["Abstract"]= body["RelatedTopics"][0]["Text"]
		  }   

	    var msg = twiml.message(body["Heading"]+"\n\n"+body["Abstract"]);
			res.writeHead(200, {'Content-Type': 'text/xml'});
		  res.end(twiml.toString());
	  });

});

app.get('/', function(request, response) {
  response.sendFile(__dirname + '/views/index.html');
});


var listener = app.listen(process.env.PORT, function() {
  console.log('Your app is listening on port ' + listener.address().port);
});