-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (49 loc) · 1.59 KB
/
index.js
File metadata and controls
60 lines (49 loc) · 1.59 KB
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
var ACCESS_TOKEN = require('./token').token;
const peeps = ['@kurtlogan', '@chrissysemens', '@shaunforde'];
var bodyParser = require('body-parser')
var cors = require('cors')
var express = require('express')
var http = require('http');
var request = require('request');
var app = express()
app.use(bodyParser.json())
app.get('/', (request, response) => {
response.send("Server active!")
})
app.post('/pushbutton', cors(), (req, response) => {
console.log('button pushed:', req.body.value)
const peep = peeps[Math.floor(Math.random() * peeps.length)];
request('http://api.adviceslip.com/advice', function(error, response, body) {
if (error) {
//error handling here
}
console.log(ACCESS_TOKEN)
var quoteResponse = JSON.parse(response.body);
var quoteText = peep + ', ' + quoteResponse.slip.advice;
const options = {
method: 'POST',
uri: 'https://slack.com/api/chat.postMessage',
form: {
token: ACCESS_TOKEN,
channel: 'general',
text: quoteText,
as_user: false,
link_names: true
},
json: true,
headers: {
'content-type': 'application/x-www-form-urlencoded'
}
}
request(options,
function(error, response, body) {
if (error) {
//error handling
}
})
})
response.send("Message posted!")
})
app.listen(3000, () => {
console.log("Server listening on port 3000!")
})