Skip to content

Instantly share code, notes, and snippets.

@samuelcouch
Last active August 29, 2015 14:18
Show Gist options
  • Save samuelcouch/30e5454101ec78012dbe to your computer and use it in GitHub Desktop.
Save samuelcouch/30e5454101ec78012dbe to your computer and use it in GitHub Desktop.
var Qs = require('qs');
var request = require('request');
//
// var SLACK_TEST_URL = 'https://slack.com/api/chat.postMessage?token=<<MY-TOKEN>>&channel=%23review-bot-test&attachments=%5B%20%20%20%20%20%20%20%20%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%22fallback%22%3A%20%22ReferenceError%20-%20UI%20is%20not%20definied%3A%20https%3A%2F%2Fhoneybadger.io%2Fpath%2Fto%2Fevent%2F%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%22text%22%3A%20%22%3Chttps%3A%2F%2Fhoneybadger.io%2Fpath%2Fto%2Fevent%2F%7CReferenceError%3E%20-%20UI%20is%20not%20defined%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%22fields%22%3A%20%5B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Project%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22value%22%3A%20%22Awesome%20Project%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22short%22%3A%20true%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22title%22%3A%20%22Environment%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22value%22%3A%20%22production%22%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22short%22%3A%20true%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%22color%22%3A%20%22%23F35A00%22%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%5D&pretty=1'
//
// Qs.parse(SLACK_TEST_URL);
// /*
// This returns
// => {
// 'https://slack.com/api/chat.postMessage?token': '<<MY-TOKEN>>',
// channel: '#review-bot-test',
// attachments: '[{ "fallback": "ReferenceError - UI is not definied: https://honeybadger.io/path/to/event/", "text": "<https://honeybadger.io/path/to/event/|ReferenceError> - UI is not defined", "fields": [{ "title": "Project", "value": "Awesome Project", "short": true }, { "title": "Environment", "value": "production", "short": true }], "color": "#F35A00" } ]',
// pretty: '1'
// }
//
// Notice that the `attachments` is actually a string
//*/
var req = {
token: '<<MY-TOKEN>>',
channel: '#review-bot-test',
text: ' ',
attachments: [{
fallback: "ReferenceError - UI is not definied: https://honeybadger.io/path/to/event/",
text: "<https://honeybadger.io/path/to/event/|ReferenceError> - UI is not defined",
fields: [{
title: "Project",
value: "Awesome Project",
short: true
},
{
title: "Environment",
value: "production",
short: true
}],
color: "#F35A00"
}]
}
var request_url = 'https://slack.com/api/chat.postMessage?' + Qs.stringify(req);
/*
This returns:
==> https://slack.com/api/chat.postMessage?token=<<MY-TOKEN>>&channel=%23review-bot-test&attachments%5B0%5D%5Bfallback%5D=ReferenceError%20-%20UI%20is%20not%20definied%3A%20https%3A%2F%2Fhoneybadger.io%2Fpath%2Fto%2Fevent%2F&attachments%5B0%5D%5Btext%5D=%3Chttps%3A%2F%2Fhoneybadger.io%2Fpath%2Fto%2Fevent%2F%7CReferenceError%3E%20-%20UI%20is%20not%20defined&attachments%5B0%5D%5Bfields%5D%5B0%5D%5Btitle%5D=Project&attachments%5B0%5D%5Bfields%5D%5B0%5D%5Bvalue%5D=Awesome%20Project&attachments%5B0%5D%5Bfields%5D%5B0%5D%5Bshort%5D=true&attachments%5B0%5D%5Bfields%5D%5B1%5D%5Btitle%5D=Environment&attachments%5B0%5D%5Bfields%5D%5B1%5D%5Bvalue%5D=production&attachments%5B0%5D%5Bfields%5D%5B1%5D%5Bshort%5D=true&attachments%5B0%5D%5Bcolor%5D=%23F35A00
The main problem is that it's missing a few characters from the original -- mainly that `[{}]` isn't included as characters in `attachments`
*/
request.get(request_url, function(err, res, body){
console.log(body)
/*
{"ok":true,"channel":"C049B9JK0","ts":"1428708007.000092","message":{"text":" ","username":"bot","type":"message","subtype":"bot_message","ts":"1428708007.000092"}}
*/
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment