Last active
September 5, 2025 03:56
-
-
Save kameshsampath/5bb62faa7de83ffdb951d13a6d2b57bf to your computer and use it in GitHub Desktop.
the slack tool snippet to use with Snowflake Intelligence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Step 1 | |
| CREATE OR REPLACE SECRET alerts_and_notification.slack_channel_aln_webhook_secret | |
| TYPE = generic_string | |
| -- https://hooks.slack.com/services/YYYYYYYY/XXXXXXX | |
| -- Your Slack Webhook Token is /YYYYYYYY/XXXXXXX | |
| SECRET_STRING = 'YOUR_SLACK_WEBHOOK_TOKEN_HERE' | |
| COMMENT = 'Webhook for alerts-and-notifications slack channel'; | |
| -- Step 2 | |
| CREATE OR REPLACE NOTIFICATION INTEGRATION notify_on_aln_channel | |
| TYPE = webhook | |
| ENABLED = true | |
| WEBHOOK_URL = 'https://hooks.slack.com/services/SNOWFLAKE_WEBHOOK_SECRET' | |
| WEBHOOK_SECRET = alerts_and_notification.slack_channel_aln_webhook_secret | |
| WEBHOOK_BODY_TEMPLATE = 'SNOWFLAKE_WEBHOOK_MESSAGE' | |
| WEBHOOK_HEADERS = ('Content-Type'='application/json'); | |
| -- Step 3 | |
| CREATE OR REPLACE PROCEDURE alerts_and_notification.send_slack_notification( | |
| body VARCHAR | |
| ) | |
| RETURNS VARCHAR | |
| LANGUAGE python | |
| RUNTIME_VERSION = '3.12' | |
| PACKAGES = ('snowflake-snowpark-python') | |
| HANDLER = 'send_to_slack' | |
| AS | |
| $$ | |
| import json | |
| import re | |
| def send_to_slack(session, body): | |
| try: | |
| # Parse and validate JSON | |
| try: | |
| parsed_json = json.loads(body) | |
| except json.JSONDecodeError as json_err: | |
| return f"Invalid JSON format: {str(json_err)}" | |
| # Build the complete Slack payload object in Python | |
| if 'blocks' in parsed_json: | |
| slack_payload = parsed_json # Already has blocks structure | |
| else: | |
| slack_payload = {"blocks": parsed_json} # Wrap in blocks object | |
| # Convert to clean JSON string with proper Unicode handling | |
| final_json = json.dumps(slack_payload, ensure_ascii=True, separators=(',', ':')) | |
| # Additional escaping for SQL string literals | |
| def escape_for_sql(json_str): | |
| # Escape single quotes for SQL | |
| json_str = json_str.replace("'", "''") | |
| # Double escape backslashes that are already escaped | |
| # This handles \n, \t, \r, \", \\, \u sequences | |
| json_str = json_str.replace('\\n', '\\\\n') # \n -> \\n | |
| json_str = json_str.replace('\\t', '\\\\t') # \t -> \\t | |
| json_str = json_str.replace('\\r', '\\\\r') # \r -> \\r | |
| # Handle Unicode escapes \uXXXX -> \\uXXXX | |
| json_str = re.sub(r'\\u([0-9a-fA-F]{4})', r'\\\\u\1', json_str) | |
| return json_str | |
| escaped_json = escape_for_sql(final_json) | |
| # Send notification | |
| session.sql(f""" | |
| CALL SYSTEM$SEND_SNOWFLAKE_NOTIFICATION( | |
| SNOWFLAKE.NOTIFICATION.APPLICATION_JSON(SNOWFLAKE.NOTIFICATION.SANITIZE_WEBHOOK_CONTENT('{escaped_json}')), | |
| SNOWFLAKE.NOTIFICATION.INTEGRATION('notify_on_aln_channel') | |
| ) | |
| """).collect() | |
| return "Slack message sent successfully" | |
| except Exception as e: | |
| return f"Error sending slack notification: {str(e)}" | |
| $$; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT | |
| id, | |
| attempt, | |
| CONVERT_TIMEZONE('America/Los_Angeles', 'Asia/Kolkata', created) AS created_local, | |
| CONVERT_TIMEZONE('America/Los_Angeles','Asia/Kolkata', processed) AS processed_local, | |
| status, | |
| error_message, | |
| message_source_info | |
| FROM TABLE(INFORMATION_SCHEMA.NOTIFICATION_HISTORY()) | |
| WHERE created >= DATEADD(minute, -1, CURRENT_TIMESTAMP()) -- Last 5 minutes | |
| ORDER BY created DESC | |
| LIMIT 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment