-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial draft of the Gitter -> Slack relay
- Loading branch information
0 parents
commit 34ff42c
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/node_modules | ||
npm-debug.log | ||
.DS_Store | ||
/*.env |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Gitter Slack Relay | ||
|
||
Super simple Node application that listens to Gitter's realtime message streaming API and forwards those messages into a Slack room. Useful when you've got a public Gitter room, use Slack regularly and want to get notifications there. | ||
|
||
This currently does not go the other way, meaning no messages that you send to the Slack channel will go back to Gitter. | ||
|
||
Original inspiration came from https://github.com/reactor/gitter-slack-relay which is the exact same thing written in Java with Spring Boot and Reactor. | ||
|
||
## Usage | ||
|
||
```sh | ||
GITTER_ROOM_ID=<gitter room id> GITTER_TOKEN=<oauth bearer token> GITTER_ROOM_SLUG=<room URL slug> SLACK_HOOK_URL=<integration url> node gitter-to-slack.js | ||
``` | ||
|
||
## Output | ||
|
||
When you send a message in Gitter: | ||
|
||
 | ||
|
||
you'll get the JSON payload from Gitter in your console: | ||
|
||
```console | ||
Received Gitter payload: {"id":"messageid","text":"this shows up in Slack","html":"this shows up in Slack","sent":"2017-07-26T21:00:50.512Z","fromUser":{"id":"userid","username":"phillipuniverse","displayName":"Phillip Verheyden","url":"/phillipuniverse","avatarUrl":"https://avatars-02.gitter.im/gh/uv/4/phillipuniverse","avatarUrlSmall":"https://avatars2.githubusercontent.com/u/684275?v=4&s=60","avatarUrlMedium":"https://avatars2.githubusercontent.com/u/684275?v=4&s=128","v":6,"gv":"4"},"unread":true,"readBy":0,"urls":[],"mentions":[],"issues":[],"meta":[],"v":1} | ||
forwarding to Slack | ||
``` | ||
|
||
and see the message show up in Slack: | ||
|
||
 | ||
|
||
### Environment Variables | ||
|
||
- **GITTER_ROOM_ID** - This is a non-human-readable form of the Gitter room id referred to from the Gitter API. The easiest way to get this ID is to go to [the Gitter rooms API docs](https://developer.gitter.im/docs/rooms-resource), copy the example request and pick out the room id that you want to listen to | ||
- **GITTER_TOKEN** - The access token from https://developer.gitter.im/apps | ||
- **GITTER_ROOM_SLUG** - Used to build the link from the Slack message to the message in Gitter. If you are in a Gitter room then this value is the part after https://gitter.im/. So if a room is accessible at https://gitter.im/BroadleafCommerce/BroadleafCommerce then this value would be `BroadleafCommerce/BroadleafCommerce` | ||
- **SLACK_HOOK_URL** - After creating a 'Custom Integration' in Slack (go to your team settings, Configure Apps -> Custom Integrations -> Add Configuration) this should be the 'Webhook URL' in the configuration settings |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
var https = require('https'); | ||
var request = require('request'); | ||
var moment = require('moment'); | ||
|
||
var roomId = process.env.GITTER_ROOM_ID; | ||
var token = process.env.GITTER_TOKEN; | ||
var gitterRoomSlug = process.env.GITTER_ROOM_SLUG; | ||
var slackHookUrl = process.env.SLACK_HOOK_URL; | ||
var heartbeat = " \n"; | ||
|
||
var options = { | ||
hostname: 'stream.gitter.im', | ||
port: 443, | ||
path: '/v1/rooms/' + roomId + '/chatMessages', | ||
method: 'GET', | ||
headers: {'Authorization': 'Bearer ' + token} | ||
}; | ||
|
||
var req = https.request(options, function(res) { | ||
res.on('data', function(chunk) { | ||
var msg = chunk.toString(); | ||
if (msg !== heartbeat) { | ||
console.log('Received Gitter payload: ' + msg + ' forwarding to Slack'); | ||
var gitterData = JSON.parse(msg); | ||
var slackMessage = '<https://gitter.im/' + gitterRoomSlug + '?at=' + gitterData.id; | ||
slackMessage += '|' + gitterData.fromUser.displayName; | ||
var sentDate = moment(gitterData.sent).format('MMM-D h:mm A') | ||
slackMessage += ' [' + sentDate + ']>'; | ||
slackMessage += ': ' + gitterData.text; | ||
request.post(process.env.SLACK_HOOK_URL, | ||
{ json: { text: slackMessage} }, | ||
function (err, resp, body) { | ||
if (err) { | ||
console.log(err); | ||
} | ||
} | ||
) | ||
} | ||
}); | ||
}); | ||
|
||
req.on('error', function(e) { | ||
console.log('Something went wrong: ' + e.message); | ||
}); | ||
|
||
req.end(); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "gitter-to-slack-node", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "gitter-to-slack.js", | ||
"dependencies": { | ||
"moment": "^2.18.1", | ||
"request": "^2.81.0" | ||
}, | ||
"devDependencies": {}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC" | ||
} |