Skip to content

Slack Connector Tutorial

Christopher Greening edited this page Jul 20, 2021 · 2 revisions

The purpose of this tutorial is to show example code that sends a message to Slack using the connectors provided by route1.io.

Table of Contents


Prerequisites

  • route1io_connectors installed
  • Credentials
    • Slack Webhook (see how to set one up here)

Importing the connector

All Slack connectors can be found in the route1io_connectors.slack module

from route1io_connectors import slack

Sending a text message

With a Slack Webhook, it's very easy to POST a message to a Slack channel.

Parameters:

  • message: Text message to send
  • webhook: URL of the Webhook to send message to
slack.slack_message(
    message="There is new data to check out in S3!",
    webhook="https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
)

Sending a block message

In cases where we want to send a more complicated message to be rendered in Slack, we can build the JSON as a Python dictionary and then pass it directly to the slack_block_message function.

See this page for learning more about Blocks in Slack.

Parameters:

  • message: Block message to send
  • webhook: URL of the Webhook to send message to
urgent_block_message = {
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "Urgent Message"
            }
        },
        {
            "type": "divider"
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": f"This is an urgent message!\nPlease check Google Sheets."
            }
        },
    ]
}
slack.slack_block_message(
    message=urgent_block_message,
    webhook="https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
)
Clone this wiki locally