API: Create a category but append/add to description more details

I’m using API request to Discourse to create a category, however in the same process I would like to add text or URL reference etc, to Category description (or is it description_text as I try to understand the properties or differences).

Note also that there might be a “robot” adding to this, so the best way…

  1. create category (working)
  2. Update category or more so append the link, but to category.description or category.description_text)?

However there are no properties on update except name, color, text_color, so are there other properties I can send? Like (and which) description or/maybe description_text?

All I want to do is add a slug link to a site related to the category I’m creating… or simply add at the end of the Discourse Category description “[link][https/example.projectxxx”

1 Mi Piace

Hi, Andrew.

Although the docs only list those 3 properties, most of the properties mentioned in the Response object can be set on the create call. I found this out when I needed to script the creation of a large number of categories with the same settings except for the name and slugs.

The only property I needed to set and consistently had issues with is the permissions.

2 Mi Piace

Yes, I’ve noticed that properties allowed are not those defined in the API documentation.

However to set the Category “description” which is very confusing as the category description is not at all what is displayed, in fact below the category name is the HTML element “cooked” which is empty, so how to set that…

From what I’ve been able to understand, I must set a post in the “About the --category name-- …” and it must be the first post in that category, but I think there is other critaria, like the cooked or also the fact that we have a Bot that first posts there.

I’ve tried adjusting the created_at/updated_at dates to make my post appear above so that the category will grab or use that description, but no luck! There must be something I’m not understanding about the “cooked” and/or category description. Very confusing and I’ve found no docs, but I’ll keep looking, if anyone has ideas on what to try, please let me know.

Ciao, riporto su questo vecchio argomento…

C’è un modo per capire la differenza tra description_text e description?
Voglio dire, a parte scoprirlo facendo :slight_smile:
magari @alank l’ha scoperto nel frattempo!
Grazie!

1 Mi Piace

Se non è documentato altrove, la ricerca nei file spec del repository discourse risponderà a domande come questa con una comprensione di base di come funzionano i linguaggi di programmazione.

In questo caso, le seguenti righe nello spec della categoria mostrano che description_text deriva da description ed è immediatamente disponibile dopo aver impostato description.

Questo mi dice che description è la stringa grezza per la descrizione e, una volta impostata, viene elaborata e assegnata a description_text. Oppure description_text potrebbe essere calcolato quando vi si accede, ma ne dubito, in ogni caso description è quello da impostare.

1 Mi Piace

Grazie Simon, questo chiarisce la differenza tra entrambi i parametri.

Tuttavia, ho appena provato e nessuno di questi due parametri è effettivamente impostato nella descrizione della categoria (o nel post della categoria Informazioni). Quindi il problema dell’OP rimane.

Aggiungo: vedo nel file categories.json che questi parametri di descrizione sono ciò che voglio, ma in qualche modo non riesco a impostarli tramite l’API!

Se non sono documentati per il payload della richiesta POST, non puoi fare affidamento sulla possibilità di impostarli in quel modo, anche se dovesse funzionare.

Sospetto che la cosa da fare sia guardare il topic_url nella risposta dopo aver creato la categoria, quindi ottenere un singolo topic utilizzando l’ID da quell’URL. Guarda il post_stream nella risposta che ha un array di post, immagino che avrà un elemento.

Ottieni l’ID di quel post e quindi aggiorna un singolo post per aggiungere la tua descrizione. Ciò dovrebbe attivare l’aggiornamento della descrizione della categoria allo stesso modo della modifica del post in Discourse, stessa regola della prima riga.

1 Mi Piace

Stavo per pubblicare, è esattamente quello che ho fatto :slight_smile:

    ## crea categoria
    ret_cat = client.create_category(name, color = color, **kwargs)

    ## ottieni il primo id del post 
    cat_id = ret_cat["category"]["id"]
    print("Cat id", cat_id)
    topics = client.category_topics(cat_id)
    first_post_id = client.topic_posts(topics["topic_list"]['topics'][0]["id"])['post_stream']['posts'][0]['id']
    ## modifica il primo post
    ret_post = client.update_post(first_post_id, cat["about_msg"],"set up")
3 Mi Piace