Skip to content

Commit

Permalink
Enrich messages with replies
Browse files Browse the repository at this point in the history
  • Loading branch information
Raj725 committed Sep 24, 2024
1 parent 9db8115 commit e10361c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
19 changes: 18 additions & 1 deletion libs/community/langchain_community/document_loaders/slack_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,30 @@ def _convert_message_to_document(
Returns:
Document: A Document object representing the message.
"""
text = message.get("text", "")
text = self._enriched_message_text(message)
metadata = self._get_message_metadata(message, channel_name)
return Document(
page_content=text,
metadata=metadata,
)

def _enriched_message_text(self, message: dict) -> str:
"""
Enrich the message text with replies in the thread.
Args:
message (dict): The message to enrich.
Returns:
str: The enriched message text.
"""
replies = message.get("replies", [])
if not replies:
return message.get("text", "")
# Get text from each reply(First reply is the original message)
reply_texts = [reply.get("text", "") for reply in replies]
return "\n\n".join(reply_texts)

def _get_message_metadata(self, message: dict, channel_name: str) -> dict:
"""Create and return metadata for a given message and channel."""
timestamp = message.get("ts", "")
Expand Down
11 changes: 10 additions & 1 deletion libs/community/langchain_community/utilities/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,18 @@ def get_user_details_map(self) -> Dict[str, Dict[str, str]]:
def get_messages(self, channel: str, limit: int = 1000) -> Any:
"""Get messages from a channel."""
try:
messages = self.slack_client.conversations_history(
response = self.slack_client.conversations_history(
channel=channel, limit=limit
)
messages = response.get("messages", [])

# Include replies to message in thread if available
for message in messages:
if message.get("thread_ts"):
thread_response = self.slack_client.conversations_replies(
channel=channel, ts=message["ts"]
)
message["replies"] = thread_response.get("messages", [])
return messages
except Exception as e:
logger.error(f"Error getting messages from Slack: {e}")
Expand Down

0 comments on commit e10361c

Please sign in to comment.