Skip to content

Commit

Permalink
Merge pull request #77 from satellite-no/post_attachment
Browse files Browse the repository at this point in the history
added post_attachment action
  • Loading branch information
amanda11 authored Apr 14, 2022
2 parents ec94e61 + f09034d commit 3a19c5c
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

# 2.2.0

* Added action `post_attachment`
* Added missing `icon_url` to config schema

# 2.1.0

* Upgrade lxml dependency from 3.8.0 to 4.6.5
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Pack which allows integration with [Slack](https://slack.com/) service.
## Configuration

Copy the example configuration in [`slack.yaml.example`](./slack.yaml.example)
to `/opt/stackstorm/configs/slack.yaml` and edit as required.
to `/opt/stackstorm/configs/slack.yaml` and edit as required.
Note: Actions ``post_message`` and ``post_attachment`` use the same ``post_message_action`` configuration.

* ``post_message_action.webhook_url`` - Webhook URL.
* ``post_message_action.channel`` - Channel to send the message to (e.g.
Expand All @@ -17,6 +18,10 @@ to `/opt/stackstorm/configs/slack.yaml` and edit as required.
messages will be posted. This setting can be overridden on per action basis.
If not provided, default value which is selected when configuring a webhook
is used.
* ``post_message_action.icon_url`` - Default icon url of the user under which the
messages will be posted. This setting can be overridden on per action basis.
If not provided, default value which is selected when configuring a webhook
is used.
* ``sensor.token`` - Authentication token used to authenticate against Real
Time Messaging API.
* ``sensor.strip_formatting`` - By default, Slack automatically parses URLs, images,
Expand Down Expand Up @@ -99,9 +104,10 @@ Example trigger payload:

## Actions

The following two actions are provided by the Slack pack.
The following actions are provided by the Slack pack.

* ``post_message`` - Post a message to the specified channel using an incoming webhook.
* ``post_attachment`` - Post an attachment to the specified channel using an incoming webhook.
* ``users.admin.invite`` - Send an invitation to join a Slack Org.
* ``users_filter_by`` - List users in a Slack team matching certain creterias.

Expand Down
63 changes: 63 additions & 0 deletions actions/post_attachment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import json

from six.moves.urllib.parse import urlencode
import requests

from st2common.runners.base_action import Action

__all__ = [
'PostMessageAction'
]


class PostAttachmentAction(Action):
def run(self, attachment, username=None, icon_emoji=None, icon_url=None, channel=None,
disable_formatting=False, webhook_url=None):
config = self.config.get('post_message_action', {})
username = username if username else config['username']
icon_emoji = icon_emoji if icon_emoji else config.get('icon_emoji', None)
icon_url = icon_url if icon_url else config.get('icon_url', None)
channel = channel if channel else config.get('channel', None)
webhook_url = webhook_url if webhook_url else config.get('webhook_url', None)

if not webhook_url:
raise ValueError('"webhook_url" needs to be either provided via '
'action parameter or specified as part of '
'post_message_action.webhook_url config option"')

headers = {}
headers['Content-Type'] = 'application/x-www-form-urlencoded'
body = {
'username': username,
'attachments': [attachment]
}

if icon_emoji:
body['icon_emoji'] = icon_emoji

if icon_url:
body['icon_url'] = icon_url

if channel:
body['channel'] = channel

if disable_formatting:
body['parse'] = 'none'

if webhook_url:
body['webhook_url'] = webhook_url

data = {'payload': json.dumps(body)}
data = urlencode(data)
response = requests.post(url=webhook_url,
headers=headers, data=data)

if response.status_code == requests.codes.ok: # pylint: disable=no-member
self.logger.info('Message successfully posted')
else:
failure_reason = ('Failed to post message: %s (status code: %s)' %
(response.text, response.status_code))
self.logger.exception(failure_reason)
raise Exception(failure_reason)

return True
36 changes: 36 additions & 0 deletions actions/post_attachment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
name: "post_attachment"
runner_type: "python-script"
description: "Post an attachment to the Slack channel."
enabled: true
entry_point: "post_attachment.py"
parameters:
attachment:
type: "object"
description: "Attachment to send (only one allowed)"
required: true
username:
type: "string"
description: "Bot username."
required: false
channel:
type: "string"
description: "Optional channel to post to. Note channel must contain leading #"
required: false
icon_emoji:
type: "string"
description: "Bot icon emoji"
required: false
icon_url:
type: "string"
description: "Bot icon URL"
required: false
disable_formatting:
type: "boolean"
description: "Disable formatting, don't parse the message and treat it as raw text"
required: false
default: false
webhook_url:
type: "string"
description: "Optional Webhook url"
required: false
4 changes: 4 additions & 0 deletions config.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
type: "string"
description: "Default icon of user under which messages will be posted"
required: false
icon_url:
type: "string"
description: "Default icon url of user under which messages will be posted"
required: false
sensor:
description: "Sensor specific settings."
type: "object"
Expand Down
2 changes: 1 addition & 1 deletion pack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ keywords:
- chat
- messaging
- instant messaging
version: 2.1.0
version: 2.2.0
python_versions:
- "3"
author : StackStorm, Inc.
Expand Down
1 change: 1 addition & 0 deletions slack.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ post_message_action:
channel: "#mychannel"
username: "my-bot"
icon_emoji: ":panda_face:"
icon_url: "https://myiconurl.com"

# Used for Slack sensor
sensor:
Expand Down

0 comments on commit 3a19c5c

Please sign in to comment.