Mechanic Get Mechanic
Task library

Demonstration: Post to a Slack channel

Demonstration: Post to a Slack channel

Try this task

When it runs

Occurs when a user manually triggers the task

What you choose

  • Slack account
  • Slack channel ID

Documentation

Demonstration: Post to a Slack channel

This task posts a demonstration message in the Slack channel of your choice. Try it out to learn how the Slack action works!

Quick Start

  1. Install the Mechanic Slack app in your Slack workspace (Settings β†’ Authentication β†’ Slack)
  2. Set two task options:
    • Your Slack account
    • The Slack channel ID to post to
      • If this is a private channel, then you must add the Mechanic bot to the channel before it can post messages
  3. Run the task

Look in your Slack channel for the demonstration message. (Although you'll likely get a combination of app, email, and web notifications for this one post πŸ˜‚)

Want to Learn More?

This task demonstrates how to:
- Create a new message in an Slack channel
- Build dynamic message structure with some common Slack block kit components: header, section, divider, button

Take a look at the task code to see exactly how it works, then use this example in your own tasks.

Slack has extensive API documentation covering the many endpoints this integration can utilize. In relation to this demo task, the following API docs are the most relevant:
- https://docs.slack.dev/reference/methods/chat.postmessage
- https://docs.slack.dev/messaging/
- https://docs.slack.dev/block-kit/

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

mechanic/user/trigger
How task subscriptions work

Option types and flags

Task code

{% assign slack_account = options.slack_account__required %}
{% assign slack_channel_id = options.slack_channel_id__required %}

{% comment %}
  IMPORTANT:
  -- This task requires the Mechanic app for Slack be installed in your Slack instance
  -- It will create a bot user - Mechanic - which the API actions will be made on behalf of
  -- This bot can automatically post to any public channels, but it must be added to any private channels you wish to post to
  -- Most Slack API errors will be surfaced by the integration, since the Slack convention is to return a 200 status even when there are errors

  API REFERENCEs:
  -- https://docs.slack.dev/reference/methods/chat.postmessage
  -- https://docs.slack.dev/messaging/
  -- https://docs.slack.dev/block-kit/
{% endcomment %}

{% if event.topic == "mechanic/user/trigger" %}
  {% comment %}
    ** Slack API - Post a new message **
    -- This action will create a new message in the configured channel of the connected Slack account
  {% endcomment %}

  {% action "slack" %}
    {
      "account": {{ slack_account | json }},
      "method": "POST",
      "url_path": "/chat.postMessage",
      "headers": {
        "Content-Type": "application/json"
      },
      "body": {
        "channel": {{ slack_channel_id | json }},
        "text": "Slack Integration Test",
        "blocks": [
          {
            "type": "header",
            "text": {
              "type": "plain_text",
              "text": "πŸŽ‰ Your Slack Integration is Working!"
            }
          },
          {
            "type": "section",
            "text": {
              "type": "mrkdwn",
              "text": "Great job setting up the Slack integration! Here's what I can do:"
            }
          },
          {
            "type": "section",
            "fields": [
              {
                "type": "mrkdwn",
                "text": "*βœ… Send Messages*\nSend messages as @mechanic"
              },
              {
                "type": "mrkdwn",
                "text": "*#️⃣ Post to Public Channels*\nSend messages to public channels the app isn’t a member of"
              },
              {
                "type": "mrkdwn",
                "text": "*πŸ“‘ Upload Files*\nUpload, edit, and delete files as @mechanic"
              },
              {
                "type": "mrkdwn",
                "text": "*πŸ‘¨πŸ½β€πŸ”§ Customize Messages*\nSend messages with a customized username and avatar"
              }
            ]
          },
          {
            "type": "divider"
          },
          {
            "type": "section",
            "text": {
              "type": "mrkdwn",
              "text": "*Shop Details:*\nβ€’ Name: {{ shop.name }}\nβ€’ Domain: {{ shop.domain }}\nβ€’ Email: {{ shop.email }}"
            },
            "accessory": {
              "type": "button",
              "text": {
                "type": "plain_text",
                "text": "Visit Shop"
              },
              "url": "https://{{ shop.domain }}",
              "action_id": "button-action"
            }
          }
        ]
      }
    }
  {% endaction %}
{% endif %}
Mechanic Liquid reference