Mechanic Get Mechanic
Task library

Report Toaster: Deliver report PDF via email or Slack

This task demonstrates how a report can be requested from Report Toaster as a PDF. Once delivered, it is emailed to one or more recipients and/or sent to a Slack channel.

Try this task

When it runs

  • Occurs when a user manually triggers the task
  • Occurs when a Report Toaster report is created

What you choose

  • Report ID
  • Email report recipients
  • Slack incoming webhook URL
  • Message body
  • Run this task daily

Documentation

This task demonstrates how a report can be requested from Report Toaster as a PDF. Once delivered, it is emailed to one or more recipients and/or sent to a Slack channel.

You can use this task with any pre-built or custom report from Report Toaster by simply changing the report ID. Learn how to find your report ID here.

The report can be requested via CSV or PDF by changing the file format property. If the file format is omitted, the report results are returned as JSON directly within the task result.

See Mechanic's documentation on Report Toaster for more information.

The task can be run manually, or optionally, set to run as a daily report by checking the "Run this task daily" option. If a less frequent schedule is desired, then you may substitute the "mechanic/scheduler/daily" subscription with a different scheduler topic. See the Mechanic scheduler event topics for more details.

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

{% if options.run_this_task_daily__boolean %}
  mechanic/scheduler/daily
{% endif %}
mechanic/user/trigger
report_toaster/reports/create
How task subscriptions work

Option types and flags

Task code

{% comment %}
  Preferred option order:

  {{ options.report_id__required}}
  {{ options.email_report_recipients__array }}
  {{ options.slack_incoming_webhook_url }}
  {{ options.message_body__multiline_required }}

{% endcomment %}

{% if options.email_report_recipients__array == blank and options.slack_incoming_webhook_url == blank %}
  {% error "One or more email addresses or a Slack webhook must be defined." %}
{% endif %}

{% if event.preview %}
  {% assign event = hash %}
  {% assign event["data"] = hash %}
  {% assign event["data"]["name"] = "A report name" %}
  {% assign event["data"]["meta"] = hash %}
  {% assign event["data"]["meta"]["task_id"] = task.id %}
  {% assign event["data"]["file"] = hash %}
  {% assign event["data"]["file"]["url"] = "http://www.google.com" %}
  {% assign event["preview"] = true %}
{% endif %}

{% if event.topic contains "mechanic/scheduler/" or event.topic == "mechanic/user/trigger" or event.preview %}
  {% action "report_toaster" %}
    {
      "operation": "report",
      "reports": [
        {
          "id": {{ options.report_id__required | json }},
          "file": {
            "format": "pdf"
          },
          "meta": {
            "task_id": {{ task.id | json }}
          }
        }
      ]
    }
  {% endaction %}

{% endif %}

{% if event.topic == "report_toaster/reports/create" or event.preview %}

  {% if options.email_report_recipients__array != blank and event.data.meta.task_id == task.id %}

    {% action "email" %}
      {
        "to": {{ options.email_report_recipients__array | join: "," | json }},
        "subject": {{ event.data.name | append: " Report" | json }},
        "body": {{ options.message_body__multiline_required | strip | newline_to_br | json }},
        "reply_to": {{ shop.customer_email | json }},
        "from_display_name": {{ shop.name | json }}
      }
    {% endaction %}
  {% endif %}

  {% if options.slack_incoming_webhook_url != blank and event.data.meta.task_id == task.id %}

    {% action "http" %}
      {
        "method": "post",
        "url": {{ options.slack_incoming_webhook_url | json }},
        "body": {
          "text": {{ options.message_body__multiline_required | strip | json }}
        }
      }
    {% endaction %}
  {% endif %}
{% endif %}
Mechanic Liquid reference

Default option values

Message body
Hello,

Your report is now available for download:

Report Name: {{ event.data.name }}
Download Link: {{ event.data.file.url }}

Enjoy!