Skip to content

Instantly share code, notes, and snippets.

@pualien
Last active February 27, 2020 15:36
Show Gist options
  • Save pualien/7c737d9624ecfb1afe779c7906c50b59 to your computer and use it in GitHub Desktop.
Save pualien/7c737d9624ecfb1afe779c7906c50b59 to your computer and use it in GitHub Desktop.
Simple methods to send javascript obejects to slack webhook
let js_obj = {a:'test_track', b:2};
function __getSlackFields(data){
var fields = [];
Object.keys(data).map((key) => {
fields.push({ title: key, value: data[key], short: true });
return key;
});
return fields;
}
function __postMessageToSlack(webhook_url, payload){
var xhr = new XMLHttpRequest()
xhr.open('POST', webhook_url, false);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(payload);
}
payload = JSON.stringify({'attachments': [{'fields': __getSlackFields(js_obj)}]});
__postMessageToSlack($$YOUR_SLACK_WEBHOOK$$, payload);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment