-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.js.old
More file actions
94 lines (88 loc) · 2.83 KB
/
index.js.old
File metadata and controls
94 lines (88 loc) · 2.83 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// dependencies
var url = require('url');
// express
var _ = require('lomath');
var express = require('express');
var app = express();
// express middlewares
var morgan = require('morgan');
var ejs = require('ejs');
var bodyParser = require('body-parser');
var multer = require('multer');
// config
var config = require('./lib/config.js');
// telegram bot
var bot = require('./lib/bot.js');
bot.listenUpdates();
// engine to render HTML
app.engine('.html', ejs.__express);
app.set('view engine', 'html');
// set the port number
app.set('port', process.env.PORT || 9443);
// Mount middlewares defaulted for root:
// log all HTTP calls for debugging
app.use(morgan('combined'));
// use resources for html: HTML, JS and CSS etc.
app.use(express.static(__dirname + '/views'));
// parse incoming formData into JSON
app.use(bodyParser.json());
// oAuth client initialization
var oauth2 = require('simple-oauth2')({
clientID: config.connectApi.clientId,
clientSecret: config.connectApi.clientSecret,
site: 'https://connect.spotware.com',
tokenPath: '/oauth/v2/token',
authorizationPath: '/oauth/v2/auth'
});
// Initial page redirecting to Github
app.get('/auth', function (req, res) {
// Authorization uri definition
var authorizationUri = oauth2.authCode.authorizeURL({
redirect_uri: url.resolve(config.bot.webHook.url, '/callback'),
access_type: 'online',
approval_prompt: 'auto',
scope: 'trading',
state: req.query.state
});
res.redirect(authorizationUri);
});
// Callback service parsing the authorization token and asking for the access token
app.get('/callback', function (req, res) {
var code = req.query.code;
var state = req.query.state;
if (code && state) {
oauth2.authCode.getToken({
code: code,
redirect_uri: url.resolve(config.bot.webHook.url, '/callback')
}, saveToken);
function saveToken(error, result) {
if (error) {
console.log('Access Token Error', error.message);
}
token = oauth2.accessToken.create(result);
bot.saveToken(token, state);
res.redirect('https://telegram.me/cTraderBot');
}
} else {
res.render('index');
}
});
// route: concise way to group all HTTP methods for a path
app.route('/')
.get(function(req, res) {
// console.log("you GET")
res.render('index')
})
.post(function(req, res) {
// send back to end req-res cycle
res.json('okay, received\n');
// robot handle as middleware for POST
//cTraderBot.handle(req, res)
})
.put(function(req, res) {
res.send("you just called PUT\n")
})
// finally, listen to the specific port for any calls
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});