-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 109d1a0
Showing
7 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# package directories | ||
node_modules | ||
jspm_packages | ||
|
||
# Serverless directories | ||
.serverless |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SLACK_TOKEN: YOUR_SLACK_KEY_HERE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |