pythonxmppsleekxmpp

Send XMPP message with custom element in message stanza using SleekXMPP


I'm developing a XMPP bot that will send/reply a message to the sender with a addition element as following

<message to="user1@example.com" from="user2@example.com">
  <my_element_1>foo</my_element_1> 
  <my_element_2>bar</my_element_2>
  <body>Hi!</body>
</message>

I've tried to used SleekXMPP Object, Iq, Message but not able to achieve the target. Beside, I also tried to use send_message method.

I'm not sure if my googling keyword is not correct but I'm not able to find any documentation or guide related to this. However, I did found guide that might be helpful for this related to plugin creation. Will be much appreciated if someone could help me on this.

Thank you


Solution

  • I just realised that my understanding is not correct. Technically, to achieve this, I just need to do the following.

    # create XML for element 1
    element1 = ET.Element('my_element_1')
    element1.text = 'foo'
    
    # create XML for element 2
    element2 = ET.Element('my_element_2')
    element2.text = 'foo'
    

    If needed, I can simply add child for the xml.

    child = ET.Element('child-key')
    child.text = 'child-value'
    media.append(child)
    

    In the Message object, I would just append as following

    msg = self.Message()
    msg.appendxml(element1)
    msg.appendxml(element2)
    msg.send()