Skip to content

Commit

Permalink
[init] finished this project
Browse files Browse the repository at this point in the history
  • Loading branch information
applefreak committed Mar 16, 2017
0 parents commit 109d1a0
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 0 deletions.
114 changes: 114 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@

# Created by https://www.gitignore.io/api/node,macos,linux,serverless

### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*

### macOS ###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env


### Serverless ###
# Ignore build directory
.serverless

# End of https://www.gitignore.io/api/node,macos,linux,serverless
bookmarklet.js
secrets.yml
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# package directories
node_modules
jspm_packages

# Serverless directories
.serverless
37 changes: 37 additions & 0 deletions autotag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

var _ = require('lodash');
var validUrl = require('valid-url');
var URL = require('url-parse');

var default_sites = {
'github.com': 'Github',
'medium.com': 'Medium',
'reddit.com': 'Reddit',
};

var autoTag = (tags, url) => {
var result = '';
if (! _.isArray(tags)) throw 'tags not an array!';
if (validUrl.isUri(url) === undefined) throw 'URL is not valid!';

var parsed = URL(url);
var hostname = default_sites[parsed.hostname];
if (hostname !== undefined) {
tags.push(hostname);
}

if (tags.length > 0) {
result = toTagString(tags);
}

return result;
}

// tags is array of strings
var toTagString = (tags) => {
if (! _.isArray(tags)) throw 'tags not an array!';

return ' #' + _.join(tags, ' #');
}

module.exports = autoTag;
42 changes: 42 additions & 0 deletions handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

var Botkit = require('botkit');
var autotag = require('./autotag');
var controller = Botkit.slackbot();

module.exports.bookmark = (event, context, callback) => {
var response_body = {
success: false,
};

try {
var bot = controller.spawn({
token: process.env.SLACK_TOKEN
});
var req_body = JSON.parse(event.body);
if (!('tags' in req_body)) req_body.tags = [];
var tags = autotag(req_body.tags, req_body.url);
bot.say({
text: '@paperbot ' + req_body.url + tags,
channel: process.env.SLACK_BOOKMARK_CHANNEL
});

response_body.success = true;

} catch(e) {
console.log('An error has occured:');
console.log(e);
response_body.error = e;
}

var response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Credentials" : true
},
body: JSON.stringify(response_body),
};

callback(null, response);
};
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "serverless-slackbot",
"version": "1.0.0",
"description": "",
"main": "handler.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Poyu Chen <p820316@gmail.com>",
"license": "ISC",
"dependencies": {
"botkit": "^0.5.1",
"lodash": "^4.17.4",
"url-parse": "^1.1.8",
"valid-url": "^1.0.9"
}
}
1 change: 1 addition & 0 deletions secrets.example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SLACK_TOKEN: YOUR_SLACK_KEY_HERE
22 changes: 22 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
service: slackBot

provider:
name: aws
runtime: nodejs4.3

custom:
secrets: ${file(secrets.yml)}
dev: dev
prod: bookmarks

functions:
bookmark:
handler: handler.bookmark
environment:
SLACK_TOKEN: ${self:custom.secrets.SLACK_TOKEN}
SLACK_BOOKMARK_CHANNEL: ${self:custom.${opt:stage, self:provider.stage}}
events:
- http:
path: add
method: POST
cors: true

0 comments on commit 109d1a0

Please sign in to comment.