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

Mechanic is an automation development platform for Shopify. :)

Send an SMS via Nexmo when a product is created

by Isaac Bowen (team@usemechanic.com)

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 when 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 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, Gurus, 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.

Events
when a product is created (shopify/products/create)
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)
Script
{% 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 %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Message content
{{ product.title }} is now available!

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