# Python \> Get post content if mentioned

**URL:** <https://meta.discourse.org/t/python-get-post-content-if-mentioned/248309>\
**Category:** Development\
**Tags:** mentions\
**Created:** [December 8, 2022, 6:49pm UTC](https://meta.discourse.org/t/python-get-post-content-if-mentioned/248309 "2022-12-08T18:49:25Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![MarcP](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/marcp/32/160184_2.png) [@MarcP](https://meta.discourse.org/u/MarcP)\
**Post date:** [December 8, 2022, 10:46pm UTC](https://meta.discourse.org/t/python-get-post-content-if-mentioned/248309/3 "2022-12-08T22:46:44Z")

</div>

This is neat. Thanks for the info. There is not many information on Python x Discourse available out here, so happy to learn, and perhaps this will help others. I just tried your code, and it seems to work as expected. Of course, I didn’t stop playing, so I’ve found a workaround to get up until step 5, and a different way to get the values I needed all the way up to a raw paste of the post content.

What I want is basically this.

1. Get notifications
2. Get post\_numbers
3. Get post content
4. Generate reply with OpenAI
5. Post reply
6. Mark that same notification as read?

```plaintext
# Set up a loop to run the bot indefinitely
while True:
    # Get user_actions and fetch post id    
    data = {
    'username': 'theuser',
    # For mentions and replies, I need to get filters 6 + 7, but for some reason only 1 json response is given.
    'filter': 6 | 7,
    'limit': 10,
    }
    
    unread_mentions = client.user_actions.json.get(data)
    print(unread_mentions)

    user_actions = unread_mentions['user_actions']

    # Loop over each unread mention
    for user_action in unread_mentions['user_actions']:
        topic_id = user_action['topic_id']
        post_number = user_action['post_number']
        post_id = user_action['post_id']
        username = user_action['username']

        print(f'Topic ID: {topic_id}, Post Number: {post_number}, Post ID: {post_id}, Reply to: {username}')
    
        # Get the post content   
        post = client.posts[post_id].json.get()

        # Print the post content
        post_content = post['raw']

        # Generate a response to the post
        response = generate_response(post_content)
    
        # Post a response to the forum
        content = {
            'topic_id': topic_id,
            'raw': response + ' @' + str(username)
        }
        make_post = client.posts.json.post(content)

```

Currently I’m having two problems:

- Only 1 mention is fetched. I must’ve forgot something, but haven’t found out why yet.
- Ofcourse we want to reply to a post only one time, and since we are looping we need something here to prevent this.
  - We can store the post\_id in a .txt file (not sure how many data this can store, I think you can handle quite a few mentions here.
  - My ideal solution, was to mark the notification as read. But I came to realize in none of our code, we are fetching **unread** mentions (well, I tried at first, fetching unread notifications, but I was unable to extract/store the post\_id variable which I needed to use /posts/get.json and fetch the “raw” content.

My end goal is to feed the response with OpenAI API. But I first need to find a way to sort out the problems above.

---

_[View the full topic](https://meta.discourse.org/t/python-get-post-content-if-mentioned/248309)._
