diff options
author | Tollef Fog Heen <tfheen@err.no> | 2016-03-05 13:28:57 +0100 |
---|---|---|
committer | Tollef Fog Heen <tfheen@err.no> | 2016-03-05 13:28:57 +0100 |
commit | 5946a4a109dcc1474ab53ccc9e2025cb6ed88867 (patch) | |
tree | 3bb125f52983323472ab0715e64fad3f5ef60651 | |
parent | efb41803ed9293f0724da2c93898abd481a3a908 (diff) | |
parent | 360a91279b54656b29253c07b7ff2a27b12e684d (diff) | |
download | wee-slack-5946a4a109dcc1474ab53ccc9e2025cb6ed88867.tar.gz |
Merge pull request #147 from rawdigits/attachment-formatting
Format more fields from attachments
-rw-r--r-- | wee_slack.py | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/wee_slack.py b/wee_slack.py index 6172636..e3df8af 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 'title_link' in attachment: + 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 |