Last active
March 12, 2018 08:40
-
-
Save Tony0205/4c2ef652d12822bad276c22ad646de52 to your computer and use it in GitHub Desktop.
JS bot for slack (customizing)
This file contains hidden or 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
const v8 = require('vm') | |
// it is slack request querystring of "command line" | |
/* | |
{"data": | |
{ | |
"token": "Slack App Credentials Token in Basic Information", | |
"team_id": "team primary id", | |
"team_domain": "team domain", | |
"channel_id": "channel id", | |
"channel_name": "channel name", | |
"user_id": "user_id", | |
"user_name": "user_name", | |
"command": "/Slash command", | |
"text": "Slack request text", | |
"response_url": "hooks url", | |
"trigger_id": "trigger id" | |
} | |
} | |
*/ | |
const userContext = {} | |
function getContext(key) { | |
if (!userContext.hasOwnProperty(key)) { | |
let reset = () => { | |
userContext[key] = {reset} | |
return userContext[key] | |
} | |
return v8.createContext(reset()) | |
} | |
return userContext[key] | |
} | |
function normalizeCode(code) { | |
// restore chars that slack api has been replaced | |
code = code.replace(/“|”/g, '"') | |
.replace(/‘|’/g, "'") | |
.replace(/&/g, '&') | |
.replace(/>/g, '>') | |
.replace(/</g, '<') | |
.replace(/```/g, '') | |
.trim() | |
// encapsule objectliteral only expression (caused by vm module's bug) | |
if (code[0] === '{' && code[code.length - 1] === '}') { | |
code = `(${code})` | |
} | |
return code | |
} | |
function formatResult(result) { | |
if (typeof result === 'undefined' || result === undefined) { | |
return "" | |
} | |
if (typeof result !== 'string') { | |
result = JSON.stringify(result) | |
} | |
return "```" + result + "```" | |
} | |
// this function is verify that requests are actually comming from Slack. | |
function verificationCheck(data) { | |
// verify | |
if(data.token == "Credentials Token" && data.team_id == "team_id"){ | |
return true; | |
} | |
return false; | |
} | |
exports.handler = function(event, context, callback) { | |
let payload = {}; // response object to Slack | |
console.log(event); | |
let sandbox = getContext(event.data.user_id || 'U000000000'); | |
let verification_check = verificationCheck(event.data); | |
// callback(Error error, Object result); | |
if (verification_check == false) { | |
callback("Your request is an invalid token or invalid user."); | |
return; | |
} | |
let code = normalizeCode(event.data.text); | |
console.log(code); | |
// TODO: transpile sourcecode when specified trigger_word is given | |
try { | |
let result = v8.runInNewContext(code, sandbox, {timeout: 7}) | |
payload.text = formatResult(result); | |
payload.response_type = "in_channel"; | |
callback(null, payload); | |
} catch(e) { | |
console.log("error?", e); | |
callback(e.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment