Grant a custom badge through the 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 (or name) of the badge you wish to grant. You also need to make sure you have generated an API key from your site’s Admin > API > Keys section (/admin/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=yourapikey

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

curl -X POST "https://forum.example.com/user_badges" \
-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"

Instead of badge_id, you can also use badge_name to identify the badge by its name:

-F "badge_name=My Custom Badge"

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.

27개의 좋아요

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개의 좋아요

자체 호스팅 또는 Enterprise 플랜을 사용 중이라면 Enable Badge 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개의 좋아요