-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from satellite-no/post_attachment
added post_attachment action
- Loading branch information
Showing
7 changed files
with
118 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters