-
Notifications
You must be signed in to change notification settings - Fork 4
Repair slack query #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| SlackGetChannelIdRequest, | ||
| SlackSendMessageRequest, | ||
| ) | ||
| from app.utils.levenshtein import get_most_similar_string | ||
|
|
||
| logging.basicConfig(level=logging.INFO) | ||
|
|
||
|
|
@@ -17,22 +18,39 @@ class SlackClient: | |
| def __init__(self, access_token: str): | ||
| self.client = AsyncWebClient(token=access_token) | ||
|
|
||
| async def get_all_channel_names(self) -> list[str]: | ||
| response = await self.client.conversations_list() | ||
| channels = response["channels"] | ||
| channel_names = [channel["name"] for channel in channels] | ||
| return channel_names | ||
|
|
||
| async def get_all_channel_ids( | ||
| self, request: SlackGetChannelIdRequest | ||
| ) -> list[dict[str, Any]]: | ||
| response = await self.client.conversations_list() | ||
| channels = response["channels"] | ||
| request_channel_names_set: set[str] = { | ||
| name.lower() for name in request.channel_names | ||
| } | ||
| request = await self._repair_channel_names(request=request) | ||
|
|
||
| channel_info = [ | ||
| {"channel_name": channel["name"], "channel_id": channel["id"]} | ||
| for channel in channels | ||
| if channel["name"].lower() | ||
| in request_channel_names_set # Slack channel names are always lower case | ||
| if channel["name"] in request.channel_names | ||
| ] | ||
| return channel_info | ||
|
|
||
| async def _repair_channel_names( | ||
| self, request: SlackGetChannelIdRequest | ||
| ) -> SlackGetChannelIdRequest: | ||
| possible_channel_names: list[str] = await self.get_all_channel_names() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: This call might be redundant if |
||
| updated_channel_names: list[str] = [] | ||
| for channel_name in request.channel_names: | ||
| updated_channel_names.append( | ||
| get_most_similar_string( | ||
| target=channel_name, candidates=possible_channel_names | ||
| ) | ||
| ) | ||
| return SlackGetChannelIdRequest(channel_names=updated_channel_names) | ||
|
|
||
| async def send_message(self, request: SlackSendMessageRequest): | ||
| response = await self.client.chat_postMessage( | ||
| channel=request.channel_id, text=request.text | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ def get_most_similar_string(target: str, candidates: list[str]) -> str: | |
| _process_string(candidate), _process_string(target) | ||
| ), | ||
| ) | ||
|
|
||
| # Return the original value if the similarity is less than the threshold | ||
| if ratio(most_similar, target) < THRESHOLD: | ||
| return None | ||
| return target | ||
|
Comment on lines
30
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: This change could lead to unexpected behavior in calling code that expects None for no match. Ensure all usages of this function are updated accordingly. |
||
| return most_similar | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: This comparison is now case-sensitive. Ensure this is intended behavior