pythondiscourse

Create a Discourse topic and immediately reply to that newly created topic


I've got a project here at work in which we automate the daily release of our offerings in Python, posted to our Discourse. The problem is that the 150,000 character limit has gotten in the way for some of the larger releases. My solution has been to split this post as many times as needed to avoid the limitation however, I'm unsure of how to achieve what I mentioned in the title.

Documentation for the Discourse API isn't really too clear to me and I've found conflicting information in several other Stack Overflow posts so I'd really love some clarification on this.

Creating topics is no issue and we've been doing so for a while but, replying to that topic is something I'm struggling with. How is this achieved? I've found some mention of 'topic_slug' which I've carved out in the script (instead of parsing the response from the topic creation, maybe that'll bite me at some point but we'll see). How is the 'topic_slug' utilised here? I've also seen mentions that replies can be either PUT or POST but I can't find this information (clearly) in the documentation.

If anybody could be so kind as to provide a simple Python snippet demonstrating what a reply would look like, I'd greatly appreciate it.


Solution

  • Seems my first attempt at implementing this actually worked.

    From the POST request creating the topic, I parse the response and pull the 'topic_id' key for use in the reply.

    resp_json = json.loads(r.text)
    topic_id = resp_json['topic_id']
    

    Then it's as simple as provided the reply text, the category (unsure if this is actually needed), and the 'topic_id' parsed from above.

    json_post_req = {
        "raw": disc_body_list[idx],
        "category": settings.discourse.category,
        "topic_id": topic_id
    }
    

    I also added a 10 second sleep in there before the reply POST due to Discourse rate limiting.

    Still unsure what the 'topic_slug' is for but, at least this works.