-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
54 lines (41 loc) · 1.81 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
var express = require('express');
var request = require('request');
const bodyParser = require('body-parser');
var app = express();
const dotenv = require('dotenv');
dotenv.config();
const accountSid = process.env.SID;
const authToken = process.env.KEY;
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('/incoming', (req, res) => {
const twiml = new MessagingResponse();
if(req.body.Body.toLowerCase().trim()!="hi" && req.body.Body.toLowerCase().trim()!="hello" && req.body.Body.toLowerCase().trim()!="test" && req.body.Body.toLowerCase().trim()!="help"){
request('https://api.duckduckgo.com/?skip_disambig=1&format=json&pretty=1&q='+req.body.Body, function (error, response, body) {
body = JSON.parse(body)
console.log('body:', body["Abstract"]);
if(body["Abstract"] == ""){
body["Abstract"]= body["RelatedTopics"][0]["Text"]
}
var msg = twiml.message(`*`+body["Heading"]+`*
`+body["Abstract"]);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
});
}
else{
var msg = twiml.message(`*Hi there! 👋*
I am a bot with alot of information to help you with and find quick information within WhatsApp.
Exciting send me any thing you want to know and i'll try to find relevant information for you.`)
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);
});