Mechanic is an automation development platform for Shopify. :)
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.
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:
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.
{% 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 %}
{{ product.title }} is now available! https://{{ shop.domain }}/products/{{ product.handle }}
true