Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a SlackAPIFileOperator to send files on Slack #8980

Closed
mikaeld opened this issue May 22, 2020 · 3 comments
Closed

Add a SlackAPIFileOperator to send files on Slack #8980

mikaeld opened this issue May 22, 2020 · 3 comments
Labels
kind:feature Feature Requests

Comments

@mikaeld
Copy link
Contributor

mikaeld commented May 22, 2020

Description

airflow.operators.slack_operator only has the SlackAPIPostOperator implementing 'chat.postMessage' Slack API method. I created an Operator for the 'files.upload' API method for a project. Here's the code. It uses the same structure as SlackAPIPostOperator, inheriting from SlackAPIOperator.

Use case / motivation

Being able to send files to Slack using the API. Webhooks do not allow for files.

Related Issues

Code could be improved to be more generic. Open to suggestions!

from airflow.utils.decorators import apply_defaults
from airflow.operators.slack_operator import SlackAPIOperator


class SlackAPIFileOperator(SlackAPIOperator):
    """
    Send a file to a slack channel

    :param channel: channel in which to sent file on slack name (#general) or
        ID (C12318391). (templated)
    :type channel: str
    :param initial_comment: message to send to slack. (templated)
    :type initial_comment: str
    :param filename: name of the file
    :type filename: str
    :param filetype: slack filetype. (templated)
        - see https://api.slack.com/types/file
    :type filetype: str
    :param content: file content. (templated)
    :type content: str
    """

    template_fields = ('channel', 'initial_comment', 'filename', 'filetype', 'content')
    ui_color = '#44BEDF'

    @apply_defaults
    def __init__(self,
                 channel='#general',
                 initial_comment='No message has been set!',
                 filename='default_name.csv',
                 filetype='csv',
                 content='default,content,csv,file',
                 *args, **kwargs):
        self.method = 'files.upload'
        self.channel = channel
        self.initial_comment = initial_comment
        self.filename = filename
        self.filetype = filetype
        self.content = content
        super(SlackAPIFileOperator, self).__init__(method=self.method, *args, **kwargs)

    def construct_api_call_params(self):
        self.api_params = {
            'channels': self.channel,
            'content': self.content,
            'filename': self.filename,
            'filetype': self.filetype,
            'initial_comment': self.initial_comment
        }
@mikaeld mikaeld added the kind:feature Feature Requests label May 22, 2020
@boring-cyborg
Copy link

boring-cyborg bot commented May 22, 2020

Thanks for opening your first issue here! Be sure to follow the issue template!

@mik-laj
Copy link
Member

mik-laj commented May 23, 2020

Can you open PR? You don't need to create a isue if you want to share the code. The issue is only needed if it is a long-term task or you want someone else to do the task (communication tool)

@mikaeld
Copy link
Contributor Author

mikaeld commented May 25, 2020

Got it! PR #9004

@mikaeld mikaeld closed this as completed May 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:feature Feature Requests
Projects
None yet
Development

No branches or pull requests

2 participants