Trigger a Zapier task with Discourse Webhooks

Want to use Discourse Webhooks to trigger a task via Zapier? Let’s get started!

Zapier requires a trigger and an action. In this howto the trigger will be a Discourse Webhook and the action will be to send an email.

Send an Email on any User event

We will now set up a Zap to send email on any user event (i.e when a user is created, approved or updated). To trigger on a specific user event, see Trigger on a specific user event below.

Create a Discourse Webhook

Create a new Zap

Look for this button on Zapier dashboard:

Set up Trigger

Set up Action on Zapier

Turn on the Zap

That’s it, now you will receive an email for every new user event (i.e when a user is created, approved or updated). :tada:


Trigger on a specific user event

Want to only send emails for a specific user event, perhaps only on account creation, but not on update? Use the “Catch Raw Hook” trigger.

  1. Click “show less common options”
  2. Click “Catch Raw Hook”
  3. Follow the remaining steps to configure the trigger normally.

Once the trigger is configured, add a filter.

  1. Click “Add a Step”
    image
  2. Click “Filter”
    image
  3. Click “Save and Continue”
  4. Select “Headers Http X Discourse Event” from the first dropdown.
  5. Select “(Text) Exactly matches” from the second dropdown.
  6. Enter the complete header you want to filter for (for example, user_logged_out).
  7. If you want Zapier to run on multiple headers, click “+OR” and add them just like the first.
  8. Click “Test & Continue”
  9. Review the filter test, then click “Continue”

Once the filter is configured, configure your preferred action.

Using the Full Webhook Header

To get access to the header, follow the follow the previous steps - select “Catch Raw Hook” and then add a filter. Configure the filter to only continue if the Headers Http X Discourse Event matches the event you are looking for.

The difficulty here is that the Catch Raw Hook passes the webhook’s raw body. The final action I’m using needs parsed data from the body. What is working for me is to add a Code action after the filter. On the Code Action modal, select “Run Javascript”:

Zapier will create an inputData object that you can add named properties to. Add a name for your property in the left column. From the right column, select “Catch Raw Hook” from the dropdown menu, and then select “Raw Body”:

Scroll down the page to see a code input:

You then need to replace the sample code with some code that parses the Raw Body and returns an object that has the values you need for your final action. Here’s the code I’m using. The object it returns is being used by the Salesforce integration. Salesforce requires a last name, so if no last name exists, an error is returned:

const parsed = JSON.parse(inputData.raw);
if (parsed.user) {
  const user = parsed.user,
        name = user.name,
        userFields = user.user_fields;
  let firstName,
      lastName,
      company;
  
  if (name) {
    const splitName = name.split(' ');
    firstName = splitName[0];
    lastName = splitName[1]
    }
  if (userFields) {
    company = userFields['10'];
  }
  if (lastName) {
    return {id: user.id,
            username: user.username,
            lastName: lastName,
            firstName: firstName,
            email: user.email,
            company: company
           };
  } else {
    return {error: "Missing last name"}
  }
} else {
  return {error: "A user object was not returned"};
}

To avoid making a failed request in the final action, another Zapier filter can be added. Set that filter to only continue if the error field returned by the javascript does not exist:

You can then add your final action. For selecting the fields to be used in the final action, use the properties returned by the Run Javascript action.


Last Reviewed by @AlexDev on 2022-08-17T00:00:00Z

31 Likes