Exemple #1
0
    def test_build_slack_message(self):
        # Given
        hook = SlackWebhookHook(**self._config)

        # When
        message = hook._build_slack_message()

        # Then
        self.assertEqual(self.expected_message_dict, json.loads(message))
Exemple #2
0
    def test_get_token_manual_token(self):
        # Given
        manual_token = 'manual_token_here'
        hook = SlackWebhookHook(webhook_token=manual_token)

        # When
        webhook_token = hook._get_token(manual_token, None)

        # Then
        self.assertEqual(webhook_token, manual_token)
 def execute(self, context):
     """
     Call the SlackWebhookHook to post the provided Slack message
     """
     self.hook = SlackWebhookHook(self.http_conn_id, self.webhook_token,
                                  self.message, self.attachments,
                                  self.blocks, self.channel, self.username,
                                  self.icon_emoji, self.icon_url,
                                  self.link_names, self.proxy)
     self.hook.execute()
 def test_url_generated_by_http_conn_id_and_endpoint(self, mock_request, mock_session):
     hook = SlackWebhookHook(http_conn_id='slack-webhook-host', webhook_token='B000/XXX')
     try:
         hook.execute()
     except MissingSchema:
         pass
     mock_request.assert_called_once_with(
         self.expected_method, self.expected_url, headers=mock.ANY, data=mock.ANY
     )
     mock_request.reset_mock()
 def test_url_generated_by_endpoint(self, mock_request, mock_session):
     hook = SlackWebhookHook(webhook_token=self.expected_url)
     try:
         hook.execute()
     except MissingSchema:
         pass
     mock_request.assert_called_once_with(
         self.expected_method, self.expected_url, headers=mock.ANY, data=mock.ANY
     )
     mock_request.reset_mock()
Exemple #6
0
    def test_get_token_conn_id_password(self):
        # Given
        conn_id = 'slack-webhook-with-password'
        hook = SlackWebhookHook(http_conn_id=conn_id)
        expected_webhook_token = 'your_token_here'

        # When
        webhook_token = hook._get_token(None, conn_id)

        # Then
        self.assertEqual(webhook_token, expected_webhook_token)
    def test_get_token_conn_id(self):
        # Given
        conn_id = 'slack-webhook-default'
        hook = SlackWebhookHook(http_conn_id=conn_id)
        expected_webhook_token = 'your_token_here'

        # When
        webhook_token = hook._get_token(None, conn_id)

        # Then
        assert webhook_token == expected_webhook_token
Exemple #8
0
 def _get_slack_hook(self) -> SlackWebhookHook:
     return SlackWebhookHook(
         http_conn_id=self.slack_conn_id,
         message=self.slack_message,
         channel=self.slack_channel,
         webhook_token=self.slack_webhook_token,
     )
class SlackWebhookOperator(SimpleHttpOperator):
    """
    This operator allows you to post messages to Slack using incoming webhooks.
    Takes both Slack webhook token directly and connection that has Slack webhook token.
    If both supplied, http_conn_id will be used as base_url,
    and webhook_token will be taken as endpoint, the relative path of the url.

    Each Slack webhook token can be pre-configured to use a specific channel, username and
    icon. You can override these defaults in this hook.

    :param http_conn_id: connection that has Slack webhook token in the extra field
    :type http_conn_id: str
    :param webhook_token: Slack webhook token
    :type webhook_token: str
    :param message: The message you want to send on Slack
    :type message: str
    :param attachments: The attachments to send on Slack. Should be a list of
        dictionaries representing Slack attachments.
    :type attachments: list
    :param blocks: The blocks to send on Slack. Should be a list of
        dictionaries representing Slack blocks.
    :type blocks: list
    :param channel: The channel the message should be posted to
    :type channel: str
    :param username: The username to post to slack with
    :type username: str
    :param icon_emoji: The emoji to use as icon for the user posting to Slack
    :type icon_emoji: str
    :param icon_url: The icon image URL string to use in place of the default icon.
    :type icon_url: str
    :param link_names: Whether or not to find and link channel and usernames in your
        message
    :type link_names: bool
    :param proxy: Proxy to use to make the Slack webhook call
    :type proxy: str
    """

    template_fields = [
        'webhook_token',
        'message',
        'attachments',
        'blocks',
        'channel',
        'username',
        'proxy',
    ]

    # pylint: disable=too-many-arguments
    @apply_defaults
    def __init__(
        self,
        *,
        http_conn_id: str,
        webhook_token: Optional[str] = None,
        message: str = "",
        attachments: Optional[list] = None,
        blocks: Optional[list] = None,
        channel: Optional[str] = None,
        username: Optional[str] = None,
        icon_emoji: Optional[str] = None,
        icon_url: Optional[str] = None,
        link_names: bool = False,
        proxy: Optional[str] = None,
        **kwargs,
    ) -> None:
        super().__init__(endpoint=webhook_token, **kwargs)
        self.http_conn_id = http_conn_id
        self.webhook_token = webhook_token
        self.message = message
        self.attachments = attachments
        self.blocks = blocks
        self.channel = channel
        self.username = username
        self.icon_emoji = icon_emoji
        self.icon_url = icon_url
        self.link_names = link_names
        self.proxy = proxy
        self.hook: Optional[SlackWebhookHook] = None

    def execute(self, context: Dict[str, Any]) -> None:
        """Call the SlackWebhookHook to post the provided Slack message"""
        self.hook = SlackWebhookHook(
            self.http_conn_id,
            self.webhook_token,
            self.message,
            self.attachments,
            self.blocks,
            self.channel,
            self.username,
            self.icon_emoji,
            self.icon_url,
            self.link_names,
            self.proxy,
        )
        self.hook.execute()