Send an SMS via Nexmo when a product is created, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

Send an SMS via Nexmo when a product is created

This task is a demonstration of sending text messages via Nexmo. In this task, we monitor for newly-created products, and we send a text message about the product to a phone number stored in that product's metafield.

Runs Occurs whenever a product is created. Configuration includes message content, phone number metafield namespace, phone number metafield key, nexmo api key, nexmo api secret, nexmo sms phone number, test mode, default country code, and minutes to wait before sending.

15-day free trial – unlimited tasks

Documentation

This task is a demonstration of sending text messages via Nexmo. In this task, we monitor for newly-created products, and we send a text message about the product to a phone number stored in that product's metafield.

This task monitors for newly-created products, and sends a text message about the product to a phone number stored in that product's metafield. If there is no number stored, or the number is invalid, no message will be sent.

Notes:

  • Nexmo requires all phone numbers to have a country code. If a phone number encountered appears to be an invalid international number, this task will try to add whatever value you've got in the "Default country code" option.
  • Your Nexmo information (API key/secret, and SMS phone number) can be found on the Nexmo dashboard's "Getting started" page
  • Use test mode to have this task report the message that it would send, if test mode was not enabled

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, everybody.

That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
shopify/products/create{% if options.minutes_to_wait_before_sending__number != blank %}+{{ options.minutes_to_wait_before_sending__number }}.minutes{% endif %}
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
message content (required, multiline), phone number metafield namespace (required), phone number metafield key (required), nexmo api key (required), nexmo api secret (required), nexmo sms phone number (required), test mode (boolean), default country code (number), minutes to wait before sending (number)
Code
{% comment %}
  Preferred option order:

  {{ options.message_content__required_multiline }}
  {{ options.phone_number_metafield_namespace__required }}
  {{ options.phone_number_metafield_key__required }}
  {{ options.nexmo_api_key__required }}
  {{ options.nexmo_api_secret__required }}
  {{ options.nexmo_sms_phone_number__required }}
  {{ options.test_mode__boolean }}
{% endcomment %}

{% assign metafield_value = product.metafields[options.phone_number_metafield_namespace__required][options.phone_number_metafield_key__required] %}

{% if event.preview %}
  {% assign metafield_value = "1 (312) 456-7890" %}
{% endif %}

{% assign recipient_number = metafield_value | e164 %}
{% if options.default_country_code__number != blank and recipient_number == blank and metafield_value != blank %}
  {% assign recipient_number = options.default_country_code__number | append: " " | append: metafield_value | e164 %}
{% endif %}

{% assign from_number = options.nexmo_sms_phone_number__required | e164 %}
{% if options.default_country_code__number != blank and from_number == blank %}
  {% assign from_number = options.default_country_code__number | append: " " | append: options.nexmo_sms_phone_number__required | e164 %}
{% endif %}

{% if recipient_number == blank and metafield_value == blank %}
  {% log "No phone number was found on file for this product." %}
{% elsif recipient_number == blank and metafield_value != blank %}
  {% assign message = metafield_value | json | append: " was found on file for this product, but this was not a valid phone number." %}
  {% log message %}
{% else %}
  {% if options.test_mode__boolean %}
    {% action "echo" %}
      {{ "Sending to " | append: recipient_number | append: ": " | append: options.message_content__required_multiline | json }}
    {% endaction %}
  {% else %}
    {% assign authorization = options.nexmo_api_key__required | append: ":" | append: options.nexmo_api_secret__required | base64 | prepend: "Basic " %}

    {% action "http" %}
      {
        "method": "post",
        "url": "https://api.nexmo.com/v0.1/messages",
        "headers": {
          "Authorization": {{ authorization | json }}
        },
        "body": {
          "to": {
            "type": "sms",
            "number": {{ recipient_number | json }}
          },
          "from": {
            "type": "sms",
            "number": {{ from_number | json }}
          },
          "message": {
            "content": {
              "type": "text",
              "text": {{ options.message_content__required_multiline | strip | json }}
            }
          }
        },
        "client_ref": {{ event.id | json }}
      }
    {% endaction %}
  {% endif %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Message content
{{ product.title }} is now available!

https://{{ shop.domain }}/products/{{ product.handle }}
Test mode
true