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 交互?


作为附带问题,可以使用 Python 创建 Discourse 插件和/或主题吗?

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 可以是一个很好的起点。你需要选择一个你想改变的小东西,然后一次只做一小步。

2 个赞

哈哈,我最近在自学 Ruby,确实很有趣。 :exploding_head:

3 个赞