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

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

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

I will report back with my successes.

「いいね!」 2

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

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

「いいね!」 1

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

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

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

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

「いいね!」 1

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

はい、これは古い投稿であることは承知していますが、本質的に私が求めているものなので、この投稿を最新の状態に保つだけで新しい投稿を作成する必要はないでしょう。

多くのプログラミング言語でプログラムを書く方法を知っていますが、残念ながらRubyはその一つではありません。この質問に対する新しい、より良い答えはありますか?

PythonからDiscourseと対話する?


余談ですが、DiscourseプラグインやテーマはPythonで作成できますか?

「いいね!」 1

テーマは絶対に無理です。JavaScript、CSS、HTMLのみです。

プラグイン:まあ、できます。

Ruby(on Rails)ベースのDiscourseプラグインにPythonモジュールをインポートし、Pycallを使用してPython関数を呼び出すことができます:GitHub - red-data-tools/pycall.rb: Calling Python functions from the Ruby language

RoRエコシステムで同等のモジュールがメンテナンスされていないため、非常に特定の目的で本番環境で使用しています。これにより、その専門的なコードをRubyに翻訳してからメンテナンスする必要がなくなります。

驚くほどうまく機能します。

とはいえ、そのプラグインの基本はまだRubyで書かれています。

Ruby on Railsも学ぶことをお勧めします。非常に快適な言語であり、私の意見ではPythonよりもコードが読みやすいです:slight_smile

必要に応じて、Pythonについてももっと学ぶ時間を取りたいと思います。

興味深いサイドノート:Discourse Chatbot 🤖 にあるエージェントの動作を作成するコードの一部は、「Funkagent」というPythonエージェントに基づいており、最初にRuby on Railsに翻訳(その後、変更、強化)しました。したがって、意味がある場合は、翻訳も選択肢になります。その場合、意味がありました。

「いいね!」 3

ありがとうございます。

それは妥当な回答であり、検討すべき選択肢です。

「いいね!」 1

私はRubyを「学んだ」前にいくつかのインポートスクリプトを書きました。やりたいことによっては、現在のスキルセットで思っている以上にできることがあるかもしれません。Rubyを使う方が、何か他のものを使いこなす方法を考えるよりも簡単でしょう。

ただし、APIとやり取りするのであれば、好きな言語を使用できます。

「いいね!」 5

Rubyは美しくデザインされており、私の意見では簡単に習得できます。

Ruby on Railsはもう少し学習が必要で、フレームワークとして慣習に依存しているため、時には扱いにくいこともありますが、慣れてしまえば非常に強力です。

「いいね!」 2

3件の投稿が新しいトピックに分割されました: プログラマー以外が Discourse AI - AI ボットを使用してプラグインやテーマを作成するのを支援するのはどれくらい難しいでしょうか

何が可能かを見る方法として、GitHub - discourse/all-the-plugins を確認できます。

シンプルに見える、やりたいことを選択してください。記念日の定義を変更する cakeday のようなものが欲しい場合は、cakeday が良い出発点になるかもしれません。変更したい小さなことを1つ選択し、一度に1つの小さなステップで物事を行います。

「いいね!」 2

ハハ、最近独学でRubyを学んでいます。本当に楽しいです。 :exploding_head:

「いいね!」 3