Skip to content

Commit d4ee867

Browse files
Fix slack message attachment
1 parent b79782b commit d4ee867

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

app.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,11 +447,14 @@ def render_message(self, message, channel_id=None):
447447
attachment_widget = Attachment(
448448
service_name=attachment.get('service_name'),
449449
title=attachment.get('title'),
450+
from_url=attachment.get('from_url'),
450451
fields=attachment.get('fields'),
451452
color=attachment.get('color'),
452-
author_name=attachment.get('author_name'),
453+
author_name=attachment.get('author_name') or attachment.get('author_subname'),
453454
pretext=attachment.get('pretext'),
454455
text=message_text,
456+
attachment_text=attachment.get('text'),
457+
ts=attachment.get('ts'),
455458
footer=attachment.get('footer')
456459
)
457460
image_url = attachment.get('image_url')

sclack/components.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from .markdown import MarkdownText
1212
from .store import Store
1313
from sclack.utils.channel import is_group, is_channel, is_dm
14+
from sclack.utils.message import format_date_time
1415

1516

1617
MARK_READ_ALARM_PERIOD = 3
@@ -36,35 +37,57 @@ def __init__(self,
3637
service_name=None,
3738
title=None,
3839
title_link=None,
40+
from_url=None,
3941
author_name=None,
4042
pretext=None,
4143
text=None,
4244
fields=None,
45+
attachment_text=None,
46+
ts=None,
4347
footer=None):
4448
body = []
4549
if not color:
4650
color = 'CCCCCC'
4751
color = '#{}'.format(shorten_hex(color))
52+
4853
self._image_index = 0
54+
self.from_url = from_url
55+
4956
if service_name:
5057
body.append(urwid.Text(('attachment_title', service_name)))
5158
self._image_index = self._image_index + 1
59+
5260
if title:
53-
body.append(urwid.Text(('attachment_title', title)))
61+
body.append(urwid.Text(('attachment_title', title.strip())))
5462
self._image_index = self._image_index + 1
63+
5564
if author_name:
5665
body.append(urwid.Text(('attachment_title', author_name)))
5766
self._image_index = self._image_index + 1
67+
5868
if pretext:
5969
body.append(urwid.Text(MarkdownText(pretext).markup))
6070
self._image_index = self._image_index + 1
61-
if text:
62-
body.append(urwid.Text(MarkdownText(text).markup))
71+
72+
text_display = attachment_text if attachment_text is not None else text
73+
if text_display:
74+
body.append(urwid.Text(MarkdownText(text_display.strip()).markup))
75+
6376
if fields:
6477
body.append(Fields(fields))
65-
if footer:
66-
body.append(urwid.Text(MarkdownText(footer).markup))
78+
79+
if footer or ts:
80+
footer_parts = []
81+
if footer:
82+
footer_parts.append(footer)
83+
if ts:
84+
footer_parts.append(format_date_time(ts))
85+
86+
footer_text = ' | '.join(footer_parts)
87+
body.append(urwid.Text(MarkdownText(footer_text).markup))
88+
6789
self.pile = urwid.Pile(body)
90+
6891
super(Attachment, self).__init__(self.pile, color)
6992

7093
@property

sclack/utils/message.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from datetime import datetime
2+
3+
4+
def format_date_time(ts):
5+
"""
6+
Format date time for message
7+
:param ts:
8+
:return:
9+
"""
10+
message_datetime = datetime.fromtimestamp(float(ts))
11+
message_date = message_datetime.date()
12+
today = datetime.today().date()
13+
14+
if message_date == today:
15+
date_text = message_datetime.strftime('Today at %I:%M%p')
16+
else:
17+
date_text = message_datetime.strftime('%b %d, %Y at %I:%M%p')
18+
19+
return date_text

0 commit comments

Comments
 (0)