Skip to content

Instantly share code, notes, and snippets.

@girliemac
Created October 25, 2016 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save girliemac/3db0b566ac6e676ec8f297ed97ce4ecd to your computer and use it in GitHub Desktop.
Save girliemac/3db0b566ac6e676ec8f297ed97ce4ecd to your computer and use it in GitHub Desktop.
Snippets for my Medium Post: Creating a Slack Command Bot from Scratch with Node.js & Distribute It
command=/httpstatus
text=302
response_url=https://hooks.slack.com/commands/1234/5678 ...
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const server = app.listen(3000, () => {  
 console.log('Express server listening on port %d in %s mode', server.address().port, app.settings.env);});
const bodyParser = require('body-parser');
app.post('/', (req, res) => {
let text = req.body.text;
// implement your bot here ...
});
if(! /^\d+$/.test(q.text)) { // not a digit
res.send('U R DOIN IT WRONG. Enter a status code like 200!');
return;
}
let data = {
response_type: 'in_channel', // public to the channel
text: '302: Found',
attachments:[
{
image_url: 'https://http.cat/302.jpg'
}
]};
res.json(data);
SLACK_CLIENT_ID=12345XXXXX.09876XXXXX
SLACK_CLIENT_SECRET=535d2f9....
SLACK_VERIFICATION_TOKEN=42P829U...
web: node index.js
<a href="https://slack.com/oauth/authorize?scope=commands+team%3Aread&client_id=your_client_id">
const request = require('request');
app.get('/slack', function(req, res){
var data = {form: {
client_id: process.env.SLACK_CLIENT_ID,
client_secret: process.env.SLACK_CLIENT_SECRET,
code: req.query.code
}};
request.post('https://slack.com/api/oauth.access', data, function (error, response, body) {
if (!error && response.statusCode == 200) {
// You are done.
// If you want to get team info, you need to get the token here
let token = JSON.parse(body).access_token; // Auth token
}
...
...
request.post('https://slack.com/api/team.info', {form: {token: token}}, function (error, response, body) {
if (!error && response.statusCode == 200) {
let team = JSON.parse(body).team.domain;
res.redirect('http://' +team+ '.slack.com');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment