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 Discourse Webhook for user event
We will update the “Payload URL” once the webhook URL is generated in next steps.
Create a new Zap
Look for this button on Zapier dashboard:
Set up Trigger
-
Select Webhook as Trigger. Look for:
-
Choose Trigger
You want to select “Catch Hook”
-
You can skip “Set up Webhooks by Zapier Hook” step.
Just press “Continue”.
-
Test Trigger
Copy the custom webhook URL generated and paste it in “Payload URL” section of Discourse Webhook originally created.
Now try creating/updating/approving a user on your Discourse instance.
If you successfully followed all the steps and created/updated/approved a user, you will see a success message on Zapier:
Clicking on “view your hook” hyperlink will show the parsed data.
Set up Action on Zapier
-
Select Email as Action. Look for:
-
Press “Save + Continue”
-
Set Up Email Template
Further modify the email template as per your requirement. Note that you can use the
+
button to add more data parsed via JSON response.Now test the action, you should receive an email from 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).
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.
- Click “show less common options”
- Click “Catch Raw Hook”
- Follow the remaining steps to configure the trigger normally.
Once the trigger is configured, add a filter.
- Click “Add a Step”
- Click “Filter”
- Click “Save and Continue”
- Select “Headers Http X Discourse Event” from the first dropdown.
- Select “(Text) Exactly matches” from the second dropdown.
- Enter the complete header you want to filter for (for example,
user_logged_out
). - If you want Zapier to run on multiple headers, click “+OR” and add them just like the first.
- Click “Test & Continue”
- 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 @SaraDev on 2022-08-17T00:00:00Z
Last edited by @JammyDodger 2024-05-26T06:52:54Z
Check document
Perform check on document: