Skip to content
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

Uses settings to control ephemeral state. #120

Merged
merged 11 commits into from
Mar 2, 2022
4 changes: 4 additions & 0 deletions nautobot_chatops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class NautobotChatOpsConfig(PluginConfig):
# Mattermost-specific settings
"mattermost_api_token": None,
"mattermost_url": None,
# As requested on https://github.com/nautobot/nautobot-plugin-chatops/issues/114 this setting is used for
# sending all messages as an emphemeral message, meaning only the person interacting with the bot will see the
# responses.
"send_all_messages_emphemeral": False,
jvanderaa marked this conversation as resolved.
Show resolved Hide resolved
}

max_version = "1.999"
Expand Down
9 changes: 8 additions & 1 deletion nautobot_chatops/dispatchers/adaptive_cards.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Dispatcher subclass for chat platforms that use Adaptive Cards (https://adaptivecards.io/)."""
from django.conf import settings

from .base import Dispatcher

Expand Down Expand Up @@ -121,7 +122,13 @@ def multi_input_dialog(self, command, sub_command, dialog_title, dialog_list):
],
}
blocks.append(buttons)
return self.send_blocks(blocks, callback_id=callback_id, modal=True, ephemeral=False, title=dialog_title)
return self.send_blocks(
blocks,
callback_id=callback_id,
modal=True,
ephemeral=settings.PLUGINS_CONFIG["nautobot_chatops"]["send_all_messages_emphemeral"],
title=dialog_title,
)

def send_warning(self, message):
"""Send a warning message to the user/channel specified by the context."""
Expand Down
15 changes: 13 additions & 2 deletions nautobot_chatops/dispatchers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,22 @@ def ask_permission_to_send_image(self, filename, action_id):

# Send various content to the user or channel

def send_markdown(self, message, ephemeral=False):
def send_markdown(
self,
message,
ephemeral=settings.PLUGINS_CONFIG["nautobot_chatops"]["send_all_messages_emphemeral"],
):
"""Send a Markdown-formatted text message to the user/channel specified by the context."""
raise NotImplementedError

def send_blocks(self, blocks, callback_id=None, modal=False, ephemeral=False, title=None):
def send_blocks(
self,
blocks,
callback_id=None,
modal=False,
ephemeral=settings.PLUGINS_CONFIG["nautobot_chatops"]["send_all_messages_emphemeral"],
title=None,
):
"""Send a series of formatting blocks to the user/channel specified by the context."""
raise NotImplementedError

Expand Down
23 changes: 20 additions & 3 deletions nautobot_chatops/dispatchers/mattermost.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,11 @@ def command_response_header(self, command, subcommand, args, description="inform
# Send various content to the user or channel

@BACKEND_ACTION_MARKDOWN.time()
def send_markdown(self, message, ephemeral=False):
def send_markdown(
self,
message,
ephemeral=settings.PLUGINS_CONFIG["nautobot_chatops"]["send_all_messages_emphemeral"],
):
"""Send a Markdown-formatted text message to the user/channel specified by the context."""
try:
if ephemeral:
Expand All @@ -360,7 +364,14 @@ def send_markdown(self, message, ephemeral=False):

# pylint: disable=arguments-differ
@BACKEND_ACTION_BLOCKS.time()
def send_blocks(self, blocks, callback_id=None, modal=False, ephemeral=False, title="Your attention please!"):
def send_blocks(
self,
blocks,
callback_id=None,
modal=False,
ephemeral=settings.PLUGINS_CONFIG["nautobot_chatops"]["send_all_messages_emphemeral"],
title="Your attention please!",
):
"""Send a series of formatting blocks to the user/channel specified by the context.

Args:
Expand Down Expand Up @@ -461,7 +472,13 @@ def prompt_for_text(self, action_id, help_text, label, title="Your attention ple
}
blocks = [textentry]
# In Mattermost, a textentry element can ONLY be sent in a modal Interactive dialog
return self.send_blocks(blocks, callback_id=action_id, ephemeral=False, modal=True, title=title)
return self.send_blocks(
blocks,
callback_id=action_id,
ephemeral=settings.PLUGINS_CONFIG["nautobot_chatops"]["send_all_messages_emphemeral"],
modal=True,
title=title,
)

def prompt_from_menu(
self, action_id, help_text, choices, default=(None, None), confirm=False, offset=0
Expand Down
13 changes: 11 additions & 2 deletions nautobot_chatops/dispatchers/ms_teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,21 @@ def ask_permission_to_send_image(self, filename, action_id):
)

@BACKEND_ACTION_MARKDOWN.time()
def send_markdown(self, message, ephemeral=False):
def send_markdown(
self, message, ephemeral=settings.PLUGINS_CONFIG["nautobot_chatops"]["send_all_messages_emphemeral"]
):
"""Send a markdown-formatted text message to the user/channel specified by the context."""
self._send({"text": message, "textFormat": "markdown"})

@BACKEND_ACTION_BLOCKS.time()
def send_blocks(self, blocks, callback_id=None, modal=False, ephemeral=False, title=None):
def send_blocks(
self,
blocks,
callback_id=None,
modal=False,
ephemeral=settings.PLUGINS_CONFIG["nautobot_chatops"]["send_all_messages_emphemeral"],
title=None,
):
"""Send a series of formatting blocks to the user/channel specified by the context."""
if title and title not in str(blocks[0]):
blocks.insert(0, self.markdown_element(self.bold(title)))
Expand Down
21 changes: 18 additions & 3 deletions nautobot_chatops/dispatchers/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ def command_response_header(self, command, subcommand, args, description="inform
# Send various content to the user or channel

@BACKEND_ACTION_MARKDOWN.time()
def send_markdown(self, message, ephemeral=False):
def send_markdown(
self, message, ephemeral=settings.PLUGINS_CONFIG["nautobot_chatops"]["send_all_messages_emphemeral"]
):
"""Send a Markdown-formatted text message to the user/channel specified by the context."""
try:
if ephemeral:
Expand All @@ -156,7 +158,14 @@ def send_markdown(self, message, ephemeral=False):

# pylint: disable=arguments-differ
@BACKEND_ACTION_BLOCKS.time()
def send_blocks(self, blocks, callback_id=None, modal=False, ephemeral=False, title="Your attention please!"):
def send_blocks(
self,
blocks,
callback_id=None,
modal=False,
ephemeral=settings.PLUGINS_CONFIG["nautobot_chatops"]["send_all_messages_emphemeral"],
title="Your attention please!",
):
"""Send a series of formatting blocks to the user/channel specified by the context.

Slack distinguishes between simple inline interactive elements and modal dialogs. Modals can contain multiple
Expand Down Expand Up @@ -400,7 +409,13 @@ def multi_input_dialog(self, command, sub_command, dialog_title, dialog_list):

blocks.append(self._input_block(action_id, dialog["label"], textentry, dialog.get("optional", False)))

return self.send_blocks(blocks, callback_id=callback_id, modal=True, ephemeral=False, title=dialog_title)
return self.send_blocks(
blocks,
callback_id=callback_id,
modal=True,
ephemeral=settings.PLUGINS_CONFIG["nautobot_chatops"]["send_all_messages_emphemeral"],
title=dialog_title,
)

def user_mention(self):
"""Markup for a mention of the username/userid specified in our context."""
Expand Down
13 changes: 11 additions & 2 deletions nautobot_chatops/dispatchers/webex.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,21 @@ def platform_lookup(cls, item_type, item_name):
return None

@BACKEND_ACTION_MARKDOWN.time()
def send_markdown(self, message, ephemeral=False):
def send_markdown(
self, message, ephemeral=settings.PLUGINS_CONFIG["nautobot_chatops"]["send_all_messages_emphemeral"]
):
"""Send a markdown-formatted text message to the user/channel specified by the context."""
self.client.messages.create(roomId=self.context["channel_id"], markdown=message)

@BACKEND_ACTION_BLOCKS.time()
def send_blocks(self, blocks, callback_id=None, modal=False, ephemeral=False, title=None):
def send_blocks(
self,
blocks,
callback_id=None,
modal=False,
ephemeral=settings.PLUGINS_CONFIG["nautobot_chatops"]["send_all_messages_emphemeral"],
title=None,
):
"""Send a series of formatting blocks to the user/channel specified by the context."""
if title and title not in str(blocks[0]):
blocks.insert(0, self.markdown_element(self.bold(title)))
Expand Down