Search code examples
pythonxmppejabberdxml-rpc

Receive message of any user in python ejabberd using xmlrpc.client


I am using ejabberd in python and I found a method to send the messages but how to get them messages or receive those messages in my python console please suggest me some method or way to do this.

to send the message my code is

import  xmlrpc.client as xmlrpclib 

server_url = 'http://127.0.0.1:5180/xmlrpc/'
server = xmlrpclib.ServerProxy(server_url)

EJABBERD_XMLRPC_LOGIN = {'user':'yatish', 'server':'localhost', 'password':'1234', 'admin':False}

def ejabberdctl(command, data):
    fn = getattr(server, command)
    print(fn.__dict__,'>>>>>>>>>>')
    return fn(EJABBERD_XMLRPC_LOGIN, data)
result = ejabberdctl('send_message', {"type":"chat","from":"yatish@localhost","to":"1@localhost",
    "subject":"backend subject","body":"Hey this is message from python1"})

here I can send messages from yatish@localhost to 1@localhost user I want to get all the messages received of the 1@lcoalhost, can you please suggest me some method I have checked all the docs and google by my side but unable to get some ay to receive all those messages in python. if the messages received the client should connected and receive the messages relatime.

thanks


Solution

  • You wrote a XMLRPC client to use the ejabberd's "send_message" administrative command to perform this task.

    But there isn't any admin command in ejabberd to check or read XMPP messages.

    I suggest you a different approach: forget about using XMLRPC or ejabberd commands. Instead, write a small XMPP client (there are libraries in python for that, see https://xmpp.org/software/libraries/ ).

    Your XMPP client should:

    1. login to the FROM account
    2. send the message
    3. logout

    Then write another small client that

    1. logins to the TO account, with a possitive presence number
    2. ejabberd will immediately send him the offline messages that were stored
    3. do whatever with those messages, and logout

    If you are able to write those XMPP clients in your prefered language (Python or whatever), you can use those clients with any XMPP server: ejabberd, or any other that you may want to install in other machines, or in the future.