Created
October 25, 2016 02:39
-
-
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
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
command=/httpstatus | |
text=302 | |
response_url=https://hooks.slack.com/commands/1234/5678 ... |
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
'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'); |
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
app.post('/', (req, res) => { | |
let text = req.body.text; | |
// implement your bot here ... | |
}); |
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
if(! /^\d+$/.test(q.text)) { // not a digit | |
res.send('U R DOIN IT WRONG. Enter a status code like 200!'); | |
return; | |
} |
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
let data = { | |
response_type: 'in_channel', // public to the channel | |
text: '302: Found', | |
attachments:[ | |
{ | |
image_url: 'https://http.cat/302.jpg' | |
} | |
]}; | |
res.json(data); |
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
SLACK_CLIENT_ID=12345XXXXX.09876XXXXX | |
SLACK_CLIENT_SECRET=535d2f9.... | |
SLACK_VERIFICATION_TOKEN=42P829U... |
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
web: node index.js |
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
<a href="https://slack.com/oauth/authorize?scope=commands+team%3Aread&client_id=your_client_id"> |
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 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 | |
} | |
... |
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
... | |
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