| 25 | |
| 26 | |
| 27 | class SlackWrapper(AbstractBaseEventIntegrationWrapper): |
| 28 | def __init__(self, api_token: str = None, channel_id: str = None): # type: ignore[assignment] |
| 29 | self.api_token = api_token |
| 30 | self.channel_id = channel_id |
| 31 | |
| 32 | def get_bot_token(self, code: str, redirect_uri: str) -> str: |
| 33 | oauth_response = self._client.oauth_v2_access( |
| 34 | client_id=settings.SLACK_CLIENT_ID, |
| 35 | client_secret=settings.SLACK_CLIENT_SECRET, |
| 36 | code=code, |
| 37 | redirect_uri=redirect_uri, |
| 38 | ) |
| 39 | return oauth_response.get("access_token") # type: ignore[no-untyped-call,no-any-return] |
| 40 | |
| 41 | def join_channel(self): # type: ignore[no-untyped-def] |
| 42 | try: |
| 43 | self._client.conversations_join(channel=self.channel_id) |
| 44 | except SlackApiError as e: |
| 45 | raise SlackChannelJoinError(e.response.get("error")) from e |
| 46 | |
| 47 | def get_channels_data(self, **kwargs) -> ChannelsDataResponse: # type: ignore[no-untyped-def] |
| 48 | """ |
| 49 | Returns non archived public channels. |
| 50 | """ |
| 51 | |
| 52 | response = self._client.conversations_list(exclude_archived=True, **kwargs) |
| 53 | channels = response["channels"] |
| 54 | channels = [ |
| 55 | SlackChannel(channel["name"], channel["id"]) for channel in channels |
| 56 | ] |
| 57 | cursor = response["response_metadata"]["next_cursor"] |
| 58 | return ChannelsDataResponse(channels=channels, cursor=cursor) |
| 59 | |
| 60 | @property |
| 61 | def _client(self) -> WebClient: |
| 62 | client = WebClient(token=self.api_token) |
| 63 | client.retry_handlers.append(RateLimitErrorRetryHandler(max_retry_count=3)) |
| 64 | return client |
| 65 | |
| 66 | @staticmethod |
| 67 | def generate_event_data(audit_log_record: AuditLog) -> dict: # type: ignore[type-arg] |
| 68 | log = audit_log_record.log |
| 69 | environment_name = audit_log_record.environment_name |
| 70 | email = audit_log_record.author_identifier |
| 71 | |
| 72 | return { |
| 73 | "blocks": [ |
| 74 | {"type": "section", "text": {"type": "plain_text", "text": log}}, |
| 75 | { |
| 76 | "type": "section", |
| 77 | "fields": [ |
| 78 | { |
| 79 | "type": "mrkdwn", |
| 80 | "text": f"*Environment:*\n{environment_name}", |
| 81 | }, |
| 82 | {"type": "mrkdwn", "text": f"*User:*\n{email}"}, |
| 83 | ], |
| 84 | }, |
no outgoing calls
searching dependent graphs…