Skip to content

Commit

Permalink
feat: roughing out chat-bot codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Coe committed Oct 15, 2016
0 parents commit 5dcf057
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
node_modules
.env
.nyc_output
coverage
11 changes: 11 additions & 0 deletions commands/flip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const flip = require('flip')

exports.command = 'flip <strings...>'

exports.describe = "to davy jones' locker with ye"

exports.builder = {}

exports.handler = function (argv) {
argv.respond(flip(argv.string.join(' ')))
}
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "pirate-joe",
"version": "1.0.0",
"description": "yargs' chat-bot",
"main": "server.js",
"scripts": {
"test": "nyc mocha test.js"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/yargs/pirate-joe.git"
},
"keywords": [
"chat-bot",
"example"
],
"author": "Ben Coe <ben@npmjs.com>",
"license": "ISC",
"bugs": {
"url": "https://github.com/yargs/pirate-joe/issues"
},
"homepage": "https://github.com/yargs/pirate-joe#readme",
"dependencies": {
"body-parser": "^1.15.2",
"dotenv": "^2.0.0",
"express": "^4.14.0",
"flip": "^1.0.0",
"request": "^2.75.0",
"yargs": "^6.1.0-candidate"
}
}
35 changes: 35 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const express = require('express')
const bodyParser = require('body-parser')

if (process.env.NODE_ENV !== 'production') require('dotenv').config()

const app = express()
const parser = require('yargs')
.commandDir('commands')
.demand(1)
.strict()
.help()

app.use(bodyParser.urlencoded({ extended: false }))

app.post('/', function (req, res) {
let context = Object.assign({}, req.body)

context.respond = function (msg) {
res.setHeader('Content-Type', 'application/json')
res.send(JSON.stringify({
response_type: 'in_channel',
text: msg
}))
}
context.batman = Math.random() * 100

parser.parse(req.body.text || '', context, (err, argv, output) => {
if (output) argv.respond(output)
})
})

const port = process.env.PORT || 3000
app.listen(port, function (foo) {
console.log('pirate joe bot listening on :' + port, 'beep boop')
})

0 comments on commit 5dcf057

Please sign in to comment.