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 إعجابات

نعم، أعلم أن هذا منشور قديم ولكن نظرًا لأنه يلبي ما أبحث عنه بشكل أساسي، فلا داعي لإنشاء منشور جديد عندما يعمل هذا المنشور ولكنه يحتاج فقط إلى تحديث.\n\nبينما أعرف كيفية البرمجة بلغات برمجة عديدة، يؤسفني القول إن Ruby ليست واحدة منها. كنت أتساءل عما إذا كانت هناك إجابات أحدث أو أفضل لهذا السؤال.\n\n> التفاعل مع Discourse من Python؟\n\n—\n\nكسؤال جانبي، هل يمكن إنشاء إضافات و/أو سمات Discourse باستخدام Python؟

إعجاب واحد (1)

السمات، بالتأكيد لا، فهذه فقط JavaScript و CSS و HTML.

الإضافات: نوعًا ما.

يمكنك استيراد وحدات Python إلى إضافة Discourse المستندة إلى Ruby (on Rails) واستدعاء دوال Python باستخدام Pycall: GitHub - red-data-tools/pycall.rb: Calling Python functions from the Ruby language

أستخدمه في بيئة الإنتاج لغرض محدد جدًا حيث لا توجد وحدة مكافئة تتم صيانتها في نظام Ruby on Rails البيئي، لذا فإنه يجنبني الاضطرار إلى ترجمة ثم صيانة كل هذا الكود المتخصص بلغة Ruby.

إنه يعمل بشكل جيد بشكل مدهش.

ومع ذلك، فإن أساس تلك الإضافة لا يزال مكتوبًا بلغة Ruby.

أوصي ببساطة بتعلم Ruby on Rails أيضًا - إنها لغة ممتعة جدًا وفي رأيي الكود أجمل للقراءة من Python :slight_smile:

يجب أن أخصص أيضًا وقتًا لتعلم المزيد من Python إذا/عندما تدعو الحاجة.

ملاحظة جانبية مثيرة للاهتمام: بعض الكود في Discourse Chatbot 🤖 الذي ينشئ سلوك الوكيل كان يعتمد على وكيل Python يسمى “Funkagent” والذي قمت بترجمته أولاً إلى Ruby on Rails (ثم قمت بتعديله وتحسينه.). لذا يمكن أن تكون الترجمة خيارًا بالطبع إذا كان ذلك منطقيًا. في تلك الحالة، كان الأمر منطقيًا.

3 إعجابات

شكرًا.

هذه إجابة معقولة وخيار محتمل سأبقيه مطروحًا.

إعجاب واحد (1)

لقد كتبت العديد من نصوص الاستيراد قبل أن “أتعلم” لغة روبي حقًا. اعتمادًا على ما تريد القيام به، قد تتمكن من فعل المزيد بمجموعة مهاراتك الحالية مما تعتقد. من المحتمل أن يكون استخدام لغة روبي أسهل من معرفة كيفية استخدام شيء آخر.

إذا كنت تتفاعل مع واجهة برمجة التطبيقات (API)، فيمكنك استخدام أي لغة تريدها.

5 إعجابات

روبي مصمم بشكل جميل وسهل التعلم، في رأيي.

يتطلب Ruby on Rails المزيد من التعلم وكإطار عمل يعتمد على الاتفاقيات يمكن أن يكون صعبًا في بعض الأحيان ولكنه قوي جدًا بمجرد أن تتعود عليه.

إعجابَين (2)

تم تقسيم 3 مشاركات إلى موضوع جديد: ما مدى صعوبة تمكين غير المبرمجين من استخدام روبوت الذكاء الاصطناعي Discourse AI - AI لإنشاء المكونات الإضافية و/أو السمات

يمكنك إلقاء نظرة على GitHub - discourse/all-the-plugins كوسيلة للاطلاع على ما هو ممكن.

اختر شيئًا تريد القيام به يبدو بسيطًا. إذا كنت تريد شيئًا مثل “cakeday” الذي يغير تعريف الذكرى السنوية، فيمكن أن يكون “cakeday” مكانًا جيدًا للبدء. ستقوم بتحديد شيء صغير تريد تغييره والقيام بالأشياء خطوة صغيرة واحدة في كل مرة.

إعجابَين (2)

هاها أنا أتعلم روبي بنفسي هذه الأيام، يا له من متعة حقيقية. :exploding_head:

3 إعجابات