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.
- Get notifications
- Get post_numbers
- Get post content
- Generate reply with OpenAI
- Post reply
- Mark that same notification as read?
# 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.