def slack_webhook_hook_demo(): slack = SlackWebhookHook(http_conn_id='slack', message='Hello, world! (SlackWebhookHook)', username='******', icon_emoji=':cat:', link_names=True) slack.execute()
def test_build_slack_message(self): # Given hook = SlackWebhookHook(**self._config) # When message = hook._build_slack_message() # Then self.assertEqual(self.expected_message, message)
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.channel, self.username, self.icon_emoji, self.link_names, self.proxy) self.hook.execute()
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 test_url_generated_by_endpoint(self, request_mock): hook = SlackWebhookHook(webhook_token=self.expected_url) try: hook.execute() except MissingSchema: pass request_mock.assert_called_once_with(self.expected_method, self.expected_url, headers=mock.ANY, data=mock.ANY) request_mock.reset_mock()
def test_url_generated_by_http_conn_id(self, request_mock): hook = SlackWebhookHook(http_conn_id='slack-webhook-url') try: hook.execute() except MissingSchema: pass request_mock.assert_called_once_with(self.expected_method, self.expected_url, headers=mock.ANY, data=mock.ANY) request_mock.reset_mock()
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 self.assertEqual(webhook_token, expected_webhook_token)
def test_url_generated_by_http_conn_id_and_endpoint( self, request_mock, session_mock): hook = SlackWebhookHook(http_conn_id='slack-webhook-host', webhook_token='B000/XXX') try: hook.execute() except MissingSchema: pass request_mock.assert_called_once_with(self.expected_method, self.expected_url, headers=mock.ANY, data=mock.ANY) request_mock.reset_mock()
def test_url_generated_by_endpoint(self, request_mock): hook = SlackWebhookHook(webhook_token=self.expected_url) try: hook.execute() except MissingSchema: pass request_mock.assert_called_once_with( self.expected_method, self.expected_url, headers=mock.ANY, data=mock.ANY ) request_mock.reset_mock()
def test_url_generated_by_http_conn_id(self, request_mock): hook = SlackWebhookHook(http_conn_id='slack-webhook-url') try: hook.execute() except MissingSchema: pass request_mock.assert_called_once_with( self.expected_method, self.expected_url, headers=mock.ANY, data=mock.ANY ) request_mock.reset_mock()
def notify_error(context): SlackWebhookHook( http_conn_id='slack_playing_ml', channel='ml', icon_emoji=':cat:', message=''' Task: {task_id} failed '''.format( task_id=context.get('task_instance').task_id)).execute()
def slack_alert(context, success=False): if not config.SLACK_TOKEN: logger.info("No Slack token, skipping Slack notification") return dag_text = context['dag'].dag_id ts = context["ts"] title = "DAG run {}".format("succeeded" if success else "failed") if not success: failed_task_logs_url = (context['dag_run'].get_task_instances( state=State.FAILED)[0].log_url) failed_task_name = (context['dag_run'].get_task_instances( state=State.FAILED)[0].task_id) dag_text = f"<{failed_task_logs_url}|{dag_text}.{failed_task_name}>" return SlackWebhookHook( webhook_token=config.SLACK_TOKEN, attachments=[{ "color": "#007000" if success else "#D2222D", "fallback": title, "blocks": [{ "type": "section", "text": { "text": title, "type": "mrkdwn" }, "fields": [ { "type": "mrkdwn", "text": "*DAG*" }, { "type": "mrkdwn", "text": "*Run*" }, { "type": "mrkdwn", "text": dag_text }, { "type": "plain_text", "text": ts }, ], }], }], ).execute()
def execute(self, context): """ Call the SparkSqlHook to run the provided sql query """ self.hook = SlackWebhookHook( self.http_conn_id, self.webhook_token, self.message, self.channel, self.username, self.icon_emoji, self.link_names, self.proxy ) self.hook.execute()
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.channel, self.username, self.icon_emoji, self.link_names, self.proxy ) self.hook.execute()
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 :param extra_options: Extra options for http hook :type extra_options: dict """ template_fields = [ 'webhook_token', 'message', 'attachments', 'blocks', 'channel', 'username', 'proxy', 'extra_options', ] @apply_defaults def __init__(self, http_conn_id=None, webhook_token=None, message="", attachments=None, blocks=None, channel=None, username=None, icon_emoji=None, icon_url=None, link_names=False, extra_options=None, proxy=None, *args, **kwargs): super(SlackWebhookOperator, self).__init__(endpoint=webhook_token, *args, **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 = None self.extra_options = extra_options 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.extra_options) self.hook.execute()
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, Slack webhook token will be used. 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 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 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 """ @apply_defaults def __init__(self, http_conn_id=None, webhook_token=None, message="", channel=None, username=None, icon_emoji=None, link_names=False, proxy=None, *args, **kwargs): super(SlackWebhookOperator, self).__init__(endpoint=webhook_token, *args, **kwargs) self.http_conn_id = http_conn_id self.webhook_token = webhook_token self.message = message self.channel = channel self.username = username self.icon_emoji = icon_emoji self.link_names = link_names self.proxy = proxy self.hook = None 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.channel, self.username, self.icon_emoji, self.link_names, self.proxy) self.hook.execute()
def notify_success(context): SlackWebhookHook(http_conn_id='slack_playing_ml', message=''' Task: {task_id} successed '''.format( task_id=context.get('task_instance').task_id), channel='ml', icon_emoji=':dog:').execute()
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, Slack webhook token will be used. 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 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 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 """ @apply_defaults def __init__(self, http_conn_id=None, webhook_token=None, message="", attachments=None, channel=None, username=None, icon_emoji=None, link_names=False, proxy=None, *args, **kwargs): super(SlackWebhookOperator, self).__init__(endpoint=webhook_token, *args, **kwargs) self.http_conn_id = http_conn_id self.webhook_token = webhook_token self.message = message self.attachments = attachments self.channel = channel self.username = username self.icon_emoji = icon_emoji self.link_names = link_names self.proxy = proxy self.hook = None 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.channel, self.username, self.icon_emoji, self.link_names, self.proxy ) self.hook.execute()