Skip to content

Commit

Permalink
Merge pull request #17 from Point72/tkp/conf
Browse files Browse the repository at this point in the history
Allow app and bot tokens to be in file
  • Loading branch information
timkpaine authored Dec 3, 2024
2 parents 550e914 + 83b771f commit 9213615
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
20 changes: 6 additions & 14 deletions csp_adapter_slack/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def _get_user_from_id(self, user_id):
if ret.status_code == 200:
# TODO OAuth scopes required
name = ret.data["user"]["profile"].get("real_name_normalized", ret.data["user"]["name"])
email = ret.data["user"]["profile"]["email"]
email = ret.data["user"]["profile"].get("email", "")
self._user_id_to_user_name[user_id] = name
self._user_name_to_user_id[name] = user_id # TODO is this 1-1 in slack?
self._user_id_to_user_email[user_id] = email
Expand Down Expand Up @@ -197,14 +197,8 @@ def _get_channel_from_name(self, channel_name):
return self._room_name_to_room_id.get(channel_name, None)
return channel_id

def _get_tags_from_message(self, blocks, authorizations=None) -> List[str]:
def _get_tags_from_message(self, blocks) -> List[str]:
"""extract tags from message, potentially excluding the bot's own @"""
authorizations = authorizations or []
if len(authorizations) > 0:
bot_id = authorizations[0]["user_id"] # TODO more than one?
else:
bot_id = ""

tags = []
to_search = blocks.copy()

Expand All @@ -216,11 +210,9 @@ def _get_tags_from_message(self, blocks, authorizations=None) -> List[str]:

if element.get("type", "") == "user":
tag_id = element.get("user_id")
if tag_id != bot_id:
# TODO tag with id or with name?
name, _ = self._get_user_from_id(tag_id)
if name:
tags.append(name)
name, _ = self._get_user_from_id(tag_id)
if name:
tags.append(name)
return tags

def _process_slack_message(self, client: SocketModeClient, req: SocketModeRequest):
Expand All @@ -233,7 +225,7 @@ def _process_slack_message(self, client: SocketModeClient, req: SocketModeReques
if req.payload["event"]["type"] in ("message", "app_mention") and req.payload["event"].get("subtype") is None:
user, user_email = self._get_user_from_id(req.payload["event"]["user"])
channel, channel_type = self._get_channel_from_id(req.payload["event"]["channel"])
tags = self._get_tags_from_message(req.payload["event"]["blocks"], req.payload["authorizations"])
tags = self._get_tags_from_message(req.payload["event"]["blocks"])
slack_msg = SlackMessage(
user=user or "",
user_email=user_email or "",
Expand Down
15 changes: 11 additions & 4 deletions csp_adapter_slack/adapter_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
from ssl import SSLContext
from typing import Optional

Expand All @@ -15,13 +16,19 @@ class SlackAdapterConfig(BaseModel):

@field_validator("app_token")
def validate_app_token(cls, v):
assert v.startswith("xapp-"), "App token must start with 'xapp-'"
return v
if v.startswith("xapp-"):
return v
elif Path(v).exists():
return Path(v).read_text().strip()
raise ValueError("App token must start with 'xoxb-' or be a file path")

@field_validator("bot_token")
def validate_bot_token(cls, v):
assert v.startswith("xoxb-"), "Bot token must start with 'xoxb-'"
return v
if v.startswith("xoxb-"):
return v
elif Path(v).exists():
return Path(v).read_text().strip()
raise ValueError("Bot token must start with 'xoxb-' or be a file path")

@field_validator("ssl")
def validate_ssl(cls, v):
Expand Down

0 comments on commit 9213615

Please sign in to comment.