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

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

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:
login to the FROM account
send the message
logout
Then write another small client that
logins to the TO account, with a possitive presence number
ejabberd will immediately send him the offline messages that were stored
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.

Related

How can I send email through python script when my company email is hosted on Google

Just like many big companies using Office365, my company is using google (gsuite) to host their email domain. I need to send automated emails to multiple people within organisation using a python script. How can that be done?
You can use a 3rd party service like Mailgun, it provides a REST API which if you hit you can trigger emails that it will send from a custom domain you configure on the service.
Its super easy to use for python, I use it for Raspberry Pi projects.
def send_simple_message():
return requests.post(
"https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages",
auth=("api", "YOUR_API_KEY"),
data={"from": "Excited User <mailgun#YOUR_DOMAIN_NAME>",
"to": ["bar#example.com", "YOU#YOUR_DOMAIN_NAME"],
"subject": "Hello",
"text": "Testing some Mailgun awesomness!"})
It is a nice alternative to using a corporate SMTP server.
Got it fixed.
In order to send an email from Python, we first need to switch ON "Less secure app access" https://myaccount.google.com/lesssecureapps?utm_source=google-account&utm_medium=web.
This we need to do if we don't have 2 Factor Authentication.
If you use 2 Factor Authentication, then you need to create an App Password and use that particular password while sending an email and not your regular password.
To create an App Password use this link: https://support.google.com/mail/answer/185833?hl=en
Now using sample script like below, we can send an email.
import smtplib
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
# start TLS for security
s.starttls()
# Authentication
s.login("username#domain.com", "app_password")
# message to be sent
message = "Message_you_need_to_send"
# sending the mail
s.sendmail("username#domain.com", "recipient#domain.com", message)
# terminating the session
s.quit()
Google provides Gmail api suite for python and it is the preferred way to access versus smtp login/password
You should refer to their developer console for examples and tutorials

How can I send a push notification without prompting the password each time?

I am using the apns library to send push notifications to my iPhone. While I'm successfully receiving push notifications to my device, the script asks for the Certificate password every time I run it to send a push notification. I'm trying to figure out a way to include the password somehow in the code but have been unsuccessful so far.
I'm running the example code from their repository:
from apns import APNs, Payload
apns = APNs(use_sandbox=True, cert_file='TestCert.pem', key_file='TestKey.pem')
# Send a notification
token_hex = '<token hidden for obvious reasons>'
payload = Payload(alert="Hello World!", sound="default", badge=1)
apns.gateway_server.send_notification(token_hex, payload)
I've also been checking the library source code but it doesn't seem to have any useful option.
I've finally found a decent solution on the answer to this question.
I've also found out that from Python 3.3 it's possible to use the password adding the method SSLContext.load_cert_chain(certfile, keyfile, password) in the function that opens the SSL connection.

How to send message using xmpppy to a jabber client?

I've to send xmpp based chat client (hipchat) and I'm using xmpp.py for this purpose. For now, I'm trying to send message from shell. Following are the statements that I'm executing from the shell:
>>> import xmpp
>>> jid = xmpp.protocol.JID('99999_9999#chat.hipchat.com')
>>> cl=xmpp.Client(jid.getDomain(),debug=[])
>>> cl.connect()
'tls'
>>> cl.auth(jid.getNode(),'password')
'sasl'
>>> cl.send(xmpp.protocol.Message('99999_9999#chat.hipchat.com','hey!'))
'3'
I'm using the same jabber id for authentication and as a receiver. I'm also online in chat room but I don't get any message. What's missing?
Some older XMPP servers need an initial presence state.
The state is sent using the following call before the cl.send:
cl.SendInitPresence(requestRoster=0)
See also the xsend example from xmpppy home page:
http://xmpppy.sourceforge.net/examples/xsend.py
I was missing the typ parameter. Adding it with value chat solved the problem:
cl.send(xmpp.protocol.Message('99999_9999#chat.hipchat.com','hey!', typ='chat'))

Need to send a HTTP request in Python that'll authenticate with Google Accounts

I have an app which amounts to a Python script, running on the user's phone, and a JS client, running in the user's browser. The Python script sends messages to App Engine as HTTP requests. The server then pushes the messages to the JS client.
The problem is authentication: The server can easily use Google Accounts to authenticate anything coming from the JS client as being sent by a particular user, but I do not know how to get the Python script to make HTTP requests which will also authenticate.
Any ideas?
According to its homepage, httplib2 has support for Google Account authentication, maybe that may help you?
Can you use OAUth to authenticate with Google, then use the OAuth token to ensure the messages are legitimate?

Is there an ejabberd python library?

Is there an ejabberd python library wherein I can register user to ejabberd from python programmatically?
Right now I'm executing "ejabberdctl register" command using the python commands module.
XMPP XEP-0077
If you have activated mod_register for In-Band registration on your Ejabberd server, then, as pointed out by #Drake, you can use an XMPP library to register users.
In Python, I would recommend Sleek XMPP. The Getting started examples are, well, a good starting point.
HTTP
If you have activated mod_register_web then you can send a HTTP POST request to http://<SERVERNAME>:5280/admin/server/<VIRTUALHOSTNAME>/users/. This URL expects the following 3 parameters:
newusername
newuserpassword
addnewuser
where the expected value for the addnewuser param seems to be the string "Add User".
Assuming you have an ejabberd admin user called user and with password password, using the requests HTTP library for Python, you could do something like the following:
import requests
from requests.auth import HTTPBasicAuth
server = "NAME OF YOUR EJABBERD SERVER"
virtualhost = "NAME OF YOUR EJABBERD HOST"
url = "http://%s:5280/admin/server/%s/user/" % (server, virtualhost)
auth = HTTPBasicAuth("user", "password")
data = {
'newusername': "new_user",
'newuserpassword': "new_password",
'addnewuser': "Add User"
}
resp = requests.post(url, data=data, auth=auth)
assert resp.status_code == 200
ejabberd is a Jabber/XMPP instant messaging server. That means you can make use of any XMPP module, like xmppy.
Also, check this thread: Which is the most mature Python XMPP library for a GChat client?.

Categories