diff --git a/.gitignore b/.gitignore index 1bf4259af7a73274c7c11d281143be9c2675a873..acd849fdaffa777220bdf5ff2ee25d626e1e6d5d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ config.js +mongoose.js \ No newline at end of file diff --git a/auth.html b/auth.html new file mode 100644 index 0000000000000000000000000000000000000000..fc6912860a5febd34ba66b3e466c69d34051fdba --- /dev/null +++ b/auth.html @@ -0,0 +1,38 @@ +<!DOCTYPE html> +<html> +<title>Happy Botday !</title> +<meta charset="UTF-8"> +<meta name="viewport" content="width=device-width, initial-scale=1"> +<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> +<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway"> +<style> + body, + h1 { + font-family: "Raleway", sans-serif + } + + body, + html { + height: 100% + } + + .bgimg { + background-image: url('/background.jpg'); + min-height: 100%; + background-position: center; + background-size: cover; + } +</style> + +<body> + + <div class="bgimg w3-display-container w3-animate-opacity w3-text-white"> + <div class="w3-display-middle"> + <p class="w3-large w3-center">Connecté ! Tu peux fermer cette fenêtre. + </p> + </div> + </div> + +</body> + +</html> \ No newline at end of file diff --git a/index.html b/index.html index 326714c592c6335c8b04f3496adb94ecc79328a1..087cba5838042eb4ae24b31e9337c95d8ebff156 100644 --- a/index.html +++ b/index.html @@ -30,7 +30,7 @@ <div class="w3-display-middle"> <h1 class="w3-jumbo w3-animate-top">HAPPY BOTDAY!</h1> <hr class="w3-border-grey" style="margin:auto;width:40%"> - <p class="w3-large w3-center">Go to <a href="https://t.me/happyCSbot">t.me/happyCSbot</a> for configuration. + <p class="w3-large w3-center">Va sur <a href="https://t.me/happyCSbot">t.me/happyCSbot</a> pour la configuration. </p> </div> </div> diff --git a/index.js b/index.js index 26127d3bfaa445332d8ca4fd389125aaac461272..b6bd6c3a8bacd58332d12c860282a53c89d00a88 100644 --- a/index.js +++ b/index.js @@ -1,24 +1,2 @@ -require('./telegram'); -const config = require('./config'); - -var app = require('express')(); - -app.listen(80, config.website.hostname, () => { - console.log(`[express] Website is up and accessible on ${config.website.protocol}://${config.website.hostname}`); -}) - -app.get('/', function (req, res) { - res.sendFile(__dirname + '/index.html') -}) - -app.get('/background.jpg', function (req, res) { - res.sendFile(__dirname + '/background.jpg') -}) - -app.get('/auth', function (req, res) { - res.sendFile(__dirname + '/auth.html'); -}) - -app.get('/:id', function (req, res) { - res.send(req.params.id) -}) \ No newline at end of file +const bot = require('./telegram'); +const app = require('./website'); \ No newline at end of file diff --git a/node_modules/request/package.json b/node_modules/request/package.json index bc4d268b3904ca335d99facdc9f954ef666459ae..abc9f07ac6dce24477da87c4d20376a97b56ce4f 100644 --- a/node_modules/request/package.json +++ b/node_modules/request/package.json @@ -1,27 +1,29 @@ { - "_from": "request@^2.83.0", + "_from": "request", "_id": "request@2.88.0", "_inBundle": false, "_integrity": "sha512-NAqBSrijGLZdM0WZNsInLJpkJokL72XYjUpnB0iwsRgxh7dB6COrHnTBNwN0E+lHDAJzu7kLAkDeY08z2/A0hg==", "_location": "/request", "_phantomChildren": {}, "_requested": { - "type": "range", + "type": "tag", "registry": true, - "raw": "request@^2.83.0", + "raw": "request", "name": "request", "escapedName": "request", - "rawSpec": "^2.83.0", + "rawSpec": "", "saveSpec": null, - "fetchSpec": "^2.83.0" + "fetchSpec": "latest" }, "_requiredBy": [ + "#USER", + "/", "/node-telegram-bot-api" ], "_resolved": "https://registry.npmjs.org/request/-/request-2.88.0.tgz", "_shasum": "9c2fca4f7d35b592efe57c7f0a55e81052124fef", - "_spec": "request@^2.83.0", - "_where": "/home/capsule_man/developpement/happy-botday/node_modules/node-telegram-bot-api", + "_spec": "request", + "_where": "/home/capsule_man/developpement/happy-botday", "author": { "name": "Mikeal Rogers", "email": "mikeal.rogers@gmail.com" diff --git a/package.json b/package.json index ee4563db6f9a598fa2bfd6467bfe9253502f33a6..a11ae9c708317cf2eb405c9feb5a044310243f26 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "license": "ISC", "dependencies": { "express": "^4.16.4", - "node-telegram-bot-api": "^0.30.0" + "node-telegram-bot-api": "^0.30.0", + "request": "^2.88.0" } } diff --git a/telegram.js b/telegram.js index db3c40207068d2369e58562c8914ef96aafd2f19..da693976f86dc9c2318c636e0b35944ca58d81ce 100644 --- a/telegram.js +++ b/telegram.js @@ -1,3 +1,4 @@ +process.env["NTBA_FIX_319"] = 1; const TelegramBot = require('node-telegram-bot-api'); const config = require('./config'); @@ -13,6 +14,8 @@ bot.onText(/\/start/, (msg, _) => { bot.onText(/\/connect/, (msg, _) => { const chatId = msg.chat.id; - const resp = `Pour vous identifier, connectez-vous via l\'OAuth2 de ViaRézo depuis ce lien : ${config.website.protocol}://${config.website.hostname}/${chatId}`; + const resp = `Pour vous identifier, connectez-vous via l\'OAuth2 de ViaRézo depuis ce lien : ${config.website.protocol}://${config.website.hostname}/?chatid=${chatId}`; bot.sendMessage(chatId, resp); -}); \ No newline at end of file +}); + +module.exports = bot; \ No newline at end of file diff --git a/website.js b/website.js new file mode 100644 index 0000000000000000000000000000000000000000..efe2fe18ec204f0cdfa5973759c0e7c000e23c1e --- /dev/null +++ b/website.js @@ -0,0 +1,49 @@ +const config = require('./config'); + +const app = require('express')(); +const request = require('request'); +const bot = require('./telegram'); + +app.listen(80, config.website.hostname, () => { + console.log(`[express] Website is up and accessible on ${config.website.protocol}://${config.website.hostname}/`); +}) + +app.get('/', function (req, res) { + if (!req.query.chatid) { return res.sendFile(`${__dirname}/index.html`) }; + + const state = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); + const redirectURI = config.website.protocol + '://' + config.website.hostname + '/auth'; + const url = `https://auth.viarezo.fr/oauth/authorize/?redirect_uri=${redirectURI}&client_id=${config.oauth2.clientid}&response_type=code&state=${state}&scope=${config.oauth2.scope}`; + return res.redirect(301, url); +}) + +app.get('/background.jpg', function (req, res) { + res.sendFile(`${__dirname}/background.jpg`) +}) + +app.get('/auth', function (req, res) { + if (!req.query.code || !req.query.state) { return res.sendFile(`${__dirname}/auth.html`) } + + const options = { + url: 'https://auth.viarezo.fr/oauth/token', + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + form: { + grant_type: 'authorization_code', + code: req.query.code, + redirect_uri: config.website.protocol + '://' + config.website.hostname + '/auth', + client_id: config.oauth2.clientid, + client_secret: config.oauth2.secretid + } + } + + request(options, (err, res, body) => { + console.log(res.statusCode); + if (!err && res.statusCode == 200) { + console.log(body) + } + }) + return res.redirect(301, `${config.website.protocol}://${config.website.hostname}/auth`); +}) + +module.exports = app; \ No newline at end of file