Last active
February 27, 2020 15:36
-
-
Save pualien/7c737d9624ecfb1afe779c7906c50b59 to your computer and use it in GitHub Desktop.
Simple methods to send javascript obejects to slack webhook
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
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