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 Likes

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 Likes

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

I will report back with my successes.

2 Likes

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 Likes

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

1 Like

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 Likes

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 Likes

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 Like

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

1 Like

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 Likes

Yes I know this is an old post but since it is essentially what I seek no sense it creating a new post when this one works but just needs to be brought up to date.

While I do know how to program in many programming languages, sorry to say Ruby is not one. Was wondering if there are newer are better answers to this question.

Interact with Discourse from Python?


As a side question, can Discourse Plugins and/or themes be created using Python?

1 Like

Themes, definitely not, that’s only Javascript, CSS & HTML.

Plugins: Kind of.

You can import Python modules into a Ruby (on Rails) based Discourse plugin and call Python functions using Pycall: GitHub - mrkn/pycall.rb: Calling Python functions from the Ruby language

I use it in Production for a very specific purpose as there is no equivalent module maintained in the RoR ecosystem so it avoids me having to translate and then maintain all that specialist code in Ruby.

It works surprisingly well.

That said, the base of that Plugin is still written in Ruby.

I would recommend you simply learnt Ruby on Rails as well - it really is a very pleasant language and imho the code is nicer to read than Python :slight_smile:

I should also take the time to learn more Python if/when needs must.

Interesting side note: some of the code in Discourse Chatbot 🤖 (Now smarter than ChatGPT!*) that creates the agent behaviour was based on a Python agent called “Funkagent” which I first translated into Ruby on Rails (and then modified, enhanced.). So translation can be an option of course if it makes sense to do so. In that case it made sense.

3 Likes

Thanks.

That is a reasonable answer and a possible option that I will keep on the table.

1 Like

I wrote several import scripts before I really “learned” ruby. Depending on what you want to do you might be able to do more with your current skill set than you think. It’s likely easier to use ruby than figure out how to use something else.

If you’re interacting with the API, though, then you can use whatever language you want.

5 Likes

Ruby is beautifully designed and a doddle to learn, imho.

Ruby on Rails requires a bit more learning and as a framework relying on conventions can be fiddly at times but very powerful once you get the hang of it.

2 Likes

3 posts were split to a new topic: How hard would it be for non programmers to be able to use the Discourse AI - AI bot to help them create plugins and/or themes

You can have a look at GitHub - discourse/all-the-plugins as a way to look around at what’s possible.

Choose something that you want to do that seems simple. If you want something like cakeday that changes the definition of an anniversary, then cakeday could be a fine place to start. You’d select on small thing you want to change and do things one tiny step at a time.

2 Likes

hah I am teaching myself Ruby these days, what fun indeed. :exploding_head:

3 Likes