Slack was really easy to get working. I am testing...
# ask-community
y
Slack was really easy to get working. I am testing slack with something like this: Assets
Copy code
@asset
def hello_world(context, slack: SlackResource) -> None:
    <http://context.log.info|context.log.info>("start hello_world")

    slack.get_client().chat_postMessage(
        channel="#test_private_channel",
        text="Hello, from Dagster! :dagster:",
    )

    # ... do stuff ....
    <http://context.log.info|context.log.info>("After hello_world")
___init___.py
Copy code
def slack_token():
    # Parse the environment variable
    secret = json.loads(os.getenv('dagsterSecret'))

    # Extract connection parameters
    slack_token = secret['slack_api_token']

    return slack_token


defs = Definitions(
    assets=all_assets,
    # resources=resources_by_deployment_name[deployment_name],
    # schedules=[core_assets_schedule],
    # sensors=all_sensors,
    resources={
        "slack": SlackResource(token=slack_token()),
    },
)
I would like to not have to set the slack token or put a garbage token for dev env. But if I do that, I get errors. Do you have any suggestions on handling this so I only use a valid Slack token on staging and prod? I am leaning toward a Slack WebClient intercepter that does the "magic" I am after, but I wanted to check if there is something better/easier first.
s
In the past I've used a mock resource for this, eg
Copy code
class MockSlackResource(SlackResource):
    def chat_postMessage(self, channel, text, unfurl_links):
        msg = f"""
            THIS IS A MOCK SLACK MESSAGE
            Channel: {channel}
            Message: {text}
            Unfurl Links Bool: {unfurl_links}
        """
        print(msg)
Then in definitions
Copy code
dev_resources = {"slack": MockSlackResource(token="fake")}
prod_resources = {"slack": SlackResource(token=EnvVar("real"))}
resources = prod_resources if os.getenv("PROD") else dev_resources
y
Thanks I will try that
@Sean Lopp I guess you mean you would mock both SlackResource and Webclient since
chat_postMessage
is not a method of
SlackResource