Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
- goduncan <https://duncan.sh>
- wbengston
- dmart
- viveksyngh <[email protected]>
- sgedward
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ HubCommander also features the ability to:
You can add additional commands by creating plugins. For example, you can create a plugin to invite users
to your organizations.

HubCommander also supports Slack ephemeral messages and threads.


Installation Documentation
-----------
Please see the documentation [here](docs/installation.md) for details.
Expand Down
26 changes: 11 additions & 15 deletions bot_components/slack_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
WORKING_COLOR = "#439FE0"


def say(channel, attachments, text=None, ephemeral=False, ephemeral_user=None, thread=None):
def say(channel, attachments, text=None, ephemeral_user=None, thread=None):
"""
Sends a message (with attachments) to Slack. Use the send_* methods instead.
:param channel:
:param attachments:
:param text:
:param ephemeral: If True, then send send ephemeral message
:param ephemeral_user:ID of the user who will receive the ephemeral message
:param thread:
:return:
Expand All @@ -33,7 +32,7 @@ def say(channel, attachments, text=None, ephemeral=False, ephemeral_user=None, t
}
verb = "chat.postMessage"

if ephemeral:
if ephemeral_user:
kwargs_to_send["user"] = ephemeral_user
verb = "chat.postEphemeral"

Expand All @@ -43,13 +42,12 @@ def say(channel, attachments, text=None, ephemeral=False, ephemeral_user=None, t
bot_components.SLACK_CLIENT.api_call(verb, **kwargs_to_send)


def send_error(channel, text, markdown=False, ephemeral=False, ephemeral_user=None, thread=None):
def send_error(channel, text, markdown=False, ephemeral_user=None, thread=None):
"""
Sends an "error" message to Slack.
:param channel:
:param text:
:param markdown: If True, then look for markdown in the message.
:param ephemeral: True to send ephemeral message
:param ephemeral_user:ID of the user who will receive the ephemeral message
:param thread:
:return:
Expand All @@ -62,16 +60,15 @@ def send_error(channel, text, markdown=False, ephemeral=False, ephemeral_user=No
if markdown:
attachment["mrkdwn_in"] = ["text"]

say(channel, [attachment], ephemeral=ephemeral, ephemeral_user=ephemeral_user, thread=thread)
say(channel, [attachment], ephemeral_user=ephemeral_user, thread=thread)


def send_info(channel, text, markdown=False, ephemeral=False, ephemeral_user=None, thread=None):
def send_info(channel, text, markdown=False, ephemeral_user=None, thread=None):
"""
Sends an "info" message to Slack.
:param channel:
:param text:
:param markdown: If True, then look for markdown in the message.
:param ephemeral: True to send ephemeral mesaage
:param ephemeral_user:ID of the user who will receive the ephemeral message
:param thread:
:return:
Expand All @@ -84,16 +81,15 @@ def send_info(channel, text, markdown=False, ephemeral=False, ephemeral_user=Non
if markdown:
attachment["mrkdwn_in"] = ["text"]

say(channel, [attachment], ephemeral=ephemeral, ephemeral_user=ephemeral_user, thread=thread)
say(channel, [attachment], ephemeral_user=ephemeral_user, thread=thread)


def send_success(channel, text, markdown=False, ephemeral=False, ephemeral_user=None, thread=None):
def send_success(channel, text, markdown=False, ephemeral_user=None, thread=None):
"""
Sends an "success" message to Slack.
:param channel:
:param text:
:param markdown: If True, then look for markdown in the message.
:param ephemeral: True to send ephemeral message
:param ephemeral_user:ID of the user who will receive the ephemeral message
:param thread:
:return:
Expand All @@ -106,20 +102,20 @@ def send_success(channel, text, markdown=False, ephemeral=False, ephemeral_user=
if markdown:
attachment["mrkdwn_in"] = ["text"]

say(channel, [attachment], ephemeral=ephemeral, ephemeral_user=ephemeral_user, thread=thread)
say(channel, [attachment], ephemeral_user=ephemeral_user, thread=thread)


def send_raw(channel, text, ephemeral=False, ephemeral_user=None, thread=None):
def send_raw(channel, text, ephemeral_user=None, thread=None):
"""
Sends an "info" message to Slack.
:param channel:
:param text:
:param ephemeral: True to send ephemeral message
:param ephemeral_user:ID of the user who will receive the ephemeral message
:param thread:
:return:
"""

say(channel, None, text, ephemeral=ephemeral, ephemeral_user=ephemeral_user, thread=thread)
say(channel, None, text, ephemeral_user=ephemeral_user, thread=thread)


def get_user_data(data):
Expand Down
2 changes: 1 addition & 1 deletion command_plugins/repeat/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def setup(self, *args):
)
def repeat_command(self, data, user_data, text):
new_text = data['text'].split(' ', 1)[1]
send_info(data["channel"], new_text, markdown=True, ephemeral=True, ephemeral_user=user_data["id"])
send_info(data["channel"], new_text, markdown=True, ephemeral_user=user_data["id"])

@hubcommander_command(
name="!RepeatThread",
Expand Down
13 changes: 13 additions & 0 deletions docs/making_plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ respectively.
These functions take in as parameters the Slack channel to post to (from `data["channel"]` in your command method),
the `text you want to be displayed in the channel`, and whether or not `markdown` should be rendered.

These functions also support threads and ephemeral messages.

#### Threads
For sending messages within Slack threads, Slack requires a timestamp to be sent over. HubCommander provides this in the
`data` dictionary's `ts` value that gets passed into each command function. To use this, in your `send_*` function call, simply
pass in `thread=data["ts"]`. An example of this in action is [here](https://github.com/Netflix/hubcommander/blob/develop/command_plugins/repeat/plugin.py#L65).

#### Ephemeral Messages
Sending ephemeral messages is similar to threads. For this, your command must receive the `user_data` dictionary that gets passed into
each command function. To send the ephemeral message to the user, you need to pass into the `send_*` function call
`ephemeral_user=user_data["id"]`. An example of this in action is [here](https://github.com/Netflix/hubcommander/blob/develop/command_plugins/repeat/plugin.py#L53).


### Add Command Authentication
To add authentication, you simply decorate the method with `@auth` (after the `@hubcommander_command()` decorator)

Expand Down