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()
def test_build_discord_payload(self): # Given hook = DiscordWebhookHook(**self._config) # When payload = hook._build_discord_payload() # Then self.assertEqual(self.expected_payload, payload)
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_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.assertRaisesRegex(AirflowException, expected_message): hook._build_discord_payload()
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)
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}"} :param webhook_endpoint: Discord webhook endpoint in the form of "webhooks/{webhook.id}/{webhook.token}" :param message: The message you want to send to your Discord channel (max 2000 characters). (templated) :param username: Override the default username of the webhook. (templated) :param avatar_url: Override the default avatar of the webhook :param tts: Is a text-to-speech message :param proxy: Proxy to use to make the Discord webhook call """ template_fields: Sequence[str] = ('username', 'message') def __init__( self, *, http_conn_id: Optional[str] = None, webhook_endpoint: Optional[str] = None, message: str = "", username: Optional[str] = None, avatar_url: Optional[str] = None, tts: bool = False, proxy: Optional[str] = None, **kwargs, ) -> None: super().__init__(endpoint=webhook_endpoint, **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: Optional[DiscordWebhookHook] = None def execute(self, context: 'Context') -> None: """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()