def test_build_discord_payload(self):
        # Given
        hook = DiscordWebhookHook(**self._config)

        # When
        payload = hook._build_discord_payload()

        # Then
        self.assertEqual(self.expected_payload, payload)
Exemple #2
0
    def test_build_discord_payload(self):
        # Given
        hook = DiscordWebhookHook(**self._config)

        # When
        payload = hook._build_discord_payload()

        # Then
        self.assertEqual(self.expected_payload, payload)
Exemple #3
0
 def execute(self, context):
     """
     Call the DiscordWebhookHook to post message
     """
     self.hook = DiscordWebhookHook(self.http_conn_id,
                                    self.webhook_endpoint, self.message,
                                    self.username, self.avatar_url,
                                    self.tts, self.proxy)
     self.hook.execute()
Exemple #4
0
    def test_get_webhook_endpoint_manual_token(self):
        # Given
        provided_endpoint = 'webhooks/11111/some-discord-token_111'
        hook = DiscordWebhookHook(webhook_endpoint=provided_endpoint)

        # When
        webhook_endpoint = hook._get_webhook_endpoint(None, provided_endpoint)

        # Then
        self.assertEqual(webhook_endpoint, provided_endpoint)
    def test_get_webhook_endpoint_manual_token(self):
        # Given
        provided_endpoint = 'webhooks/11111/some-discord-token_111'
        hook = DiscordWebhookHook(webhook_endpoint=provided_endpoint)

        # When
        webhook_endpoint = hook._get_webhook_endpoint(None, provided_endpoint)

        # Then
        self.assertEqual(webhook_endpoint, provided_endpoint)
Exemple #6
0
    def test_get_webhook_endpoint_conn_id(self):
        # Given
        conn_id = 'default-discord-webhook'
        hook = DiscordWebhookHook(http_conn_id=conn_id)
        expected_webhook_endpoint = 'webhooks/00000/some-discord-token_000'

        # When
        webhook_endpoint = hook._get_webhook_endpoint(conn_id, None)

        # Then
        self.assertEqual(webhook_endpoint, expected_webhook_endpoint)
Exemple #7
0
    def test_build_discord_payload_message_length(self):
        # Given
        config = self._config.copy()
        # create message over the character limit
        config["message"] = 'c' * 2001
        hook = DiscordWebhookHook(**config)

        # When/Then
        expected_message = 'Discord message length must be 2000 or fewer characters'
        with self.assertRaisesRegex(AirflowException, expected_message):
            hook._build_discord_payload()
    def test_get_webhook_endpoint_conn_id(self):
        # Given
        conn_id = 'default-discord-webhook'
        hook = DiscordWebhookHook(http_conn_id=conn_id)
        expected_webhook_endpoint = 'webhooks/00000/some-discord-token_000'

        # When
        webhook_endpoint = hook._get_webhook_endpoint(conn_id, None)

        # Then
        self.assertEqual(webhook_endpoint, expected_webhook_endpoint)
    def test_build_discord_payload_message_length(self):
        # Given
        config = self._config.copy()
        # create message over the character limit
        config["message"] = 'c' * 2001
        hook = DiscordWebhookHook(**config)

        # When/Then
        expected_message = 'Discord message length must be 2000 or fewer characters'
        with self.assertRaisesRegexp(AirflowException, expected_message):
            hook._build_discord_payload()
Exemple #10
0
    def test_get_webhook_endpoint_invalid_url(self):
        # Given
        provided_endpoint = 'https://discordapp.com/some-invalid-webhook-url'

        # When/Then
        expected_message = 'Expected Discord webhook endpoint in the form of'
        with self.assertRaisesRegex(AirflowException, expected_message):
            DiscordWebhookHook(webhook_endpoint=provided_endpoint)
 def execute(self, context):
     """
     Call the DiscordWebhookHook to post message
     """
     self.hook = DiscordWebhookHook(
         self.http_conn_id,
         self.webhook_endpoint,
         self.message,
         self.username,
         self.avatar_url,
         self.tts,
         self.proxy
     )
     self.hook.execute()
Exemple #12
0
class DiscordWebhookOperator(SimpleHttpOperator):
    """
    This operator allows you to post messages to Discord using incoming webhooks.
    Takes a Discord connection ID with a default relative webhook endpoint. The
    default endpoint can be overridden using the webhook_endpoint parameter
    (https://discordapp.com/developers/docs/resources/webhook).

    Each Discord webhook can be pre-configured to use a specific username and
    avatar_url. You can override these defaults in this operator.

    :param http_conn_id: Http connection ID with host as "https://discord.com/api/" and
                         default webhook endpoint in the extra field in the form of
                         {"webhook_endpoint": "webhooks/{webhook.id}/{webhook.token}"}
    :type http_conn_id: str
    :param webhook_endpoint: Discord webhook endpoint in the form of
                             "webhooks/{webhook.id}/{webhook.token}"
    :type webhook_endpoint: str
    :param message: The message you want to send to your Discord channel
                    (max 2000 characters). (templated)
    :type message: str
    :param username: Override the default username of the webhook. (templated)
    :type username: str
    :param avatar_url: Override the default avatar of the webhook
    :type avatar_url: str
    :param tts: Is a text-to-speech message
    :type tts: bool
    :param proxy: Proxy to use to make the Discord webhook call
    :type proxy: str
    """

    template_fields = ['username', 'message']

    @apply_defaults
    def __init__(self,
                 http_conn_id=None,
                 webhook_endpoint=None,
                 message="",
                 username=None,
                 avatar_url=None,
                 tts=False,
                 proxy=None,
                 *args,
                 **kwargs):
        super().__init__(endpoint=webhook_endpoint, *args, **kwargs)

        if not http_conn_id:
            raise AirflowException('No valid Discord http_conn_id supplied.')

        self.http_conn_id = http_conn_id
        self.webhook_endpoint = webhook_endpoint
        self.message = message
        self.username = username
        self.avatar_url = avatar_url
        self.tts = tts
        self.proxy = proxy
        self.hook = None

    def execute(self, context):
        """
        Call the DiscordWebhookHook to post message
        """
        self.hook = DiscordWebhookHook(self.http_conn_id,
                                       self.webhook_endpoint, self.message,
                                       self.username, self.avatar_url,
                                       self.tts, self.proxy)
        self.hook.execute()
class DiscordWebhookOperator(SimpleHttpOperator):
    """
    This operator allows you to post messages to Discord using incoming webhooks.
    Takes a Discord connection ID with a default relative webhook endpoint. The
    default endpoint can be overridden using the webhook_endpoint parameter
    (https://discordapp.com/developers/docs/resources/webhook).

    Each Discord webhook can be pre-configured to use a specific username and
    avatar_url. You can override these defaults in this operator.

    :param http_conn_id: Http connection ID with host as "https://discord.com/api/" and
                         default webhook endpoint in the extra field in the form of
                         {"webhook_endpoint": "webhooks/{webhook.id}/{webhook.token}"}
    :type http_conn_id: str
    :param webhook_endpoint: Discord webhook endpoint in the form of
                             "webhooks/{webhook.id}/{webhook.token}"
    :type webhook_endpoint: str
    :param message: The message you want to send to your Discord channel
                    (max 2000 characters)
    :type message: str
    :param username: Override the default username of the webhook
    :type username: str
    :param avatar_url: Override the default avatar of the webhook
    :type avatar_url: str
    :param tts: Is a text-to-speech message
    :type tts: bool
    :param proxy: Proxy to use to make the Discord webhook call
    :type proxy: str
    """

    template_fields = ('username', 'message')

    @apply_defaults
    def __init__(self,
                 http_conn_id=None,
                 webhook_endpoint=None,
                 message="",
                 username=None,
                 avatar_url=None,
                 tts=False,
                 proxy=None,
                 *args,
                 **kwargs):
        super(DiscordWebhookOperator, self).__init__(endpoint=webhook_endpoint,
                                                     *args,
                                                     **kwargs)

        if not http_conn_id:
            raise AirflowException('No valid Discord http_conn_id supplied.')

        self.http_conn_id = http_conn_id
        self.webhook_endpoint = webhook_endpoint
        self.message = message
        self.username = username
        self.avatar_url = avatar_url
        self.tts = tts
        self.proxy = proxy
        self.hook = None

    def execute(self, context):
        """
        Call the DiscordWebhookHook to post message
        """
        self.hook = DiscordWebhookHook(
            self.http_conn_id,
            self.webhook_endpoint,
            self.message,
            self.username,
            self.avatar_url,
            self.tts,
            self.proxy
        )
        self.hook.execute()