Concedi un badge personalizzato tramite l'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 Mi Piace

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 Mi Piace

Vedi anche Create a custom badge with an image through the API, che mostra come caricare e ha anche un altro esempio di assegnazione del badge (in python).

1 Mi Piace

Qual è il motivo per cui questo metodo non è descritto nella documentazione API su https://docs.discourse.org/?

Non sono riuscito a trovarlo lì.

2 Mi Piace

Poiché non sembra essere compito di nessuno mantenere aggiornata o completa la documentazione e qualche anima gentile ha pubblicato queste istruzioni qui. In generale, la documentazione per l’API è Reverse engineer the Discourse API e guardando il codice sorgente.

7 Mi Piace

Bello, quindi non c’è modo di automatizzare l’assegnazione di badge tramite API?

Ad esempio, a qualcuno che riceve 5.000 “mi piace” alle proprie risposte viene assegnato un badge.

1 Mi Piace

Se sei self-hosted o su Enterprise, vorrai Abilitare Badge SQL.

2 Mi Piace

Se non è possibile abilitare il badge SQL, sarebbe tecnicamente possibile automatizzare questo processo, ma sarebbe un processo in due fasi. Innanzitutto, effettua una richiesta API per eseguire una query di Data Explorer che restituisca i nomi utente degli utenti che soddisfano i tuoi criteri e a cui non è ancora stato assegnato il badge: Esegui query di Data Explorer con l’API di Discourse. Quindi, utilizza i nomi utente restituiti da tale query per assegnare il badge tramite l’API.

Tuttavia, questo sembra piuttosto inefficiente. Per quanto ne so, non esiste un percorso che possa essere utilizzato per assegnare un badge a più utenti con una singola chiamata API, quindi a seconda di quanti utenti si qualificano per il badge, potresti finire per effettuare molte richieste API.

In definitiva, questo sembra il tipo di cosa che potrebbe essere gestita dal plugin Automation. Potrebbe esserci un trigger ricorrente che esegue una query di Data Explorer con un dato ID di query. In base ai campi restituiti dalla query, i risultati potrebbero essere utilizzati per attivare vari script. Ad esempio, se venisse restituito un campo username dalla query, potrebbe essere utilizzato per eseguire script che assegnano badge, aggiungono utenti a gruppi, ecc.

2 Mi Piace

Non avrà gli stessi problemi che hanno causato la disabilitazione dell’SQL del badge in primo luogo?

1 Mi Piace

Non credo. Le query di Data Explorer vengono eseguite in un blocco di transazione che scade dopo 10 secondi. Penso che il punto sia evitare il tipo di problema che le query dei badge possono innescare. Se le cose non fossero configurate in quel modo, una query inefficiente di Data Explorer potrebbe bloccare il sito.

Pensandoci ora, mi chiedo se le query SQL personalizzate per i badge possano avere lo stesso tipo di protezione.

4 Mi Piace