-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sweep: Augment on_ticket so that when a user adds a slack link, we au…
…tomatically unroll the thread and extract the information (#3668) # Description This pull request introduces enhancements to the `on_ticket` handler within the SweepAI application. It augments the existing functionality by automatically unrolling Slack threads when a Slack link is included in a ticket summary. The extracted conversation from the Slack thread is then appended to the ticket summary, providing additional context and information directly within the ticket. # Summary - Added `SLACK_API_KEY` environment variable to `sweepai/config/server.py` for Slack integration. - Imported `WebClient` and `SlackApiError` from `slack_sdk` in `sweepai/handlers/on_ticket.py` to enable communication with the Slack API. - Implemented a new feature in `on_ticket` that: - Detects a Slack link in the ticket summary. - Uses the Slack API to fetch the permalink data and retrieve the thread replies. - Appends the text of the Slack thread messages to the ticket summary. - Handles potential `SlackApiError` exceptions and logs errors accordingly. - The changes ensure that relevant Slack conversations are automatically included in the ticket details, improving the ticket resolution process. Fixes #3656. --- <details> <summary><b>🎉 Latest improvements to Sweep:</b></summary> <ul> <li>New <a href="https://progress.sweep.dev">dashboard</a> launched for real-time tracking of Sweep issues, covering all stages from search to coding.</li> <li>Integration of OpenAI's latest Assistant API for more efficient and reliable code planning and editing, improving speed by 3x.</li> <li>Use the <a href="https://marketplace.visualstudio.com/items?itemName=GitHub.vscode-pull-request-github">GitHub issues extension</a> for creating Sweep issues directly from your editor.</li> </ul> </details> --- ### 💡 To get Sweep to edit this pull request, you can: * Comment below, and Sweep can edit the entire PR * Comment on a file, Sweep will only modify the commented file * Edit the original issue to get Sweep to recreate the PR from scratch *This is an automated message generated by [Sweep AI](https://sweep.dev).* --------- Co-authored-by: sweep-nightly[bot] <131841235+sweep-nightly[bot]@users.noreply.github.com> Co-authored-by: wwzeng1 <[email protected]>
- Loading branch information
1 parent
2b9073a
commit d9dbd22
Showing
5 changed files
with
71 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import re | ||
|
||
from loguru import logger | ||
from slack_sdk import WebClient | ||
from slack_sdk.errors import SlackApiError | ||
|
||
from sweepai.config.server import SLACK_API_KEY | ||
|
||
def get_thread_by_thread_ts(client: WebClient, channel_id: int, thread_ts: int): | ||
response = client.conversations_replies( | ||
channel=channel_id, | ||
ts=thread_ts | ||
) | ||
|
||
if response["ok"]: | ||
return response["messages"] | ||
else: | ||
print(f"Error fetching thread: {response['error']}") | ||
return None | ||
|
||
def add_slack_context(summary) -> str: | ||
result = "" | ||
if not SLACK_API_KEY: | ||
return result | ||
slack_link_match = re.search(r'(https://[\w-]+\.slack\.com/archives/\w+/p\d+)', summary) | ||
if not slack_link_match: | ||
return result | ||
slack_link = slack_link_match.group(1) | ||
slack_client = WebClient(token=SLACK_API_KEY) | ||
try: | ||
# Extract channel and message_ts from the Slack link | ||
link_parts = slack_link.split('/') | ||
slack_channel_id = link_parts[-2] | ||
slack_message_ts = link_parts[-1][1:] # Remove the 'p' prefix | ||
# you need to add a dot six places from the right | ||
slack_message_ts = f"{slack_message_ts[:-6]}.{slack_message_ts[-6:]}" | ||
# Fetch the message object | ||
thread_messages = get_thread_by_thread_ts(client=slack_client, channel_id=slack_channel_id, thread_ts=slack_message_ts) | ||
if len(thread_messages) > 0: | ||
result += f"\n\nThe following slack thread was attached to {slack_link}:\n<slack_thread>\n" | ||
for idx, thread_message in enumerate(thread_messages): | ||
result += f"Message {idx}: {thread_message['text']}\n" if "text" in thread_message else "" | ||
result += "</slack_thread>" | ||
except SlackApiError as e: | ||
logger.error(f"Error fetching Slack message or thread: {e}") | ||
return result | ||
|
||
if __name__ == "__main__": | ||
url = "" | ||
result = add_slack_context(url) | ||
print(result) |