APIを使用してカスタムバッジを付与する

Custom badges that you have created on your forum can be granted through the API. This is a great way to award badges without having to use custom badge SQL.

To grant a badge through the API, you need to know the username of the user that you wish to grant the badge to, and the ID of the badge you wish to grant. You also need to make sure you have generated an All Users API Key from your site’s Admin/API section.

Finding your API Key

This screenshot is from my local development site. Generally, you need to be very careful about sharing your API keys:

Finding the Badge ID

You can get the Badge ID from the badge’s URL. Go to your Admin/Badges section and then click on the badge that you wish to grant. The URL will look something like this: https://forum.example.com/admin/badges/102. The last number in the URL is the badge ID.

Making the API call

To test an API call, you can try granting a badge using curl or Postman. Here is how I grant a badge from my computer’s terminal with curl.

First, to make things easier, set an api_key variable:

 api_key=yourallusersapikey

Then to grant a badge with the ID of 102 to the user bobby:

curl -X POST "https://forum.example.com/user_badges" \
-H "Content-Type: multipart/form-data;" \
-H "Api-Key: $api_key" \
-H "Api-Username: system" \
-F "username=bobby" \
-F "badge_id=102" \
-F "reason=https://forum.example.com/t/whats-the-best-photo-youve-ever-taken/160/2"

The reason parameter is optional. If you supply it, it must be set to the URL of a topic or a post on your site.

You should get a JSON response with details about the badge and when it was granted.

「いいね!」 26

Has anybody added a badge (or flair) via Zapier to Discourse API like outlined here?

Also, seems like a great candidate for an action via the official integration, @HAWK.

「いいね!」 3

画像付きカスタムバッジをAPI経由で作成する方法に関する Create a custom badge with an image through the API も参照してください。このリンクには、アップロード方法と、バッジの付与に関する別の例(Python)も記載されています。

「いいね!」 1

このメソッドが https://docs.discourse.org/ のAPIドキュメントに記載されていない理由は何ですか?

そこで見つけられませんでした。

「いいね!」 2

ドキュメントを完全な状態に保ったり、最新の状態にしたりすることが誰の仕事でもないようで、誰かが親切にもここに手順を投稿したからです。一般的に、APIのドキュメントはDiscourse APIのリバースエンジニアリングとソースコードを見ることによって作成されます。

「いいね!」 7

API経由でこのようなバッジの付与を自動化する方法はないということですね。

例えば、返信に5,000いいねが付いた人にバッジが付与される、といった具合です。

「いいね!」 1

セルフホストまたはエンタープライズをご利用の場合は、バッジSQLを有効にする必要があります。

「いいね!」 2

バッジSQLを有効にできない場合、技術的には自動化可能ですが、2段階のプロセスになります。まず、APIリクエストを実行して、条件を満たしているがまだバッジが付与されていないユーザーのユーザー名を返すData Explorerクエリを実行します。Discourse APIでData Explorerクエリを実行する。次に、そのクエリによって返されたユーザー名を使用して、API経由でバッジを付与します。

ただし、これは少し非効率に感じます。私の知る限り、単一のAPI呼び出しで複数のユーザーにバッジを付与するために使用できるルートはないため、資格のあるユーザー数によっては、多数のAPIリクエストを行うことになる可能性があります。

最終的には、これはAutomationプラグインで処理できる種類のもののようです。指定されたクエリIDを持つData Explorerクエリを実行する定期的なトリガーを設定できます。クエリによって返されたフィールドに基づいて、結果を使用してさまざまなスクリプトをトリガーできます。たとえば、クエリによってusernameフィールドが返された場合、バッジを付与したり、ユーザーをグループに追加したりするスクリプトを実行するために使用できます。

「いいね!」 2

バッジのSQLが無効になった原因と同じ問題が発生しませんか?

「いいね!」 1

そうは思いません。Data Explorer のクエリは、10 秒後にタイムアウトするトランザクション ブロックで実行されます。これは、バッジ クエリがトリガーする可能性のある種類の問題を回避するためのものだと思います。もし設定がそのようになっていなければ、非効率的な Data Explorer クエリがサイトをクラッシュさせる可能性があります。

今考えてみると、カスタム バッジ SQL にも同様のセーフガードがあるかどうか疑問に思います。

「いいね!」 4