Interact with discourse from Python?

Hi everyone! Love discourse, it’s the backbone of our community.

We are an open source project with a strong presence on Github. We have a robot written in Python which manages our Github repositories and does things like merge pull requests automatically if they have been approved. Since we have a lot of repositories and not everyone follows every repository it would be awesome if the robot could post in the forums when a new project is created or when a pull request needs to be reviewed.

Can anyone point me in the direction of how I could best call the Discourse API from Python?

3 Mi Piace

There is a python discourse api library that is a few years old:

https://github.com/tindie/pydiscourse

And a forked version that has been updated somewhat recently:

https://github.com/bennylope/pydiscourse

Hopefully the latter one will work for you.

Also this plugin may be of interest to you:

https://meta.discourse.org/t/the-github-linkback-plugin/66081

7 Mi Piace

Thanks @blake! That is exactly the kind of thing I needed to get started.

I will report back with my successes.

2 Mi Piace

I recently started a project for this, and am actively updating it. Currently working towards the next dot release:

https://github.com/samamorgan/discourse

10 Mi Piace

Did it work out for you? I am trying this as well. I keep getting “DiscourseClientError: Payload Too Large”.

1 Mi Piace

I’m suddenly seeing this error too over the weekend when attempting to fetch a topic. This topic fetch code hasn’t changed in over a year, so something about the API must have changed. I’m investigating.

2 Mi Piace

Payload Too Large is because of this change.

I don’t know about the project mentioned above, but here is the status for bennylope’s project. Maybe the same fix is needed on other wrappers as well.

https://github.com/bennylope/pydiscourse issue #38 fixes the problem, but the fixed version has not yet been pushed to pypi- https://github.com/bennylope/pydiscourse/issues/38. Waiting for github @bennylope to do it. So you can wait for pypi to be updated, or git pull master.

5 Mi Piace

Thanks! This worked. I uninstalled pydiscourse, and used a local copy based on what you suggested. And now, it works!
I am trying to use Python’s Sentiment analyzer to get a sentiment analysis of the latest posts. I will keep you updated on how it goes.

1 Mi Piace

Is anybody else using this? Are you able to get more than 20 posts per topic using this?

1 Mi Piace

You probably need to use pagination. I haven’t tried this for posts specifically, but something like:

pagesize = 20
for page in range(100):
    client.posts(topic_id, limit=pagesize, offset=page*pagesize)
3 Mi Piace

Sì, so che questo è un vecchio post, ma dato che è essenzialmente ciò che cerco, non ha senso crearne uno nuovo quando questo funziona, ma ha solo bisogno di essere aggiornato.

Sebbene sappia programmare in molti linguaggi di programmazione, mi dispiace dire che Ruby non è uno di questi. Mi chiedevo se ci fossero risposte più recenti o migliori a questa domanda.

Interagire con Discourse da Python?


Come domanda secondaria, è possibile creare plugin e/o temi di Discourse utilizzando Python?

1 Mi Piace

Temi, assolutamente no, sono solo Javascript, CSS e HTML.

Plugin: Più o meno.

Puoi importare moduli Python in un plugin Discourse basato su Ruby (on Rails) e chiamare funzioni Python usando Pycall: GitHub - red-data-tools/pycall.rb: Calling Python functions from the Ruby language

Lo uso in produzione per uno scopo molto specifico poiché non esiste un modulo equivalente mantenuto nell’ecosistema RoR, quindi mi evita di dover tradurre e poi mantenere tutto quel codice specialistico in Ruby.

Funziona sorprendentemente bene.

Detto questo, la base di quel plugin è ancora scritta in Ruby.

Ti consiglio semplicemente di imparare anche Ruby on Rails, è davvero un linguaggio molto piacevole e secondo me il codice è più bello da leggere di Python :slight_smile:

Dovrei anche prendermi il tempo per imparare più Python se/quando necessario.

Nota a margine interessante: parte del codice in Discourse Chatbot 🤖 che crea il comportamento dell’agente si basava su un agente Python chiamato “Funkagent” che ho prima tradotto in Ruby on Rails (e poi modificato, migliorato.). Quindi la traduzione può essere un’opzione ovviamente se ha senso farlo. In quel caso aveva senso.

3 Mi Piace

Grazie.

Questa è una risposta ragionevole e un’opzione possibile che terrò in considerazione.

1 Mi Piace

Ho scritto diversi script di importazione prima di aver veramente “imparato” ruby. A seconda di ciò che vuoi fare, potresti essere in grado di fare di più con le tue attuali competenze di quanto pensi. È probabile che sia più facile usare ruby che capire come usare qualcos’altro.

Se stai interagendo con l’API, allora puoi usare qualsiasi linguaggio tu voglia.

5 Mi Piace

Ruby è progettato in modo splendido e facile da imparare, secondo me.

Ruby on Rails richiede un po’ più di apprendimento e, come framework basato sulle convenzioni, a volte può essere complicato ma molto potente una volta che ci si prende la mano.

2 Mi Piace

3 post sono stati divisi in un nuovo argomento: Quanto sarebbe difficile per i non programmatori poter utilizzare il bot AI di Discourse AI per aiutarli a creare plugin e/o temi

Puoi dare un’occhiata a GitHub - discourse/all-the-plugins per vedere cosa è possibile fare.

Scegli qualcosa che vuoi fare che sembri semplice. Se vuoi qualcosa come cakeday che cambia la definizione di un anniversario, allora cakeday potrebbe essere un buon punto di partenza. Selezioneresti una piccola cosa che vuoi cambiare e faresti le cose un piccolo passo alla volta.

2 Mi Piace

hah Sto imparando Ruby da solo in questi giorni, che divertimento davvero. :exploding_head:

3 Mi Piace