Skip to content

🚀 Feature: Create Discord sink with interactive button support#2039

Open
letho1608 wants to merge 1 commit intorobusta-dev:masterfrom
letho1608:contribai/feat/create-discord-sink-with-interactive-but
Open

🚀 Feature: Create Discord sink with interactive button support#2039
letho1608 wants to merge 1 commit intorobusta-dev:masterfrom
letho1608:contribai/feat/create-discord-sink-with-interactive-but

Conversation

@letho1608
Copy link
Copy Markdown

🚀 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: high
File: src/robusta/core/sinks/discord/discord_sink.py

Solution

Create a new Discord sink implementation that:

Changes

  • src/robusta/core/sinks/discord/discord_sink.py (modified)

Testing

  • Existing tests pass
  • Manual review completed
  • No new warnings/errors introduced

Closes #1029

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>
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 28, 2026

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 28, 2026

Walkthrough

This change extends the Discord sink with interactive button event handling capabilities. The DiscordSink class now extracts bot token and application ID from configuration, passes them to the sender, and introduces methods to handle button events and register interaction handlers, bringing Discord to feature parity with Slack for interactive alerts.

Changes

Cohort / File(s) Summary
Discord Interactive Button Support
src/robusta/core/sinks/discord/discord_sink.py
Added imports for button event handling. Modified __init__ to extract bot_token and application_id from sink config and pass them to DiscordSender. Introduced handle_button_event() method to process button interactions with error handling and logging. Introduced register_interaction_handler() method to conditionally register interaction callbacks.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly indicates the addition of interactive button support to the Discord sink, which aligns with the main objective of the changeset.
Description check ✅ Passed The description explains the problem being solved (adding interactive button support to Discord sink) and references the implementation file, clearly relating to the changeset.
Linked Issues check ✅ Passed The PR implements interactive button support for Discord sink as requested in issue #1029, enabling two-way communication through Discord's bot API to provide parity with Slack's interactive buttons.
Out of Scope Changes check ✅ Passed All changes are scoped to the Discord sink implementation file (discord_sink.py) and directly support the objective of adding interactive button functionality to Discord.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 14d6ba5 and dc419b1.

📒 Files selected for processing (1)
  • src/robusta/core/sinks/discord/discord_sink.py

@@ -1,4 +1,8 @@
import logging
from typing import Dict, Any
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

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.

Suggested change
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.

Comment on lines +30 to +41
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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Why only Slack have interactive buttons? (2 way communication)

2 participants