pyxmpp2
pyxmpp2 copied to clipboard
Allow HTML formatted messages
I have tried to send a message containing bold text. Unfortunaly the API does not allow to pass html tags unmodified.
A Jabber message with format looks this:
<message from="[email protected]/blabla" type="chat" to="[email protected]" id="purple5cc6aad7" >
<active xmlns="http://jabber.org/protocol/chatstates"/>
<body>bold italic</body>
<html xmlns="http://jabber.org/protocol/xhtml-im">
<body xmlns="http://www.w3.org/1999/xhtml">
<p>
<span style="font-weight: bold;" >bold <em>italic</em>
</span>
</p>
</body>
</html>
</message>
PyXMPP2 doesn't have any dedicated XHTML-IM support yet, but you can add the XHTML code to the Message stanza as an XMLPayload object. Like this:
from pyxmpp2.etree import ElementTree
from pyxmpp2.message import Message
from pyxmpp2.stanzapayload import XMLPayload
message = Message(to_jid=recipient, stanza_type='chat', body='bold italic')
formatted = ElementTree.XML('''<html xmlns="http://jabber.org/protocol/xhtml-im">
<body xmlns="http://www.w3.org/1999/xhtml">
<p>
<span style="font-weight: bold;" >bold <em>italic</em>
</span>
</p>
</body>
</html>''')
message.add_payload(XMLPayload(formatted))
Dedicated support for XHTML-IM may be added to PyXMPP2 some day, but it is not high priority (much more important things are still missing).
Hi! I am having a similar issue here, but on the other way. I'd like to receive XML messages from a Jabber server, but it mess with the message parser. Is there any easy way to do this? Maybe overriding the client function fix_in_stanza(stanza)?
@wschoenell How dos it mess with the message parser? Any XMPP message must be a well-formed XML element and as such is available through the PyXMPP2 API without any data loss. Why do you think you should override fix_in_stanza()?
Hi... I'm sorry... I was using the wrong jabber list to do the tests. The one that I was using had a malformed XML stream. I changed the list and everything works. :) Thanks for your prompt answer!