我假设您想在帖子中提及某个用户名(例如 UserA)时,由 OpenAI 生成回复。如果正确,那么您不必向 Discourse 发出 API 请求来获取通知,而是可以将 Discourse 通知事件 webhook 指向您的 Python 应用程序服务器。Python 应用程序需要查看 webhook 的载荷。如果载荷的 notification_type 设置为 1,并且其 user_id 与您要获取通知的用户 ID 匹配,您就可以向 Discourse 发出 API 请求,以获取载荷的 original_post_id 字段中设置的帖子。然后将该请求返回的帖子内容发送给 OpenAI。
以下是提及通知载荷的示例:
{
"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"
}
}
}
这种方法的优点是,您可以实时获取通知,而无需向您的 Discourse 站点发出大量 API 请求。
如果您选择 webhook 方法,最好在 Discourse 上设置 webhook 的“Secret”字段,然后在您的 Python 应用程序上验证请求,以确保密钥设置正确。我还没有尝试设置 Python 应用程序上的路由来实现这一点。有关如何在 WordPress 上验证 webhook 的 Secret 的详细信息,请参见:wp-discourse-custom-webhook-request/wp-discourse-custom-webhook-request.php at master · scossar/wp-discourse-custom-webhook-request · GitHub