From b66d75dac3e22b58b75324a26e86c9c87c5a7b97 Mon Sep 17 00:00:00 2001 From: nicholas Date: Sat, 15 Dec 2018 14:32:31 -0500 Subject: [PATCH] Start a POST response to Slack mentions. --- index.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++---------- package.json | 2 ++ 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index f23a264..3487ae5 100644 --- a/index.js +++ b/index.js @@ -1,25 +1,70 @@ -const express = require('express') -const app = express(); +const https = require('https'); +const express = require('express'); +const bodyParser = require('body-parser'); const strategies = require('./strategies'); +const app = express(); +app.use(bodyParser.json()); app.get('/', (req, res) => { const theStrategy = strategies[ Math.floor(Math.random()*strategies.length) ]; - res.send(theStrategy); + res.write('
'); + res.write(theStrategy.toLowerCase()); + res.write('
'); + res.end(); }); app.post('/', (req, res, next) => { - let payload = req.body; - res.sendStatus(200); - - if (payload.event.type === 'app_mention') { - // respond in kind + if (req.body.type === 'url_verification') { + return res.status(200).json(req.body.challenge); } + + if (req.body.type ==='event_callback') { + if (req.body.event.type === 'app_mention' && 1===2) { + var theResponse = { + hostname: 'slack.com', + path: '/api/chat.postMessage?token='+botAccessToken, + port: 443, + method: 'POST', + channel: 'CETLNV30Q', + headers: { + 'Content-Type': 'text/plain; charset=utf-8', + }, + text: 'Hihihi', + channel: 'CETLNV30Q', + } + + var theMessage = { + text: 'Hello I Am Bot', + channel: 'CETLNV30Q', + } + + var theResponseRequest = https.request(theResponse, (theSlackResponse) => { + console.log('statusCode: '+theSlackResponse.statusCode); + console.log('headers: '+JSON.stringify(theSlackResponse.headers,null,2)); + + theSlackResponse.on('data', (d) => { + process.stdout.write(d); + }); + }); + + theResponseRequest.on('error', (e) => { + console.error(e); + }); + + theResponseRequest.write('hi123'); + + theResponseRequest.end(); + + + } + + } + + return res.status(200).end(); }); app.listen(4242, () => { console.log('oblique strategies are being served') }); - - diff --git a/package.json b/package.json index b4401ac..2de711a 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,10 @@ "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Nicholas Warzin", + "repository": "https://nickwarzin.com:4243/nicholas/oblique-strategies", "license": "ISC", "dependencies": { + "body-parser": "^1.18.3", "express": "^4.16.4" } }