Skip to content

Instantly share code, notes, and snippets.

@pdtyreus
Created September 8, 2016 22:15
Show Gist options
  • Select an option

  • Save pdtyreus/029947a42c5af460c617947170dbdec5 to your computer and use it in GitHub Desktop.

Select an option

Save pdtyreus/029947a42c5af460c617947170dbdec5 to your computer and use it in GitHub Desktop.
Slack chat.postMessage with attachments
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());
});
});
@pdtyreus

pdtyreus commented Sep 8, 2016

Copy link
Copy Markdown
Author

In the API documentation for chat.postMessage, the example shows you can add attachments to the message. Despite the endpoint name chat.postMessage the API call is actually an HTTP GET, with the message object passed as part of the URL query string.

Since the attachments are an array, the brackets can confuse the Slack API and lead to the cryptic invalid_array_arg error. The solution to this is to stringify and url encode the attachment array before adding it to the query string. I couldn't find reference to this anywhere in the API documentation, so I'm hoping this gist will help someone else.

@tomredman

Copy link
Copy Markdown

Thank you, I needed this!

@samturner3

Copy link
Copy Markdown

+1 Thanks!

@Sy1v4in

Sy1v4in commented Apr 6, 2018

Copy link
Copy Markdown

+1 Thanks!

@lambro

lambro commented Jul 31, 2018

Copy link
Copy Markdown

πŸ‘ Awesome! Thanks! πŸ’ƒ

@nuk

nuk commented Sep 18, 2018

Copy link
Copy Markdown

πŸ™Œ tks a lot

@johnmutuma5

Copy link
Copy Markdown

Nice. πŸ‘

@Xotl

Xotl commented May 5, 2019

Copy link
Copy Markdown

First result in Google about the Slack invalid_array_arg error. Thanks a lot 😊

@martyboggs

Copy link
Copy Markdown

@haffla

haffla commented Feb 25, 2020

Copy link
Copy Markdown

This is still true even though the endpoint is POST now, so so bad

https://api.slack.com/methods/chat.postMessage#arg_attachments

A JSON-based array of structured attachments, presented as a URL-encoded string

So it's not JSON, it's JSON-based

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment