使用 Zapier 对 Discourse API 发送请求

Discourse API 的请求可以通过创建一个使用 Zapier Webhook 作为其操作步骤的 Zap 来实现自动化。本主题将介绍如何执行以下操作的请求:

  • 将用户添加到群组
  • 授予自定义徽章

要了解如何使用 Zapier 执行其他类型的 API 请求,请阅读本主题,然后在 Discourse API 文档 中搜索您想要执行的操作。您还可以通过阅读 Reverse engineer the Discourse API 来了解如何为特定操作创建 API 请求。

设置触发步骤

每个 Zap 都必须有一个触发步骤和一个操作步骤。触发步骤用于将数据从应用程序传递到 Zap 的操作步骤。在某些情况下,触发步骤还可以用于阻止 Zap 完成其操作步骤,除非其数据满足特定条件。

要设置您的操作步骤,请访问 您的 Zapier 仪表板 并点击“创建 Zap”按钮。此时将打开一个搜索表单,要求您选择一个触发应用程序。

在本主题的示例中,我使用 WordPress 新用户事件作为操作步骤。这是因为它易于设置以测试 API 调用。

触发步骤的“测试此步骤”节点允许您从触发应用程序中选择一份数据样本,该样本将传递给 Zap 的操作步骤。这些数据对于设置操作步骤非常有用。

添加可选步骤以从 Discourse 检索详细信息

根据从触发应用程序传递的数据,您可能需要在进行最终 API 请求之前从 Discourse 检索一些数据。例如,WordPress 新用户触发器会传递用户的 WordPress 用户名和电子邮件地址。我知道电子邮件地址将与 Discourse 用户的电子邮件地址匹配,但在此示例的 API 请求中,我需要知道用户的 Discourse 用户名。

要从电子邮件地址获取用户的 Discourse 详细信息,请在您的 Zap 中添加一个操作步骤。从 Webhook 的操作菜单中选择"GET"。

在 Webhook 的编辑模板步骤中,在 URL 部分输入您的 Discourse 站点的基础 URL,后跟 /admin/users/list/all.json。例如,我的站点的基础 URL 是 https://demo.scossar.com,因此我在 URL 字段中输入 https://demo.scossar.com/admin/users/list/all.json

在查询字符串参数部分,输入"email"作为键,然后点击“插入字段”图标以打开下拉菜单。选择由触发步骤传递的包含用户电子邮件地址的值。

对请求进行身份验证

向下滚动表单至其“请求头”部分。此部分用于对请求进行身份验证。它需要三个键/值对:

  • Api-Username:您站点上管理员用户的用户名。在大多数情况下,选择“系统”用户是一个不错的选择。
  • Api-Key:与您在第一个键/值对中使用的用户名关联的 API 密钥。
  • Content-Typemultipart/form-data

完成的“请求头”部分应与此类似,但使用您用户的 API 密钥:

点击“继续”按钮,查看从此请求中从 Discourse 检索到的数据。

添加最终操作步骤

配置好触发步骤和从 Discourse 检索数据的可选步骤后,点击“添加步骤”链接,并从操作菜单中选择"Zapier 的 Webhooks"。然后,系统会要求您选择要在对 Discourse 的 API 请求中使用的请求方法。

以下是本主题所用示例所需的请求类型:

  • 将用户添加到群组:PUT
  • 授予自定义徽章:POST

要配置除本主题示例之外的其他 API 请求,请查看 Discourse API 文档,看看是否有您想要进行的请求示例。如果您在那里找不到示例,请阅读 如何逆向工程 Discourse API,以了解如何查找您想要执行的操作的 URL 和请求方法。找到请求方法后,从操作菜单中选择它。

注意:如果您的请求使用 DELETE 方法,请从操作菜单中选择“自定义请求”。

配置最终操作步骤

所有 API 请求的操作“请求头”部分都可以以相同的方式进行配置。有关详细信息,请参阅本主题的“对请求进行身份验证”部分。如果您添加了从 Discourse 检索详细信息的可选步骤,则最终操作的“请求头”部分可以以完全相同的方式进行配置。

添加完请求头键/值对后,您需要填写表单的 URL 和数据字段以进行 API 请求。

将用户添加到群组

要将用户添加到群组,需向 /groups/<group_id>/members.json 发出 PUT 请求。查找群组 ID 最简单的方法是通过 Discourse UI 访问群组页面,然后在浏览器地址栏的 URL 中输入 .json。例如,我的站点有一个“支持”群组,位于 https://demo.scossar.com/g/support。通过访问 https://demo.scossar.com/g/support.json,我可以看到群组 ID 为 41。我的论坛的基础 URL 是 https://demo.scossar.com。我最终操作步骤中将用户添加到群组的 URL 设置为 https://demo.scossar.com/groups/41/members.json

将用户添加到群组的请求需要一个参数——用逗号分隔的用户名列表。在表单的“数据”部分,输入"usernames"作为键。然后点击“插入字段”图标,搜索从触发步骤或可选的 GET 操作步骤传递的用户名属性。

在我的情况下,我想要 GET 步骤检索到的用户名,因此我展开"GET"菜单并从下拉列表中选择“用户名”。

表单的所有其他部分可以保留其默认值。

点击“继续”按钮,然后测试您的步骤。如果您通过前几个步骤传递的用户存在于您的 Discourse 站点上,并且他们还不是您所选群组的成员,那么在测试该步骤时,他们应该会被添加到该群组中。

如果一切按预期工作,请启用您的 Zap。

授予自定义徽章

要向用户授予自定义徽章,需向您的论坛基础 URL + /user_badges 发出 POST 请求。对于我的站点,授予徽章的 URL 是 https://demo.scossar.com/user_badges。Zapier 上完成的 URL 字段如下所示:

表单的“数据”部分需要两个键/值对。添加一个"username"键,并将其设置为返回用户姓名的字段。添加一个"badge_id"键,并将其设置为您想要授予的徽章的 ID。您可以通过访问您的管理员/徽章页面并从左侧菜单中选择徽章来找到徽章 ID。您将在浏览器地址栏的 URL 的最后一个值中看到徽章 ID。

我要授予的徽章 ID 为 105,因此我完成的“数据”部分如下所示:

确保您已配置表单的“请求头”部分,然后点击“继续”。然后点击“发送测试”按钮以测试您的 Zap。通过前几个步骤传递的用户应该被授予该徽章。

如果一切正常,请启用 Zap。

15 个赞

Can this workflow be used to automatically create an account for a new user and add them to a specific discourse group?

The use case here is someone buys a training product, I want to automatically then add them to the relevant discourse sub forum for that product.

I can get the new customer data into Zapier but the regular discourse zap doesn’t have create account capabilities - just wondering if I can do this via the API and webhook link as you describe in this example.

Thanks!

2 个赞

The easiest approach would be to send an invite email to the person after they make a purchase. You can setup the invite so that the user is automatically added to a Discourse group when they accept the invitation. See Automate sending Discourse invite emails with Zapier for details.

If sending invites is not an option for you, it should be possible to use a Zapier webhook to create a Discourse user. The Discourse API Docs give details about the request to create a user.

8 个赞

Thanks @simon I’ll try the email invite. No doubt some people won’t get it/find it/open it but it’s the easier option for sure.

Appreciate the speedy response too!

It’s often easier to have whatever is selling the thing make the discourse api calls directly.

1 个赞

Is there a reason why this works for me when I do it in Zapier, but I get an empty response when I try to do my own fetch request?

I’m using ObservableHQ, but the idea is the same. Shouldn’t this work?:

fetch("https://mycommunity.com/g.json", {
  headers: {
    "Content-Type": "multipart/form-data",
    "Api-Username": Secret("DISCOURSE_USERNAME"),
    "Api-Key": Secret("DISCOURSE_KEY")
  },
  method: "GET",
  mode: "no-cors"
})

Are you able to get any API requests to Discourse working with calls in that format with ObservableHQ? It seems that there must be something wrong with the format of the request.

You can also double check requests by making curl calls from your terminal, or by using Postman.

1 个赞

OK, I figured out what I needed to do:

  1. Install cors-anywhere locally and run the server
  2. Modify the request to include the reverse proxy before the API endpoint and call the json method in the Fetch Response object:
await (fetch("http://localhost:8080/mycommunity.com/g.json", {
  headers: {
    "Api-Username": Secret("DISCOURSE_USERNAME"),
    "Api-Key": Secret("DISCOURSE_KEY")
  },
})).json()
2 个赞

先留个链接在这儿,可能值得一看:

2 个赞

你好 @simon,感谢这篇教程。我正在 Zapier 中设置此功能,但我的论坛没有用于授予徽章的此 URL。

我想请问,这不是所有 Discourse 安装的标准 URL 吗?或者该 URL 是否在最近的更新中发生了变化?

它应该有。不过,这不是可以通过 Discourse 用户界面直接访问的 URL。我刚刚按照此处概述的步骤,在我的网站上检查了该路由:如何逆向工程 Discourse API。根据该检查,主题中提供的详细信息 似乎仍然准确无误。

2 个赞

不出所料,是我自己的错误。

1 个赞