Slack API - Add custom plain text error message

I am using Python 2.7 along with python-slackclient . I have such an attachment structure:

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 message is posted, it simply publishes clear text without formatting:

{"attachments": [{"title": "Upgrading Grafana to 3.0", "color": "# 7CD197", "text": "Hi guys, I added that my browser is JIRA, so this email will create a ticket which we can queue for his support. \ u00a0 Eric, if you wouldn \ u2019t mind just replying to this email with additional info? \ n \ n \ u00a0 \ n \ n \ u00a0 \ n \ n Missing: Thursday, August 25, 2016 11:41 AM \ n "," title_link ":" https://jira.jr.com/browse/ops-164 "," mrkdwn_in ": [" text "," preposition "," fields "] , "preposition": "A detailed summary for ops-164", "fallback": "Upgrade Grafana to 3.0, https://jira.jr.com/browse/ops-164 "}]}

What am I doing wrong? I also tried doing attachments=self.msg in a call to Send() , but at the same time I did not get any output to my slack channel.

+7
python slack-api slack
source share
2 answers

As it turned out, the challenge

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

will appear to add the top layer { "attachments": ... } for you. Therefore, changing my self.msg is simple:

 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 shell { "attachments": ... } , the api was able to post the attached message as expected.

+5
source share

There are several quirks in the chat.postMessage method - like most Slack web interfaces, it only supports application/x-www-form-urlencoded content types and does not support JSON. An aspect of quirkier is that the attachments parameter accepts a JSON array encoded by URLs. Right now, it looks like you are sending the text parameter to your own Python array.

For Slack to understand this structure, you first need to turn it into a JSON string. The API wrapper you are using can probably handle the next step of converting to a URL-encoded representation.

Finally, the application itself does not fit into the text message - this is a separate field. You want to specify something more similar to this by defining the 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 after including attachments.

+1
source share

All Articles