-
-
Notifications
You must be signed in to change notification settings - Fork 647
feat(slack): add BotInteraction model and reaction feedback handler #5035
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
Open
mohityadav8
wants to merge
13
commits into
OWASP:main
Choose a base branch
from
mohityadav8:feat/nestbot-bot-interaction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
baa224b
feat(slack): add BotInteraction model and reaction feedback handler
mohityadav8 df760cb
test: add missing __init__.py for reaction_added tests
mohityadav8 f074326
fix(slack): address CodeRabbit review feedback
mohityadav8 598990e
Update __init__.py
mohityadav8 0a61447
Remove comment from BotInteraction migration file
mohityadav8 a117981
chore: rerun CI
mohityadav8 bd8d3d3
Update message_auto_reply_test.py
mohityadav8 4b111c4
Add assertions for bot interaction creation in tests
mohityadav8 4c9813a
style: apply pre-commit fixes
mohityadav8 7332a52
Update backend/tests/unit/apps/slack/services/message_auto_reply_test.py
mohityadav8 e09d8cb
chore: rerun CI
mohityadav8 0dc4d10
style: remove trailing whitespace
mohityadav8 55516ac
Merge branch 'main' into feat/nestbot-bot-interaction
mohityadav8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,87 @@ | ||
| """Bot interaction admin configuration.""" | ||
|
|
||
| from django.contrib import admin | ||
|
|
||
| from apps.slack.models.bot_interaction import BotInteraction | ||
|
|
||
|
|
||
| class BotInteractionAdmin(admin.ModelAdmin): | ||
| """Admin for BotInteraction model.""" | ||
|
|
||
| fieldsets = ( | ||
| ( | ||
| "Interaction", | ||
| { | ||
| "fields": ( | ||
| "channel_id", | ||
| "user_id", | ||
| "user_message", | ||
| "bot_response", | ||
| ) | ||
| }, | ||
| ), | ||
| ( | ||
| "Classification", | ||
| { | ||
| "fields": ( | ||
| "intent_category", | ||
| "confidence_score", | ||
| "tokens_used", | ||
| ) | ||
| }, | ||
| ), | ||
| ( | ||
| "Feedback", | ||
| { | ||
| "fields": ( | ||
| "thumbs_up", | ||
| "slack_reply_ts", | ||
| ) | ||
| }, | ||
| ), | ||
| ) | ||
| list_display = ( | ||
| "channel_id", | ||
| "user_id", | ||
| "short_message", | ||
| "intent_category", | ||
| "thumbs_up", | ||
| "tokens_used", | ||
| "nest_created_at", | ||
| ) | ||
| list_filter = ( | ||
| "thumbs_up", | ||
| "intent_category", | ||
| ) | ||
| readonly_fields = ( | ||
| "channel_id", | ||
| "user_id", | ||
| "user_message", | ||
| "bot_response", | ||
| "slack_reply_ts", | ||
| "tokens_used", | ||
| "nest_created_at", | ||
| "nest_updated_at", | ||
| ) | ||
| search_fields = ( | ||
| "channel_id", | ||
| "user_id", | ||
| "user_message", | ||
| "intent_category", | ||
| ) | ||
|
|
||
| MESSAGE_TRUNCATE_LENGTH = 60 | ||
|
|
||
| @admin.display(description="Message") | ||
| def short_message(self, obj): | ||
| """Return truncated user message for list display.""" | ||
| if not obj.user_message: | ||
| return "" | ||
| return ( | ||
| obj.user_message[: self.MESSAGE_TRUNCATE_LENGTH] + "…" | ||
| if len(obj.user_message) > self.MESSAGE_TRUNCATE_LENGTH | ||
| else obj.user_message | ||
| ) | ||
|
|
||
|
|
||
| admin.site.register(BotInteraction, BotInteractionAdmin) |
This file contains hidden or 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 hidden or 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,22 @@ | ||
| def configure_slack_events(): | ||
| """Configure Slack events after Django apps are ready.""" | ||
| from apps.slack.apps import SlackConfig | ||
| from apps.slack.events import ( | ||
| app_home_opened, | ||
| app_mention, | ||
| message_posted, | ||
| team_join, | ||
| url_verification, | ||
| ) | ||
| from apps.slack.events.event import EventBase | ||
| from apps.slack.events.member_joined_channel import ( | ||
| catch_all, | ||
| contribute, | ||
| gsoc, | ||
| owasp_community, | ||
| project_nest, | ||
| ) | ||
| from apps.slack.events.reaction_added import bot_feedback # noqa: F401 ← ADD THIS LINE | ||
|
|
||
| if SlackConfig.app: | ||
| EventBase.configure_events() |
This file contains hidden or 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 @@ | ||
| """Slack reaction_added event handlers.""" |
This file contains hidden or 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,66 @@ | ||
| """Slack reaction_added event handler for bot reply feedback.""" | ||
|
|
||
| import logging | ||
|
|
||
| from apps.slack.events.event import EventBase | ||
| from apps.slack.models.bot_interaction import BotInteraction | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| THUMBS_UP = "thumbsup" | ||
| THUMBS_DOWN = "thumbsdown" | ||
| FEEDBACK_REACTIONS = {THUMBS_UP, THUMBS_DOWN} | ||
|
|
||
|
|
||
| class BotFeedback(EventBase): | ||
| """Records 👍 / 👎 reactions on NestBot replies.""" | ||
|
|
||
| event_type = "reaction_added" | ||
|
|
||
| def handle_event(self, event, client): | ||
| """Handle a reaction_added event. | ||
|
|
||
| Checks whether the reacted-to message ts matches a BotInteraction | ||
| slack_reply_ts. If so, records the feedback. Reactions on non-bot | ||
| messages are silently ignored. | ||
|
|
||
| Args: | ||
| event (dict): The Slack reaction_added event payload. | ||
| client: The Slack WebClient instance (unused but required by base). | ||
|
|
||
| """ | ||
| reaction = event.get("reaction", "") | ||
| if reaction not in FEEDBACK_REACTIONS: | ||
| return | ||
|
|
||
| item = event.get("item", {}) | ||
| if item.get("type") != "message": | ||
| return | ||
|
|
||
| reply_ts = item.get("ts", "") | ||
| if not reply_ts: | ||
| return | ||
|
|
||
| channel_id = item.get("channel", "") | ||
| if not channel_id: | ||
| return | ||
|
|
||
| interaction = ( | ||
| BotInteraction.objects.filter( | ||
| slack_reply_ts=reply_ts, | ||
| channel_id=channel_id, | ||
| ) | ||
| .order_by("-nest_created_at") | ||
| .first() | ||
| ) | ||
| if interaction is None: | ||
| return | ||
|
|
||
| interaction.thumbs_up = reaction == THUMBS_UP | ||
| interaction.save(update_fields=["thumbs_up", "nest_updated_at"]) | ||
|
|
||
| logger.info( | ||
| "Feedback recorded for BotInteraction pk=%s: %s", | ||
| interaction.pk, | ||
| "👍" if interaction.thumbs_up else "👎", | ||
| ) |
This file contains hidden or 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,79 @@ | ||
| # Generated migration for BotInteraction model | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("slack", "0022_workspace_invite_link_last_alert_message_ts_and_more"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="BotInteraction", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.BigAutoField( | ||
| auto_created=True, | ||
| primary_key=True, | ||
| serialize=False, | ||
| verbose_name="ID", | ||
| ), | ||
| ), | ||
| ("nest_created_at", models.DateTimeField(auto_now_add=True)), | ||
| ("nest_updated_at", models.DateTimeField(auto_now=True)), | ||
| ( | ||
| "channel_id", | ||
| models.CharField(max_length=64, verbose_name="Channel ID"), | ||
| ), | ||
| ( | ||
| "user_id", | ||
| models.CharField(max_length=64, verbose_name="User ID"), | ||
| ), | ||
| ("user_message", models.TextField(verbose_name="User message")), | ||
| ("bot_response", models.TextField(verbose_name="Bot response")), | ||
| ( | ||
| "intent_category", | ||
| models.CharField( | ||
| blank=True, | ||
| default="", | ||
| max_length=64, | ||
| verbose_name="Intent category", | ||
| ), | ||
| ), | ||
| ( | ||
| "confidence_score", | ||
| models.FloatField(blank=True, null=True, verbose_name="Confidence score"), | ||
| ), | ||
| ( | ||
| "thumbs_up", | ||
| models.BooleanField( | ||
| blank=True, | ||
| help_text="True = 👍, False = 👎, None = no reaction yet.", | ||
| null=True, | ||
| verbose_name="Thumbs up", | ||
| ), | ||
| ), | ||
| ( | ||
| "tokens_used", | ||
| models.PositiveIntegerField(default=0, verbose_name="Tokens used"), | ||
| ), | ||
| ( | ||
| "slack_reply_ts", | ||
| models.CharField( | ||
| blank=True, | ||
| db_index=True, | ||
| default="", | ||
| help_text="Slack message ts of the bot reply. Used to match reaction_added events.", | ||
| max_length=32, | ||
| verbose_name="Slack reply timestamp", | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "verbose_name_plural": "Bot Interactions", | ||
| "db_table": "slack_bot_interactions", | ||
| }, | ||
| ), | ||
| ] | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| from .bot_interaction import BotInteraction | ||
| from .conversation import Conversation | ||
| from .event import Event | ||
| from .member import Member | ||
| from .message import Message | ||
| from .workspace import Workspace | ||
| from .workspace import Workspace |
This file contains hidden or 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,64 @@ | ||
| """Slack app bot interaction model.""" | ||
|
|
||
| from django.db import models | ||
|
|
||
| from apps.common.models import TimestampedModel | ||
|
|
||
|
|
||
| class BotInteraction(TimestampedModel): | ||
| """Tracks AI-generated replies and their user feedback.""" | ||
|
|
||
| class Meta: | ||
| """Model options.""" | ||
|
|
||
| db_table = "slack_bot_interactions" | ||
| verbose_name_plural = "Bot Interactions" | ||
|
|
||
| channel_id = models.CharField( | ||
| verbose_name="Channel ID", | ||
| max_length=64, | ||
| ) | ||
| user_id = models.CharField( | ||
| verbose_name="User ID", | ||
| max_length=64, | ||
| ) | ||
| user_message = models.TextField( | ||
| verbose_name="User message", | ||
| ) | ||
| bot_response = models.TextField( | ||
| verbose_name="Bot response", | ||
| ) | ||
| intent_category = models.CharField( | ||
| verbose_name="Intent category", | ||
| max_length=64, | ||
| blank=True, | ||
| default="", | ||
| ) | ||
| confidence_score = models.FloatField( | ||
| verbose_name="Confidence score", | ||
| null=True, | ||
| blank=True, | ||
| ) | ||
| thumbs_up = models.BooleanField( | ||
| verbose_name="Thumbs up", | ||
| null=True, | ||
| blank=True, | ||
| help_text="True = 👍, False = 👎, None = no reaction yet.", | ||
| ) | ||
| tokens_used = models.PositiveIntegerField( | ||
| verbose_name="Tokens used", | ||
| default=0, | ||
| ) | ||
| slack_reply_ts = models.CharField( | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| verbose_name="Slack reply timestamp", | ||
| max_length=32, | ||
| blank=True, | ||
| default="", | ||
| db_index=True, | ||
| help_text="Slack message ts of the bot reply. Used to match reaction_added events.", | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| def __str__(self): | ||
| """Human readable representation.""" | ||
| feedback = {True: "👍", False: "👎", None: "—"}[self.thumbs_up] | ||
| return f"[{self.channel_id}] {self.user_message[:50]!r} → {feedback}" | ||
This file contains hidden or 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
1 change: 1 addition & 0 deletions
1
backend/tests/unit/apps/slack/events/reaction_added/__init__.py
This file contains hidden or 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 @@ | ||
| """Tests for reaction_added events.""" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
P2:
slack_reply_tsshould not be optional because it is the key used to attach 👍/👎 feedback. Allowing empty values stores interactions that can never be correlated back from reactions.Prompt for AI agents