send_targets.py 1.7 KB

1234567891011121314151617181920212223242526272829303132
  1. """send_message declarations for the Slack ``PlatformEntry`` (kept out of the adapter module)."""
  2. import re
  3. from gateway.platforms.send_targets import StandaloneSendPolicy
  4. # Slack conversation IDs: C (public), G (private/group), D (DM); uppercase alnum, 9+ chars. User IDs
  5. # (U...) become ``user:U...`` and are opened as D... conversations first (posting straight to a U/W
  6. # id fails); ``@handle`` -> ``user_name:...`` resolves via users.list.
  7. _SEND_TARGET_RE = re.compile(r"^\s*([CGD][A-Z0-9]{8,})\s*$")
  8. _SEND_USER_ID_RE = re.compile(r"^\s*(U[A-Z0-9]{8,})\s*$")
  9. _SEND_USER_NAME_RE = re.compile(r"^\s*@([A-Za-z0-9._-]{1,80})\s*$")
  10. _SEND_MENTION_RE = re.compile(r"^\s*<@(U[A-Z0-9]{8,})(?:\|[^>]+)?>\s*$")
  11. # Session-derived Slack thread targets use "<conversation_id>:<thread_ts>".
  12. _SEND_THREAD_TARGET_RE = re.compile(r"^\s*([CGD][A-Z0-9]{8,}):([^\s:]+)\s*$")
  13. # (regex, chat_id template, thread comes from group 2) — thread form before bare id.
  14. _SEND_FORMS = ((_SEND_THREAD_TARGET_RE, "{}", True), (_SEND_TARGET_RE, "{}", False),
  15. (_SEND_USER_ID_RE, "user:{}", False), (_SEND_MENTION_RE, "user:{}", False),
  16. (_SEND_USER_NAME_RE, "user_name:{}", False))
  17. def _parse_send_target(ref):
  18. return next(((template.format(m.group(1)), m.group(2) if has_thread else None)
  19. for regex, template, has_thread in _SEND_FORMS if (m := regex.fullmatch(ref))), None)
  20. SEND_ENTRY_KWARGS = {
  21. "target_ref_syntax_fn": _parse_send_target,
  22. # Media goes through files_upload_v2 in the standalone sender.
  23. "standalone_send": StandaloneSendPolicy(label="Slack", media=True, captionable=True,
  24. empty_list_between_chunks=True),
  25. }