TransWikia.com

Slack API - Attatchments from custom bot post as plain text

Stack Overflow Asked by MrDuk on February 3, 2021

I’m using Python 2.7 along with a python-slackclient. I have an attachment structure like so:

self.msg = {
    "attachments": [
        {
            "fallback": "%s, %s" % (self.jiraIssueObj.fields.summary, self.link),
            "pretext": "Detail summary for %s" % self.jiraIssueObj,
            "title": self.jiraIssueObj.fields.summary,
            "title_link": self.link,
            "text": self.jiraIssueObj.fields.description[0:self.maxSummary],
            "color": "#7CD197",
            "mrkdwn_in": ["text", "pretext", "fields"]
        }
    ]
}

then,

def Send(self):
        if (self.msg):
            slack_client.api_call("chat.postMessage", channel=self.channel, text=self.msg, as_user=True)
            self.msg = None

However, when this posts, it just posts the plaintext, with no formatting:

{"attachments": [{"title": "Upgrade Grafana to 3.0", "color": "#7CD197
", "text": "Iu2019ve added the JIRA maillist so this email
will create a ticket we can queue it up in support.u00a0 Eric if you
wouldnu2019t mind just replying to this email with the additional
info?nnu00a0nnu00a0nnSent: Thursday, August
25, 2016 11:41 AMn", "title_link":
"https://jira.jr.com/browse/ops-164", "mrkdwn_in": ["text",
"pretext", "fields"], "pretext": "Detail summary for ops-164",
"fallback": "Upgrade Grafana to 3.0,
https://jira.jr.com/browse/ops-164"}]}

What am I doing wrong? I’ve tried also doing attachments=self.msg in the Send() call, but I get no output at all to my slack channel when doing that.

2 Answers

As it turns out, the call to

slack_client.api_call("chat.postMessage", channel=self.channel, attachments=self.msg, as_user=True)

apears to add the top layer { "attachments": ... } for you. So by changing my self.msg to simply be:

self.format = [{
    "fallback": "%s, %s" % (self.jiraIssueObj.fields.summary, self.link),
    "pretext": "Detail summary for %s" % self.jiraIssueObj,
    "title": self.jiraIssueObj.fields.summary,
    "title_link": self.link,
    "text": self.jiraIssueObj.fields.description[0:self.maxSummary],
    #"color": "#7CD197",
    "mrkdwn_in": ["text", "pretext", "fields"]
}]

without this outer { "attachments": ... } wrapper, the api was able to post the message attachment as expected.

Answered by MrDuk on February 3, 2021

The chat.postMessage method has a couple of quirks -- like most of Slack's web APIs, it only supports application/x-www-form-urlencoded content-types, and does not support JSON. The quirkier aspect is that the attachments parameter takes a URL-encoded array of JSON. Right now, it appears you're sending the text parameter a native Python array.

For Slack to understand that structure, you'll first need to turn it into a JSON string. The API wrapper you're using can probably handle the next step of conversion into a URL-encoded representation.

Finally, the attachment itself doesn't get placed in the text of the message -- that's a separate field. You'll want to specify something more like this, after defining your JSON string as self.attachments:

slack_client.api_call("chat.postMessage", channel=self.channel, attachments=self.attachments, as_user=True)

The text field becomes optional once you include attachments.

Answered by Taylor Singletary on February 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP