Cloudhooks GPT prompting guide

Executive summary

This guide helps developers effectively use Cloudhooks GPT to create and improve webhook handlers (hooks) in Cloudhooks. It covers prompt structuring, terminology, and practical examples for generating hooks, utilizing context variables, and working with various actions. Whether you're new to Cloudhooks or looking to optimize your hook development process, this guide provides the essential knowledge and best practices.

Introduction to Cloudhooks GPT

Cloudhooks GPT is a development tool to create and improve hooks in Cloudhooks. It’s a specialized version of OpenAI’s ChatGPT model, and it’s available for users with a ChatGPT Plus subscription.

Cloudhooks GPT is particularly useful for developers drafting their first hook in Cloudhooks, as the model knows the expected function signature of the hook, all supported actions, the structure of the ‘context’ variable, and the utility functions available in the hook’s environment.

Developers can describe their requirements in plain English (using a ‘prompt’) to which Cloudhooks GPT responds with javascript code or text depending on the type of the prompt.

Disclaimer

Cloudhooks GPT is a tool for drafting your hook, it’s not a complete replacement for javascript programming skills. You should verify, revise, and improve the generated code.

Cloudhooks GPT sometimes hallucinates which may result in javascript code with invalid syntax, actions not supported by Cloudhooks, or the hook accessing payload properties that don’t exist.

Structure of an effective prompt

A good prompt for Cloudhooks GPT should have the following structure:

  • Clear Objective: Define the specific task you need.
    For example, create a hook or list supported actions.
  • Relevant Details: Include necessary details like the type of trigger and payload, the required actions (e.g., tagging an order, making a HTTP POST request, sending an email), properties of the payload to use, etc.
  • Constraints and Requirements: Mention constraints like using async/await, try/catch, or specific Shopify API versions.

Terminology used in prompts

In order to get the best result, the use of certain phrases is highly recommended in prompts:

  • Hook: Webhook handler in Cloudhooks.
  • Trigger event: Event to which the hook responds.
  • Payload: Data sent to the hook by the store.
  • Action: A function of the 'actions' variable of the hook carrying out a certain action.
  • Context: The 'context' variable of the hook containing information about the run environment.
  • Hook utilities: The 'hookUtils' variable of the hook providing helper packages and functions.

Drafting hooks with Cloudhooks GPT

Color-coding prompts

For demonstration purposes, let’s color-code the various parts of the prompt as follows:

  • Brown: Main objective or task description
  • Green: Specific details about the trigger and payload
  • Purple: Constraints and special requirements

Generating hooks

For generating a hook, tell the Cloudhooks GPT explicitly what you need:

Here is the generated hook:

module.exports = async function(payload, actions, context) {
  try {
    // Calculate total order amount
    let totalAmount = payload.total_price || 0;

    if (totalAmount >= 100) {
      // Add 'big-order' tag to the order
      await actions.shopify.tagOrder(payload.id, 'big-order');

      // Add 'big-spender' tag to the customer
      await actions.shopify.tagCustomer(payload.customer.id, 'big-spender');
    } else {
      console.log('Small order');
    }
  } catch (error) {
    console.error(`Error processing order: ${error}`);
  }
};

Utilizing the 'context' variable

Cloudhooks GPT is aware of the contents of the ‘context’ variable, so you can refer to them in the prompt:

Cloudhooks GPT generates a concise hook:

module.exports = async function(payload, actions, context) {
    const mode = context.isTestMode ? 'Test' : 'Live';
    console.log(`${mode} mode on ${context.shopifyDomain}`);
};

Utilizing the 'hookUtils' variable

Cloudhooks GPT is aware of the contents of the ‘hookUtils’ variable, so for example, you can use crypto functions in the prompt:

Here is the resulting hook:

module.exports = async function(payload, actions, context) {
    try {
        const { createHash } = hookUtils.crypto;
        
        const hashProperty = (value) => {
            return createHash('sha256').update(value).digest('hex');
        };

        const compiledObject = {
            orderId: hashProperty(payload.id.toString()),
            email: hashProperty(payload.email),
            totalOrderAmount: hashProperty(payload.total_price.toString())
        };

        await actions.http.post('https://cloudhooks.dev', compiledObject);
    } catch (error) {
        console.error('Error in hook execution:', error);
    }
};

Action-related prompts

You can get information about supported actions or a subset of actions:

Here is the result of the prompt: