Unable to send slack.chat.postMessage with JSON format
OllyNuralAND opened this issue · 6 comments
Hi,
I'm new to this, so apologies if this is incorrectly formatted or needs some work as a request.
When sending a postMessage, I am trying to pass in a JSON message so as to display to the user, and when received on slack my message is always appearing as a string - and not being parsed as JSON and displaying a nice format. (See here)
Running on v10.1.1 on OSX
Example code:
const Slack = require('slack')
const slack = new Slack({token: config.slackAuthenticationConfig.token})
let reposToSend = {
attachments : [
{
title: repo.full_name,
title_link: repo.html_url,
color: "#0000FF",
text: "Last pushed to on: " + repo.pushed_at
},
...
]
}
slack.chat.postMessage({
text: JSON.stringify(reposToSend),
attachments: reposToSend,
channel: config.slackConfig.channel,
username: 'Some Github bot'
})
Expected - something similar to link above whereby a nicely formatted message is shown
Actual - In slack seeing this:
"{"attachments":[{"title":"some title","title_link":"some title link","color":"#0000FF","text":"some text"},...]}"
I have had a look through the code, and perhaps am thinking it is because it are not setting the 'Content-Type' : 'application/json' anywhere? The same request works when using webhooks, but it seems the capability was added for this format in October 2017.
Thanks!
Hey @OllyNuralAND! You shouldn't need to stringify anything yourself (unless you want it to show up as JSON in Slack).
The text
property sets the raw text you want to show up in Slack. The content in reposToSend
can be passed directly to chat.postMessage
.
slack.chat.postMessage({
text: "...",
attachments: [{
title: repo.full_name,
title_link: repo.html_url,
color: "#0000FF",
text: "Last pushed to on: " + repo.pushed_at
}],
channel: config.slackConfig.channel,
username: 'Some Github bot'
})
Let me know if that helps you out!
Hey :) I completely forgot to mention, I had tried that - as in same as above but with reposToSend
instead of JSON.stringify(reposToSend)
, however to no avail. I still receive the same string JSON in Slack.
Also my code example above is a bit off, attachments
within the postMessage({})
does not apply or affect my requests in any way. Looking at the API, it doesn't even seem to be a param I can use! I only tried it because of the actual slack API.
I think you're still getting a JSON string in Slack, because you're using JSON.stringify
when you don't need to be.
If you want to keep reposToSend
as a separate object, then you'd need to merge it into the parameters you're already specifying.
slack.chat.postMessage({
text: "Whatever you want this to say",
attachments: reposToSend.attachments,
channel: config.slackConfig.channel,
username: 'Some Github bot'
})
Could you try that exact code and post a screenshot of how that shows up in Slack for you?
If you're interested, the code which sets the Content-type
and handles the JSON.stringify
for you is all in tiny-json-http.
Ahhhhh it was the reposToSend.attachments that was getting me, my bad. Was putting the whole object in, rather than the attachments array.
Thanks for your super quick help! Very glad I get to use this than having to handle requests myself :)
And also thanks, I'll have a look now :)
No problem! Sometimes all it needs is a second pair of eyes!