Changeset View
Changeset View
Standalone View
Standalone View
python_modules/libraries/dagster-slack/dagster_slack/resources.py
from slackclient import SlackClient | import sys | ||||
from dagster import Field, StringSource, resource, seven | from dagster import Field, StringSource, resource | ||||
class SlackConnection: | class SlackConnection: | ||||
DAGSTER_ICON_URL = "https://user-images.githubusercontent.com/609349/57993619-9ff2ee00-7a6e-11e9-9184-b3414e3eeb30.png" | |||||
DAGSTER_SLACK_USERNAME = "dagsterbot" | |||||
sashank: Does this actually work generically? Or does it only work in our workspace? I have a feeling… | |||||
Done Inline ActionsAh, I just kept it since it was in the previous message arguments. But looks like icon_url can't be specified anyways with the newer tokens so I'll just remove it. rexledesma: Ah, I just kept it since it was in the previous message arguments. But looks like `icon_url`… | |||||
def __init__(self, token): | def __init__(self, token): | ||||
self.token = token | # Conditional import of slack, since sdk was rewritten for py3 | ||||
self.sc = SlackClient(self.token) | # pylint:disable=import-error | ||||
if sys.version_info.major >= 3: | |||||
from slack import WebClient | |||||
else: | |||||
from slackclient import SlackClient as WebClient | |||||
self._token = token | |||||
self.sc = WebClient(self._token) | |||||
class _Chat: | class _Chat: | ||||
@classmethod | @classmethod | ||||
def post_message( | def post_message(cls, channel, text, **kwargs): | ||||
Not Done Inline Actionswe should drop this whole wrapper class and just expose the client max: we should drop this whole wrapper class and just expose the client | |||||
Done Inline Actionsrexledesma: whynot | |||||
cls, | |||||
channel="#noise", | |||||
username="dagsterbot", | |||||
text="Hello from Dagster!", | |||||
# pylint: disable=line-too-long | |||||
icon_url="https://user-images.githubusercontent.com/609349/57993619-9ff2ee00-7a6e-11e9-9184-b3414e3eeb30.png", | |||||
attachments=None, | |||||
): | |||||
"""slack_resource.chat.post_message() : chat.postMessage | """slack_resource.chat.post_message() : chat.postMessage | ||||
See https://api.slack.com/methods/chat.postMessage | See https://api.slack.com/methods/chat.postMessage | ||||
""" | """ | ||||
api_params = { | api_params = { | ||||
"channel": channel, | "channel": channel, | ||||
"username": username, | |||||
"text": text, | "text": text, | ||||
"icon_url": icon_url, | "icon_url": SlackConnection.DAGSTER_ICON_URL, | ||||
"attachments": seven.json.dumps(attachments), | "username": SlackConnection.DAGSTER_SLACK_USERNAME, | ||||
} | } | ||||
if sys.version_info.major >= 3: | |||||
api_params.update(kwargs) | |||||
return self.sc.chat_postMessage(**api_params) | |||||
else: | |||||
return self.sc.api_call("chat.postMessage", **api_params) | return self.sc.api_call("chat.postMessage", **api_params) | ||||
self.chat = _Chat | self.chat = _Chat | ||||
def api_call(self, method, timeout=None, **kwargs): | def api_call(self, api_method, **kwargs): | ||||
return self.sc.api_call(method, timeout, **kwargs) | """ slack_resource.api_call() | ||||
See https://slack.dev/python-slackclient/basic_usage.html#calling-any-api-methods | |||||
""" | |||||
if sys.version_info.major >= 3: | |||||
return self.sc.api_call(api_method, json=kwargs) | |||||
else: | |||||
return self.sc.api_call(api_method, **kwargs) | |||||
@resource( | @resource( | ||||
{ | { | ||||
"token": Field( | "token": Field( | ||||
StringSource, | StringSource, | ||||
description="""To configure access to the Slack API, you'll need an access | description="""To configure access to the Slack API, you'll need an access | ||||
token provisioned with access to your Slack workspace. | token provisioned with access to your Slack workspace. | ||||
▲ Show 20 Lines • Show All 41 Lines • Show Last 20 Lines |
Does this actually work generically? Or does it only work in our workspace? I have a feeling that you can't arbitrarily specify usernames and icons like this, and it only works in our workspace because we actually have a dagsterbot installed.