From f28ab5f2b9e265659c58dc0de8c9b05c59653fe6 Mon Sep 17 00:00:00 2001 From: dannc Date: Thu, 23 Feb 2017 13:35:07 +0700 Subject: [PATCH] hello bot --- app.js | 24 ++++++++++++++++++++++++ bot.js | 13 +++++++++++++ package.json | 2 +- 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 bot.js diff --git a/app.js b/app.js index e69de29..300b8b8 100644 --- a/app.js +++ b/app.js @@ -0,0 +1,24 @@ +var express = require('express'); +var bodyParser = require('body-parser'); +var zmrBot = require('./bot.js'); + +var app = express(); +var port = process.env.PORT || 3000; + +// body parser middleware +app.use(bodyParser.urlencoded({ extended: true })); + +// test route +app.get('/', function (req, res) { res.status(200).send('Hello world!') }); + +app.post('/hello', zmrBot); + +// error handler +app.use(function (err, req, res, next) { + console.error(err.stack); + res.status(400).send(err.message); +}); + +app.listen(port, function () { + console.log('Slack bot listening on port ' + port); +}); \ No newline at end of file diff --git a/bot.js b/bot.js new file mode 100644 index 0000000..17724cf --- /dev/null +++ b/bot.js @@ -0,0 +1,13 @@ +module.exports = function (req, res, next) { + var userName = req.body.user_name; + var botPayload = { + text : 'Hello, ' + userName + '!' + }; + + // avoid infinite loop + if (userName !== 'slackbot') { + return res.status(200).json(botPayload); + } else { + return res.status(200).end(); + } +} \ No newline at end of file diff --git a/package.json b/package.json index af6d487..a551da1 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "zmr_slackbot", "version": "0.0.1", "description": "zaymer slackbot", - "main": "bot.js", + "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" },