🚀 Feature: Create Discord sink with interactive button support#2039
🚀 Feature: Create Discord sink with interactive button support#2039letho1608 wants to merge 1 commit intorobusta-dev:masterfrom
Conversation
Implement a Discord sink that supports interactive buttons using Discord's bot API and callback mechanisms. This will provide the same functionality as Slack's interactive buttons. Affected files: discord_sink.py Signed-off-by: Le Quang Tho <92069270+letho1608@users.noreply.github.com>
WalkthroughThis change extends the Discord sink with interactive button event handling capabilities. The Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/robusta/core/sinks/discord/discord_sink.py`:
- Line 2: Remove the unused typing import by deleting "Dict" from the import
statement in discord_sink.py (leave "Any" if still needed); check the module for
any usages of "Dict" (none expected) and run linters to confirm the F401 warning
is resolved.
- Around line 30-41: handle_button_event currently calls
self.sender.handle_interaction unconditionally and register_interaction_handler
silently no-ops if the sender lacks methods; update handle_button_event to first
check hasattr(self.sender, "handle_interaction") and if missing log a clear
error and return False (or a suitable default) instead of letting AttributeError
be raised/caught, and update register_interaction_handler to check for
"register_interaction_handler" on self.sender and log a warning if absent so
missed wiring is visible; reference the methods handle_button_event and
register_interaction_handler in this file and the DiscordSender implementation
(DiscordSender) so maintainers know where to add the concrete handle_interaction
and register_interaction_handler implementations.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: de4c1e2f-c6ec-44c6-8870-bac203794836
📒 Files selected for processing (1)
src/robusta/core/sinks/discord/discord_sink.py
| @@ -1,4 +1,8 @@ | |||
| import logging | |||
| from typing import Dict, Any | |||
There was a problem hiding this comment.
Remove unused Dict import.
Dict is not used (Line 2), and this is already flagged by Flake8 (F401).
✂️ Proposed fix
-from typing import Dict, Any
+from typing import Any📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| from typing import Dict, Any | |
| from typing import Any |
🧰 Tools
🪛 Flake8 (7.3.0)
[error] 2-2: 'typing.Dict' imported but unused
(F401)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/robusta/core/sinks/discord/discord_sink.py` at line 2, Remove the unused
typing import by deleting "Dict" from the import statement in discord_sink.py
(leave "Any" if still needed); check the module for any usages of "Dict" (none
expected) and run linters to confirm the F401 warning is resolved.
| def handle_button_event(self, event: ButtonEvent) -> bool: | ||
| """Handle interactive button events from Discord.""" | ||
| try: | ||
| return self.sender.handle_interaction(event) | ||
| except Exception as e: | ||
| logging.error(f"Failed to handle Discord button event: {e}", exc_info=True) | ||
| return False | ||
|
|
||
| def register_interaction_handler(self, interaction_id: str, handler: Any): | ||
| """Register a handler for Discord interactions.""" | ||
| if hasattr(self.sender, 'register_interaction_handler'): | ||
| self.sender.register_interaction_handler(interaction_id, handler) |
There was a problem hiding this comment.
Interactive buttons are effectively broken due to missing sender contract.
On Line 33, self.sender.handle_interaction(...) is called unconditionally, but DiscordSender currently does not expose this method (src/robusta/integrations/discord/sender.py, Lines 91-313). This will raise AttributeError, be caught, and always return False.
Additionally, Lines 40-41 silently skip registration when register_interaction_handler is missing, so callbacks may never be wired.
🛠️ Proposed defensive fix in this file (while implementing sender methods separately)
def handle_button_event(self, event: ButtonEvent) -> bool:
"""Handle interactive button events from Discord."""
+ interaction_handler = getattr(self.sender, "handle_interaction", None)
+ if interaction_handler is None:
+ logging.error("DiscordSender.handle_interaction is not implemented")
+ return False
try:
- return self.sender.handle_interaction(event)
+ return interaction_handler(event)
except Exception as e:
logging.error(f"Failed to handle Discord button event: {e}", exc_info=True)
return False
def register_interaction_handler(self, interaction_id: str, handler: Any):
"""Register a handler for Discord interactions."""
- if hasattr(self.sender, 'register_interaction_handler'):
- self.sender.register_interaction_handler(interaction_id, handler)
+ registrar = getattr(self.sender, "register_interaction_handler", None)
+ if registrar is None:
+ logging.error(
+ "DiscordSender.register_interaction_handler is not implemented; "
+ "interaction '%s' was not registered",
+ interaction_id,
+ )
+ return
+ registrar(interaction_id, handler)You’ll still need to add handle_interaction and register_interaction_handler implementations in src/robusta/integrations/discord/sender.py for the feature to actually work end-to-end.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/robusta/core/sinks/discord/discord_sink.py` around lines 30 - 41,
handle_button_event currently calls self.sender.handle_interaction
unconditionally and register_interaction_handler silently no-ops if the sender
lacks methods; update handle_button_event to first check hasattr(self.sender,
"handle_interaction") and if missing log a clear error and return False (or a
suitable default) instead of letting AttributeError be raised/caught, and update
register_interaction_handler to check for "register_interaction_handler" on
self.sender and log a warning if absent so missed wiring is visible; reference
the methods handle_button_event and register_interaction_handler in this file
and the DiscordSender implementation (DiscordSender) so maintainers know where
to add the concrete handle_interaction and register_interaction_handler
implementations.
🚀 New Feature
Problem
Implement a Discord sink that supports interactive buttons using Discord's bot API and callback mechanisms. This will provide the same functionality as Slack's interactive buttons.
Severity:
highFile:
src/robusta/core/sinks/discord/discord_sink.pySolution
Create a new Discord sink implementation that:
Changes
src/robusta/core/sinks/discord/discord_sink.py(modified)Testing
Closes #1029