Skip to content

Instantly share code, notes, and snippets.

@sjoerdvisscher
Last active August 18, 2017 15:59
Show Gist options
  • Save sjoerdvisscher/8692426 to your computer and use it in GitHub Desktop.
Save sjoerdvisscher/8692426 to your computer and use it in GitHub Desktop.
Create Slack bots with Meteor!
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&region=US&width=300&height=180>")
});
});
}
@rantav
Copy link

rantav commented Jan 28, 2015

For meteor you'd probably want to replace HTTP by Meteor.http

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment