-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
74 lines (59 loc) · 2.42 KB
/
script.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
var http = require('http'),
io = require('socket.io').listen(http),
fs = require('fs'),
five = require('johnny-five'),
express = require('express'),
request = require('request'),
dotenv = require('dotenv');
dotenv.load();
var clientId = 'CLIENT_ID';
var clientSecret = 'CLIENT_SECRET';
var app = express();
// We define the port we want to listen to. Logically this has to be the same port than we specified on ngrok.
const PORT=4390;
app.listen(PORT, function () {
//Callback triggered when server is successfully listening. Hurray!
console.log("Example app listening on port " + PORT);
});
// This route handles GET requests to our root ngrok address and responds with the same "Ngrok is working message" we used before
app.get('/', function(req, res) {
res.send('Ngrok is working! Path Hit: ' + req.url);
});
// This route handles get request to a /oauth endpoint. We'll use this endpoint for handling the logic of the Slack oAuth process behind our app.
app.get('/oauth', function(req, res) {
// When a user authorizes an app, a code query parameter is passed on the oAuth endpoint. If that code is not there, we respond with an error message
if (!req.query.code) {
res.status(500);
res.send({"Error": "Looks like we're not getting code."});
console.log("Looks like we're not getting code.");
} else {
// If it's there...
// We'll do a GET call to Slack's `oauth.access` endpoint, passing our app's client ID, client secret, and the code we just got as query parameters.
request({
url: 'https://slack.com/api/oauth.access', //URL to hit
qs: {code: req.query.code, client_id: clientId, client_secret: clientSecret}, //Query string data
method: 'GET', //Specify the method
}, function (error, response, body) {
if (error) {
console.log(error);
} else {
res.json(body);
}
})
}
});
// Route the endpoint that our slash command will point to and send back a simple response to indicate that ngrok is working
app.post('/command', function(req, res) {
res.send('l\'arrosage démarre!');
board = new five.Board();
board.on("ready", function() {
servo = new five.Servo({
pin: 10,
startAt: 0
});
servo.to(180, 2000);
setTimeout(() => {
servo.to(0, 2000);
}, 3500);
});
});