Hey there, in case you didn’t know there is Discourse Chatbot (supporting ChatGPT) that already works in topics/chats/PM’s.
So unless you are building your bot for educational purposes, I would just install that if I were you.
I also think that doing this through API, because you probably want to get chat history to improve your AI responses, might be possible but you might bump into some challenges (correct me if I’m wrong).