Last active
August 18, 2017 15:59
-
-
Save sjoerdvisscher/8692426 to your computer and use it in GitHub Desktop.
Create Slack bots with Meteor!
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
function slackpost(channel, name, text) { | |
HTTP.post("https://q42.slack.com/services/hooks/incoming-webhook", {"params": | |
{"token": "TODO: fill in token!", | |
"payload": JSON.stringify({ | |
"channel": "#" + channel, | |
"username": name, | |
"text": text, | |
"icon_emoji": (name.indexOf("bot") > -1 ? ":ghost:" : "") | |
}) | |
}} | |
); | |
} | |
if (Meteor.isServer) { | |
Router.map(function () { | |
var that = this; | |
function command(name, body, hideCommand) { | |
that.route(name, { | |
where: 'server', | |
path: '/' + name, | |
action: function () { | |
var p = this.params; | |
if (!hideCommand) | |
slackpost(p.channel_name, p.user_name, "/" + name + " " + p.text); | |
body(function(t) { slackpost(p.channel_name, name + "bot", t) }, p.text, p); | |
this.response.writeHead(200, {'Content-Type': 'text/html'}); | |
this.response.end(name + " command executed!"); | |
} | |
}) | |
} | |
command("hoi", function(post, text) { | |
post("<http://static.q42.nl/images/employees/" + text + "gif.gif>"); | |
}); | |
command("stock", function(post, text) { | |
post("<http://chart.finance.yahoo.com/t?s=" + text + "&lang=en-US®ion=US&width=300&height=180>") | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For meteor you'd probably want to replace
HTTP
byMeteor.http