What you describe is impossible – I don’t know how to get, in Ruby code, the message ID header that the mail server appends to emails at the time it sends them, before dispatching the email. That ID doesn’t even exist until the message is being transmitted.
Those ids are created at the time the message is sent by the mail server as far as I know, and they are internal to the mail server software.
Also, the “in reply to” should always be in reply to the original topic creation, the first post of the topic, which you may never have even received an notification about. So there is no “message id” to work with in that case…