For Odoo 18 at least, you could define a template and use it with message_post_with_source(). Pass on any values you want to render within that template in the render_values dict.
self.with_user(sender_user_id).message_post_with_source(
'your_module.your_message_template',
render_values={'self': self, 'message': message, 'title': title, 'message_type': message_type,
'message_tag': message_tag, 'data': json.dumps(data), 'url': url,
'some_result': some_result},
subtype_xmlid='mail.mt_note',
)
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<template id="your_message_template">
<p>
<span t-attf-class="badge text-#{message_type}"
style="border: 1px solid; border-radius: 10px;"
t-esc="message_tag"/> <t t-if="title"><t t-esc="title"/></t><br t-if="message"/>
<t t-esc="message"/>
</p>
<blockquote>
<span class="fst-normal"><b>URL:</b> <t t-esc="url"/></span><br/>
<span class="fst-normal"><b>Request:</b> <br/><code t-esc="data"/></span><br/>
<b class="fst-normal">Response:</b> <br/><t t-esc="some_result"/>
</blockquote>
</template>
</odoo>
Results in:

Since it's a template, go wild
Did you solve this? I got the same problem.