2025-03-30 12:53:48 -04:00
|
|
|
|
from typing import Dict, Any, Optional
|
|
|
|
|
|
|
2025-05-10 18:04:58 +08:00
|
|
|
|
|
2025-03-30 12:53:48 -04:00
|
|
|
|
class SlackEvent(dict):
|
|
|
|
|
|
@staticmethod
|
2025-05-10 18:04:58 +08:00
|
|
|
|
def from_payload(payload: Dict[str, Any]) -> Optional['SlackEvent']:
|
2025-03-30 12:53:48 -04:00
|
|
|
|
try:
|
|
|
|
|
|
event = SlackEvent(payload)
|
|
|
|
|
|
return event
|
|
|
|
|
|
except KeyError:
|
|
|
|
|
|
return None
|
2025-05-10 18:04:58 +08:00
|
|
|
|
|
2025-03-30 12:53:48 -04:00
|
|
|
|
@property
|
|
|
|
|
|
def text(self) -> str:
|
2025-05-10 18:04:58 +08:00
|
|
|
|
if self.get('event', {}).get('channel_type') == 'im':
|
|
|
|
|
|
blocks = self.get('event', {}).get('blocks', [])
|
|
|
|
|
|
if not blocks:
|
|
|
|
|
|
return ''
|
|
|
|
|
|
|
|
|
|
|
|
elements = blocks[0].get('elements', [])
|
|
|
|
|
|
if not elements:
|
|
|
|
|
|
return ''
|
|
|
|
|
|
|
|
|
|
|
|
elements = elements[0].get('elements', [])
|
|
|
|
|
|
text = ''
|
|
|
|
|
|
|
|
|
|
|
|
for el in elements:
|
|
|
|
|
|
if el.get('type') == 'text':
|
|
|
|
|
|
text += el.get('text', '')
|
|
|
|
|
|
elif el.get('type') == 'link':
|
|
|
|
|
|
text += el.get('url', '')
|
|
|
|
|
|
|
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
|
|
if self.get('event', {}).get('channel_type') == 'channel':
|
|
|
|
|
|
message_text = ''
|
|
|
|
|
|
for block in self.get('event', {}).get('blocks', []):
|
|
|
|
|
|
if block.get('type') == 'rich_text':
|
|
|
|
|
|
for element in block.get('elements', []):
|
|
|
|
|
|
if element.get('type') == 'rich_text_section':
|
2025-04-01 02:34:16 -04:00
|
|
|
|
parts = []
|
2025-05-10 18:04:58 +08:00
|
|
|
|
for el in element.get('elements', []):
|
|
|
|
|
|
if el.get('type') == 'text':
|
|
|
|
|
|
parts.append(el['text'])
|
|
|
|
|
|
elif el.get('type') == 'link':
|
|
|
|
|
|
parts.append(el['url'])
|
|
|
|
|
|
message_text = ''.join(parts)
|
2025-03-30 12:53:48 -04:00
|
|
|
|
|
2025-05-10 18:04:58 +08:00
|
|
|
|
return message_text
|
2025-03-30 12:53:48 -04:00
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def user_id(self) -> Optional[str]:
|
2025-05-10 18:04:58 +08:00
|
|
|
|
return self.get('event', {}).get('user', '')
|
|
|
|
|
|
|
2025-03-30 12:53:48 -04:00
|
|
|
|
@property
|
|
|
|
|
|
def channel_id(self) -> Optional[str]:
|
2025-05-10 18:04:58 +08:00
|
|
|
|
return self.get('event', {}).get('channel', '')
|
|
|
|
|
|
|
2025-03-30 12:53:48 -04:00
|
|
|
|
@property
|
|
|
|
|
|
def type(self) -> str:
|
2025-05-10 18:04:58 +08:00
|
|
|
|
"""message对应私聊,app_mention对应频道at"""
|
|
|
|
|
|
return self.get('event', {}).get('channel_type', '')
|
|
|
|
|
|
|
2025-03-30 12:53:48 -04:00
|
|
|
|
@property
|
|
|
|
|
|
def message_id(self) -> str:
|
2025-05-10 18:04:58 +08:00
|
|
|
|
return self.get('event_id', '')
|
|
|
|
|
|
|
2025-03-30 12:53:48 -04:00
|
|
|
|
@property
|
|
|
|
|
|
def pic_url(self) -> str:
|
|
|
|
|
|
"""提取 Slack 事件中的图片 URL"""
|
2025-05-10 18:04:58 +08:00
|
|
|
|
files = self.get('event', {}).get('files', [])
|
2025-03-30 12:53:48 -04:00
|
|
|
|
if files:
|
2025-05-10 18:04:58 +08:00
|
|
|
|
return files[0].get('url_private', '')
|
2025-03-30 22:24:53 -04:00
|
|
|
|
return None
|
2025-05-10 18:04:58 +08:00
|
|
|
|
|
2025-03-30 12:53:48 -04:00
|
|
|
|
@property
|
|
|
|
|
|
def sender_name(self) -> str:
|
2025-05-10 18:04:58 +08:00
|
|
|
|
return self.get('event', {}).get('user', '')
|
|
|
|
|
|
|
2025-03-30 12:53:48 -04:00
|
|
|
|
def __getattr__(self, key: str) -> Optional[Any]:
|
|
|
|
|
|
return self.get(key)
|
|
|
|
|
|
|
|
|
|
|
|
def __setattr__(self, key: str, value: Any) -> None:
|
|
|
|
|
|
self[key] = value
|
|
|
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
2025-05-10 18:04:58 +08:00
|
|
|
|
return f'<SlackEvent {super().__repr__()}>'
|