Hi all! is it possible to post files to slack usin...
# announcements
c
Hi all! is it possible to post files to slack using dagster_slack? I have not figured out which library it is using under the hood
d
hey @Cris - we do this internally by sending a
BytesStream
to slack as a file:
Copy code
@solid(
    description="A solid to upload a buffered stream to Slack.",
    input_defs=[
        InputDefinition(
            name="stream", dagster_type=BytesStream, description="A buffered stream of a matplotlib figure."
        )
    ],
    output_defs=[OutputDefinition(name="slack_file", dagster_type=SlackFile)],
    config_schema={
        "channels": Field(
            config=Array(String),
            description="A list of channel names or IDs where the file will be shared.",
            is_required=True,
        ),
        "filename": Field(
            config=Noneable(String), description="Filename of the shared file.", is_required=False, default_value=None
        ),
        "filetype": Field(
            config=Noneable(String), description="Filetype of the shared file.", is_required=False, default_value=None
        ),
        "initial_comment": Field(
            config=Noneable(String),
            description="The message text introducing the file.",
            is_required=False,
            default_value=None,
        ),
        "title": Field(
            config=Noneable(String), description="Title of the shared file.", is_required=False, default_value=None
        ),
    },
    required_resource_keys={"slack"},
    tags={"kind": "slack"},
)
def send_stream_to_slack(context, stream: BytesStream):

    params = {k: v for k, v in context.solid_config.items() if v is not None}
    params.update({"channels": ",".join(context.solid_config["channels"])})
    resp = context.resources.slack.files_upload(file=stream, **params)

    if not resp["ok"]:
        raise Failure(description="Unsuccessful file upload to Slack.")

    slack_file = SlackFile.from_resp_object(resp["file"])
    yield AssetMaterialization(
        metadata_entries=[EventMetadataEntry.url(url=slack_file.permalink, label="Slack File")],
        asset_key=slack_file.permalink,
    )

    yield Output(value=slack_file, output_name="slack_file")
shows up in slack like this:
1
👍 1
worth noting that we spun up our own slack resource and a few custom dagster types to do this
(we use the python slackclient under the hood)
c
Thanks! this is really useful, you're using this library right https://github.com/slackapi/python-slackclient?
d
yep!