I’m assuming that what you want to do is generate a response from OpenAI when a given username (for example, UserA) is mentioned in a post. If that’s correct, instead of making API requests to Discourse to get the notifications, you could point a Discourse Notification Event webhook to your Python application’s server. The Python application would need to look at the webhook’s payload. If the payload’s notification_type was set to 1, and its user_id matched the ID of the user you are interested in getting notifications for, you’d make an API request to Discourse to get the post that’s set in the payload’s original_post_id field. Then send the post content that’s returned from that request on to OpenAI.
Here’s an example of what the payload for a mention notification looks like:
{
"notification": {
"id": 55267,
"user_id": 2,
"notification_type": 1,
"read": false,
"high_priority": false,
"created_at": "2022-12-08T23:23:36.379Z",
"post_number": 5,
"topic_id": 277,
"fancy_title": "I should be able to call this topic anything",
"slug": "i-should-be-able-to-call-this-topic-anything",
"data": {
"topic_title": "I should be able to call this topic anything",
"original_post_id": 10999,
"original_post_type": 1,
"original_username": "scossar",
"revision_number": null,
"display_username": "scossar"
}
}
}
The benefit of this approach is that it would let you get notifications in real time - without having to make large numbers of API requests to your Discourse site.
If you go with the webhook approach, it would be a good idea to set the webhook’s “Secret” field on Discourse, then validate the request on your Python application to make sure that the secret key is correctly set. I haven’t tried setting up a route on a Python application to do this yet. Details about how to validate a webhook’s Secret on WordPress are here: wp-discourse-custom-webhook-request/wp-discourse-custom-webhook-request.php at master · scossar/wp-discourse-custom-webhook-request · GitHub.