Forked from pdtyreus/SlackPostMessageWithAttachments.js
Created
September 24, 2018 22:32
-
-
Save synsa/7b5e24b4743c12e6e39b01bee049f460 to your computer and use it in GitHub Desktop.
Slack chat.postMessage with attachments
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
var http = require("https"); | |
var querystring = require('querystring'); | |
//2 attachments as JavaScript Objects | |
var attachments = [{ | |
fallback: "Attachment 1 Fallback", | |
title: "This is Attachment 1", | |
text: "Attachment 1 Text", | |
color: "#3964db" | |
}, { | |
fallback: "Attachment 2 Fallback", | |
title: "This is Attachment 2", | |
text: "Attachment 2 Text", | |
color: "#3964db" | |
}]; | |
//add the attachments to the message by stringify-ing them | |
//posting without stringify-ing the attachments will lead to an error from the Slack API | |
var message = { | |
token: "your-token", | |
channel: "@daniel", | |
as_user: false, | |
username: "daniel", | |
attachments: JSON.stringify(attachments), | |
text: "This is a message with attachments" | |
} | |
var qs = querystring.stringify(message); | |
var options = { | |
"method": "GET", | |
"hostname": "slack.com", | |
"path": "/api/chat.postMessage?" + qs | |
}; | |
var req = http.request(options, function (res) { | |
var chunks = []; | |
res.on("data", function (chunk) { | |
chunks.push(chunk); | |
}); | |
res.on("end", function () { | |
var body = Buffer.concat(chunks); | |
console.log(body.toString()); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment