Making a difference between public topic and private message on API

AI-generated summary

TheChachara is building a bot that tweets new topics from their Discourse instance but struggles to distinguish between public topics and private messages, since both are structured as posts. They shared sample post objects for both types, noting no obvious field differentiates them at the post level. pfaffman advises checking the associated topic record via the topic_id field in the post — the topic object contains a field indicating whether it’s a private message. TheChachara clarifies they only process first posts (post_number => 1) and asks if more data can be fetched by querying the post ID directly. pfaffman confirms they must retrieve the topic record to determine the post’s context. The goal is to avoid manually updating category whitelists and instead automate filtering based on topic type.

Hi y’all.
I’ve created a very tiny bot for our discourse installation which fires upon a webhook.
I’ve also connected the bot to Twitter, so every time anybody creates a new topic, a tweet is posted on our Twitter account (Thanks to Abraham for his https://twitteroauth.com)

The thing is that, because of how Discourse is created, everything is a post. Both public and private messages.
In my particular case, here’s the post object from a public post:

stdClass Object
(
    [post] => stdClass Object
        (
            [id] => 909
            [username] => TheChachara
            [avatar_template] => /user_avatar/thechachara.com/thechachara/{size}/46_2.png
            [created_at] => 2019-02-01T11:50:23.974Z
            [cooked] => <p>Posted on a public category.</p>
            [post_number] => 1
            [post_type] => 1
            [updated_at] => 2019-02-01T11:50:23.974Z
            [reply_count] => 0
            [reply_to_post_number] => 
            [quote_count] => 0
            [avg_time] => 
            [incoming_link_count] => 0
            [reads] => 0
            [score] => 0
            [topic_id] => 63
            [topic_slug] => this-is-a-public-topic
            [topic_title] => This is a public topic
            [primary_group_name] => 
            [version] => 1
            [user_title] => 
            [moderator] => 
            [admin] => 1
            [staff] => 1
            [user_id] => 1
            [hidden] => 
            [trust_level] => 1
            [deleted_at] => 
            [user_deleted] => 
            [edit_reason] => 
            [wiki] => 
            [topic_posts_count] => 1
        )

)

On the other hand, this is a private message:

stdClass Object
(
    [post] => stdClass Object
        (
            [id] => 908
            [username] => TheChachara
            [avatar_template] => /user_avatar/thechachara.com/thechachara/{size}/46_2.png
            [created_at] => 2019-02-01T11:37:14.954Z
            [cooked] => <p>Esto es un mensaje de prueba</p>
            [post_number] => 1
            [post_type] => 1
            [updated_at] => 2019-02-01T11:37:14.954Z
            [reply_count] => 0
            [reply_to_post_number] => 
            [quote_count] => 0
            [avg_time] => 
            [incoming_link_count] => 0
            [reads] => 0
            [score] => 0
            [topic_id] => 62
            [topic_slug] => nuevo-mensaje-publicado
            [topic_title] => Nuevo mensaje publicado
            [primary_group_name] => 
            [version] => 1
            [user_title] => 
            [moderator] => 
            [admin] => 1
            [staff] => 1
            [user_id] => 1
            [hidden] => 
            [trust_level] => 1
            [deleted_at] => 
            [user_deleted] => 
            [edit_reason] => 
            [wiki] => 
            [topic_posts_count] => 1
        )

)

I can’t find a way to make a difference between them.

The solution I came to is to set which categories to report by the webhook, but I would like to control it from the bot, because tomorrow (yep, Dory’s memory here) I could add a new category, and forget to allow this category to be checked when sending the webhook.

Is there a way to send more data over the webhook?

TiA people :slight_smile:

1 Like

You’ll need to look at the topic that the post belongs to. There is a field there that tells you if it is a pm.

OK, sorry not to mention. I only process those posts that are

[post_number] => 1

Which means it’s the start of a thread.


Or maybe you’re saying that I can retrieve the more data by requesting the post by its id ?

You need to pull the topic record. There is a topic_id field in the post record.

2 Likes