From 3c4e38c8133f9b4b6276d50d33ada1abfe84dec3 Mon Sep 17 00:00:00 2001 From: Tollef Fog Heen Date: Mon, 21 Dec 2015 19:51:22 +0100 Subject: Format more fields from attachments This makes the formatting of attachments somewhat more verbose and makes it more similar to the web client. Downside is that it takes more vertical space. --- wee_slack.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/wee_slack.py b/wee_slack.py index 6172636..12b891a 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -1642,14 +1642,36 @@ def unwrap_attachments(message_json, text_before): attachment_text = '' if "attachments" in message_json: if text_before: - attachment_text = u' --- ' + attachment_text = u'\n' for attachment in message_json["attachments"]: + # Attachments should be rendered roughly like: + # + # $pretext + # $title ($title_link) OR $from_url + # $text + # $fields t = [] - if "from_url" in attachment and text_before is False: - t.append(attachment['from_url']) - if "fallback" in attachment: + if 'pretext' in attachment: + t.append(attachment['pretext']) + if "title" in attachment: + if attachment["title_link"]: + t.append('%s (%s)' % (attachment["title"], attachment["title_link"],)) + else: + t.append(attachment["title"]) + elif "from_url" in attachment: + t.append(attachment["from_url"]) + if "text" in attachment: + tx = re.sub(r' *\n[\n ]+', '\n', attachment["text"]) + t.append(tx) + if 'fields' in attachment: + for f in attachment['fields']: + if f['title'] != '': + t.append('%s %s' % (f['title'], f['value'],)) + else: + t.append(f['value']) + if t == [] and "fallback" in attachment: t.append(attachment["fallback"]) - attachment_text += ": ".join(t) + attachment_text += "\n".join([x.strip() for x in t if x]) return attachment_text -- cgit From 360a91279b54656b29253c07b7ff2a27b12e684d Mon Sep 17 00:00:00 2001 From: Tollef Fog Heen Date: Tue, 19 Jan 2016 07:47:12 +0100 Subject: Avoid unknown entries by checking for title_link properly --- wee_slack.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wee_slack.py b/wee_slack.py index 12b891a..e3df8af 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -1654,7 +1654,7 @@ def unwrap_attachments(message_json, text_before): if 'pretext' in attachment: t.append(attachment['pretext']) if "title" in attachment: - if attachment["title_link"]: + if 'title_link' in attachment: t.append('%s (%s)' % (attachment["title"], attachment["title_link"],)) else: t.append(attachment["title"]) -- cgit