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 „Gefällt mir“

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 „Gefällt mir“

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

I will report back with my successes.

2 „Gefällt mir“

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 „Gefällt mir“

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

1 „Gefällt mir“

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 „Gefällt mir“

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 „Gefällt mir“

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 „Gefällt mir“

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

1 „Gefällt mir“

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 „Gefällt mir“

Ja, ich weiß, dass dies ein alter Beitrag ist, aber da er im Wesentlichen dem entspricht, was ich suche, macht es keinen Sinn, einen neuen Beitrag zu erstellen, wenn dieser funktioniert, aber nur auf den neuesten Stand gebracht werden muss.

Obwohl ich in vielen Programmiersprachen programmieren kann, muss ich leider sagen, dass Ruby nicht dazu gehört. Ich habe mich gefragt, ob es neuere oder bessere Antworten auf diese Frage gibt.

Mit Discourse aus Python interagieren?


Als Nebenfrage: Können Discourse-Plugins und/oder -Themes mit Python erstellt werden?

1 „Gefällt mir“

Themes, definitiv nicht, das ist nur Javascript, CSS & HTML.

Plugins: Irgendwie schon.

Sie können Python-Module in ein auf Ruby (on Rails) basierendes Discourse-Plugin importieren und Python-Funktionen mit Pycall aufrufen: GitHub - red-data-tools/pycall.rb: Calling Python functions from the Ruby language

Ich verwende es in der Produktion für einen ganz bestimmten Zweck, da es kein gleichwertiges Modul im RoR-Ökosystem gibt, sodass ich nicht den gesamten spezialisierten Code in Ruby übersetzen und dann pflegen muss.

Es funktioniert überraschend gut.

Das Basis-Plugin ist jedoch immer noch in Ruby geschrieben.

Ich würde Ihnen empfehlen, einfach auch Ruby on Rails zu lernen – es ist wirklich eine sehr angenehme Sprache und meiner Meinung nach ist der Code schöner zu lesen als Python :slight_smile:

Ich sollte mir auch die Zeit nehmen, mehr Python zu lernen, wenn die Notwendigkeit besteht.

Interessante Randnotiz: Ein Teil des Codes in Discourse Chatbot 🤖, der das Agentenverhalten erstellt, basierte auf einem Python-Agenten namens „Funkagent“, den ich zuerst in Ruby on Rails übersetzt (und dann modifiziert, erweitert) habe. Eine Übersetzung kann also natürlich eine Option sein, wenn sie sinnvoll ist. In diesem Fall war sie sinnvoll.

3 „Gefällt mir“

Danke.

Das ist eine vernünftige Antwort und eine mögliche Option, die ich im Auge behalten werde.

1 „Gefällt mir“

Ich habe mehrere Import-Skripte geschrieben, bevor ich Ruby wirklich „gelernt“ habe. Je nachdem, was Sie tun möchten, können Sie mit Ihren aktuellen Fähigkeiten möglicherweise mehr erreichen, als Sie denken. Es ist wahrscheinlich einfacher, Ruby zu verwenden, als herauszufinden, wie man etwas anderes benutzt.

Wenn Sie jedoch mit der API interagieren, können Sie jede beliebige Sprache verwenden.

5 „Gefällt mir“

Ruby ist schön gestaltet und meiner Meinung nach ein Kinderspiel zu lernen.

Ruby on Rails erfordert etwas mehr Lernaufwand und kann als Framework, das auf Konventionen basiert, manchmal fummelig sein, ist aber sehr leistungsfähig, wenn man den Dreh raus hat.

2 „Gefällt mir“

3 Beiträge wurden in ein neues Thema aufgeteilt: Wie schwierig wäre es für Nicht-Programmierer, den Discourse AI - KI-Bot zu nutzen, um ihnen bei der Erstellung von Plugins und/oder Themes zu helfen

Sie können sich GitHub - discourse/all-the-plugins ansehen, um sich umzusehen, was möglich ist.

Wählen Sie etwas, das Sie tun möchten und das einfach erscheint. Wenn Sie etwas wie cakeday möchten, das die Definition eines Jubiläums ändert, dann könnte cakeday ein guter Ausgangspunkt sein. Sie würden eine kleine Sache auswählen, die Sie ändern möchten, und die Dinge Schritt für Schritt tun.

2 „Gefällt mir“

hah, ich bringe mir heutzutage Ruby selbst bei, wie lustig. :exploding_head:

3 „Gefällt mir“