mikaeld opened a new issue #8980:
URL: https://github.com/apache/airflow/issues/8980


   **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!
   
   ```python
   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)
           - see https://api.slack.com/reference/block-kit/blocks.
       :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
           }
   ```
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to