2011-08-18 04:21:37 +00:00
|
|
|
.. _echobot:
|
|
|
|
|
2011-08-14 03:58:53 +00:00
|
|
|
===============================
|
2014-07-17 12:19:04 +00:00
|
|
|
Slixmpp Quickstart - Echo Bot
|
2011-08-14 03:58:53 +00:00
|
|
|
===============================
|
|
|
|
|
|
|
|
.. note::
|
2014-08-17 19:53:34 +00:00
|
|
|
|
2011-08-14 03:58:53 +00:00
|
|
|
If you have any issues working through this quickstart guide
|
2015-02-24 17:58:40 +00:00
|
|
|
join the chat room at `slixmpp@muc.poez.io
|
|
|
|
<xmpp:slixmpp@muc.poez.io?join>`_.
|
2011-08-14 03:58:53 +00:00
|
|
|
|
2014-07-17 12:19:04 +00:00
|
|
|
If you have not yet installed Slixmpp, do so now by either checking out a version
|
2019-02-12 10:34:57 +00:00
|
|
|
with `Git <https://lab.louiz.org/poezio/slixmpp>`_.
|
2011-08-14 03:58:53 +00:00
|
|
|
|
|
|
|
As a basic starting project, we will create an echo bot which will reply to any
|
|
|
|
messages sent to it. We will also go through adding some basic command line configuration
|
|
|
|
for enabling or disabling debug log outputs and setting the username and password
|
|
|
|
for the bot.
|
|
|
|
|
|
|
|
For the command line options processing, we will use the built-in ``optparse``
|
|
|
|
module and the ``getpass`` module for reading in passwords.
|
|
|
|
|
|
|
|
TL;DR Just Give Me the Code
|
|
|
|
---------------------------
|
|
|
|
As you wish: :ref:`the completed example <echobot_complete>`.
|
|
|
|
|
|
|
|
Overview
|
|
|
|
--------
|
|
|
|
|
|
|
|
To get started, here is a brief outline of the structure that the final project will have:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import sys
|
2015-02-24 17:58:40 +00:00
|
|
|
import asyncio
|
2011-08-14 03:58:53 +00:00
|
|
|
import logging
|
|
|
|
import getpass
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
2014-07-17 12:19:04 +00:00
|
|
|
import slixmpp
|
2011-08-14 03:58:53 +00:00
|
|
|
|
|
|
|
'''Here we will create out echo bot class'''
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
'''Here we will configure and read command line options'''
|
|
|
|
|
|
|
|
'''Here we will instantiate our echo bot'''
|
|
|
|
|
|
|
|
'''Finally, we connect the bot and start listening for messages'''
|
|
|
|
|
|
|
|
Creating the EchoBot Class
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
There are three main types of entities within XMPP — servers, components, and
|
|
|
|
clients. Since our echo bot will only be responding to a few people, and won't need
|
|
|
|
to remember thousands of users, we will use a client connection. A client connection
|
|
|
|
is the same type that you use with your standard IM client such as Pidgin or Psi.
|
|
|
|
|
2014-07-17 12:19:04 +00:00
|
|
|
Slixmpp comes with a :class:`ClientXMPP <slixmpp.clientxmpp.ClientXMPP>` class
|
|
|
|
which we can extend to add our message echoing feature. :class:`ClientXMPP <slixmpp.clientxmpp.ClientXMPP>`
|
2011-08-14 03:58:53 +00:00
|
|
|
requires the parameters ``jid`` and ``password``, so we will let our ``EchoBot`` class accept those
|
|
|
|
as well.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2014-07-17 12:19:04 +00:00
|
|
|
class EchoBot(slixmpp.ClientXMPP):
|
2014-08-17 19:53:34 +00:00
|
|
|
|
2011-08-14 03:58:53 +00:00
|
|
|
def __init__(self, jid, password):
|
2016-09-30 19:25:36 +00:00
|
|
|
super().__init__(jid, password)
|
2011-08-14 03:58:53 +00:00
|
|
|
|
|
|
|
Handling Session Start
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
The XMPP spec requires clients to broadcast its presence and retrieve its roster (buddy list) once
|
|
|
|
it connects and establishes a session with the XMPP server. Until these two tasks are completed,
|
|
|
|
some servers may not deliver or send messages or presence notifications to the client. So we now
|
2014-08-17 19:53:34 +00:00
|
|
|
need to be sure that we retrieve our roster and send an initial presence once the session has
|
2011-08-14 03:58:53 +00:00
|
|
|
started. To do that, we will register an event handler for the :term:`session_start` event.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
def __init__(self, jid, password):
|
2016-09-30 19:25:36 +00:00
|
|
|
super().__init__(jid, password)
|
2011-08-14 03:58:53 +00:00
|
|
|
|
|
|
|
self.add_event_handler('session_start', self.start)
|
|
|
|
|
|
|
|
|
|
|
|
Since we want the method ``self.start`` to execute when the :term:`session_start` event is triggered,
|
|
|
|
we also need to define the ``self.start`` handler.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
def start(self, event):
|
|
|
|
self.send_presence()
|
|
|
|
self.get_roster()
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
Not sending an initial presence and retrieving the roster when using a client instance can
|
|
|
|
prevent your program from receiving presence notifications or messages depending on the
|
|
|
|
XMPP server you have chosen.
|
|
|
|
|
|
|
|
Our event handler, like every event handler, accepts a single parameter which typically is the stanza
|
|
|
|
that was received that caused the event. In this case, ``event`` will just be an empty dictionary since
|
|
|
|
there is no associated data.
|
|
|
|
|
2014-07-17 12:19:04 +00:00
|
|
|
Our first task of sending an initial presence is done using :meth:`send_presence <slixmpp.basexmpp.BaseXMPP.send_presence>`.
|
|
|
|
Calling :meth:`send_presence <slixmpp.basexmpp.BaseXMPP.send_presence>` without any arguments will send the simplest
|
2011-08-14 03:58:53 +00:00
|
|
|
stanza allowed in XMPP:
|
|
|
|
|
|
|
|
.. code-block:: xml
|
|
|
|
|
|
|
|
<presence />
|
|
|
|
|
|
|
|
|
2014-07-17 12:19:04 +00:00
|
|
|
The second requirement is fulfilled using :meth:`get_roster <slixmpp.clientxmpp.ClientXMPP.get_roster>`, which
|
2011-08-14 03:58:53 +00:00
|
|
|
will send an IQ stanza requesting the roster to the server and then wait for the response. You may be wondering
|
2014-07-17 12:19:04 +00:00
|
|
|
what :meth:`get_roster <slixmpp.clientxmpp.ClientXMPP.get_roster>` returns since we are not saving any return
|
2011-08-14 03:58:53 +00:00
|
|
|
value. The roster data is saved by an internal handler to ``self.roster``, and in the case of a :class:`ClientXMPP
|
2014-07-17 12:19:04 +00:00
|
|
|
<slixmpp.clientxmpp.ClientXMPP>` instance to ``self.client_roster``. (The difference between ``self.roster`` and
|
2011-08-14 03:58:53 +00:00
|
|
|
``self.client_roster`` is that ``self.roster`` supports storing roster information for multiple JIDs, which is useful
|
|
|
|
for components, whereas ``self.client_roster`` stores roster data for just the client's JID.)
|
|
|
|
|
|
|
|
It is possible for a timeout to occur while waiting for the server to respond, which can happen if the
|
|
|
|
network is excessively slow or the server is no longer responding. In that case, an :class:`IQTimeout
|
2014-07-17 12:19:04 +00:00
|
|
|
<slixmpp.exceptions.IQTimeout>` is raised. Similarly, an :class:`IQError <slixmpp.exceptions.IQError>` exception can
|
2011-08-14 03:58:53 +00:00
|
|
|
be raised if the request contained bad data or requested the roster for the wrong user. In either case, you can wrap the
|
|
|
|
``get_roster()`` call in a ``try``/``except`` block to retry the roster retrieval process.
|
|
|
|
|
|
|
|
The XMPP stanzas from the roster retrieval process could look like this:
|
|
|
|
|
|
|
|
.. code-block:: xml
|
|
|
|
|
|
|
|
<iq type="get">
|
|
|
|
<query xmlns="jabber:iq:roster" />
|
|
|
|
</iq>
|
|
|
|
|
|
|
|
<iq type="result" to="echobot@example.com" from="example.com">
|
|
|
|
<query xmlns="jabber:iq:roster">
|
|
|
|
<item jid="friend@example.com" subscription="both" />
|
|
|
|
</query>
|
|
|
|
</iq>
|
|
|
|
|
|
|
|
Responding to Messages
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
Now that an ``EchoBot`` instance handles :term:`session_start`, we can begin receiving and
|
|
|
|
responding to messages. Now we can register a handler for the :term:`message` event that is raised
|
|
|
|
whenever a messsage is received.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
def __init__(self, jid, password):
|
2016-09-30 19:25:36 +00:00
|
|
|
super().__init__(jid, password)
|
2011-08-14 03:58:53 +00:00
|
|
|
|
|
|
|
self.add_event_handler('session_start', self.start)
|
|
|
|
self.add_event_handler('message', self.message)
|
|
|
|
|
|
|
|
|
|
|
|
The :term:`message` event is fired whenever a ``<message />`` stanza is received, including for
|
|
|
|
group chat messages, errors, etc. Properly responding to messages thus requires checking the
|
|
|
|
``'type'`` interface of the message :term:`stanza object`. For responding to only messages
|
|
|
|
addressed to our bot (and not from a chat room), we check that the type is either ``normal``
|
|
|
|
or ``chat``. (Other potential types are ``error``, ``headline``, and ``groupchat``.)
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
def message(self, msg):
|
|
|
|
if msg['type'] in ('normal', 'chat'):
|
|
|
|
msg.reply("Thanks for sending:\n%s" % msg['body']).send()
|
|
|
|
|
|
|
|
Let's take a closer look at the ``.reply()`` method used above. For message stanzas,
|
|
|
|
``.reply()`` accepts the parameter ``body`` (also as the first positional argument),
|
2014-08-17 19:53:34 +00:00
|
|
|
which is then used as the value of the ``<body />`` element of the message.
|
2011-08-14 03:58:53 +00:00
|
|
|
Setting the appropriate ``to`` JID is also handled by ``.reply()``.
|
|
|
|
|
2014-07-17 12:19:04 +00:00
|
|
|
Another way to have sent the reply message would be to use :meth:`send_message <slixmpp.basexmpp.BaseXMPP.send_message>`,
|
2011-08-14 03:58:53 +00:00
|
|
|
which is a convenience method for generating and sending a message based on the values passed to it. If we were to use
|
|
|
|
this method, the above code would look as so:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
def message(self, msg):
|
|
|
|
if msg['type'] in ('normal', 'chat'):
|
|
|
|
self.send_message(mto=msg['from'],
|
|
|
|
mbody='Thanks for sending:\n%s' % msg['body'])
|
|
|
|
|
|
|
|
Whichever method you choose to use, the results in action will look like this:
|
|
|
|
|
|
|
|
.. code-block:: xml
|
|
|
|
|
|
|
|
<message to="echobot@example.com" from="someuser@example.net" type="chat">
|
|
|
|
<body>Hej!</body>
|
|
|
|
</message>
|
|
|
|
|
|
|
|
<message to="someuser@example.net" type="chat">
|
|
|
|
<body>Thanks for sending:
|
|
|
|
Hej!</body>
|
|
|
|
</message>
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
XMPP does not require stanzas sent by a client to include a ``from`` attribute, and
|
|
|
|
leaves that responsibility to the XMPP server. However, if a sent stanza does
|
|
|
|
include a ``from`` attribute, it must match the full JID of the client or some
|
2014-07-17 12:19:04 +00:00
|
|
|
servers will reject it. Slixmpp thus leaves out the ``from`` attribute when replying
|
2011-08-14 03:58:53 +00:00
|
|
|
using a client connection.
|
|
|
|
|
|
|
|
Command Line Arguments and Logging
|
|
|
|
----------------------------------
|
|
|
|
|
2014-07-17 12:19:04 +00:00
|
|
|
While this isn't part of Slixmpp itself, we do want our echo bot program to be able
|
2011-08-14 03:58:53 +00:00
|
|
|
to accept a JID and password from the command line instead of hard coding them. We will
|
|
|
|
use the ``optparse`` module for this, though there are several alternative methods, including
|
|
|
|
the newer ``argparse`` module.
|
|
|
|
|
|
|
|
We want to accept three parameters: the JID for the echo bot, its password, and a flag for
|
|
|
|
displaying the debugging logs. We also want these to be optional parameters, since passing
|
2014-08-17 19:53:34 +00:00
|
|
|
a password directly through the command line can be a security risk.
|
2011-08-14 03:58:53 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
optp = OptionParser()
|
|
|
|
|
|
|
|
optp.add_option('-d', '--debug', help='set logging to DEBUG',
|
|
|
|
action='store_const', dest='loglevel',
|
|
|
|
const=logging.DEBUG, default=logging.INFO)
|
|
|
|
optp.add_option("-j", "--jid", dest="jid",
|
|
|
|
help="JID to use")
|
|
|
|
optp.add_option("-p", "--password", dest="password",
|
|
|
|
help="password to use")
|
|
|
|
|
|
|
|
opts, args = optp.parse_args()
|
|
|
|
|
|
|
|
if opts.jid is None:
|
|
|
|
opts.jid = raw_input("Username: ")
|
|
|
|
if opts.password is None:
|
|
|
|
opts.password = getpass.getpass("Password: ")
|
|
|
|
|
|
|
|
Since we included a flag for enabling debugging logs, we need to configure the
|
|
|
|
``logging`` module to behave accordingly.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
# .. option parsing from above ..
|
|
|
|
|
|
|
|
logging.basicConfig(level=opts.loglevel,
|
|
|
|
format='%(levelname)-8s %(message)s')
|
|
|
|
|
|
|
|
|
|
|
|
Connecting to the Server and Processing
|
|
|
|
---------------------------------------
|
|
|
|
There are three steps remaining until our echo bot is complete:
|
|
|
|
1. We need to instantiate the bot.
|
|
|
|
2. The bot needs to connect to an XMPP server.
|
|
|
|
3. We have to instruct the bot to start running and processing messages.
|
|
|
|
|
|
|
|
Creating the bot is straightforward, but we can also perform some configuration
|
|
|
|
at this stage. For example, let's say we want our bot to support `service discovery
|
|
|
|
<http://xmpp.org/extensions/xep-0030.html>`_ and `pings <http://xmpp.org/extensions/xep-0199.html>`_:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
# .. option parsing and logging steps from above
|
|
|
|
|
|
|
|
xmpp = EchoBot(opts.jid, opts.password)
|
|
|
|
xmpp.register_plugin('xep_0030') # Service Discovery
|
|
|
|
xmpp.register_plugin('xep_0199') # Ping
|
|
|
|
|
|
|
|
If the ``EchoBot`` class had a hard dependency on a plugin, we could register that plugin in
|
|
|
|
the ``EchoBot.__init__`` method instead.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2014-08-17 19:53:34 +00:00
|
|
|
If you are using the OpenFire server, you will need to include an additional
|
2011-08-14 03:58:53 +00:00
|
|
|
configuration step. OpenFire supports a different version of SSL than what
|
2014-07-17 12:19:04 +00:00
|
|
|
most servers and Slixmpp support.
|
2011-08-14 03:58:53 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
2014-08-17 19:53:34 +00:00
|
|
|
|
2011-08-14 03:58:53 +00:00
|
|
|
import ssl
|
|
|
|
xmpp.ssl_version = ssl.PROTOCOL_SSLv3
|
|
|
|
|
|
|
|
Now we're ready to connect and begin echoing messages. If you have the package
|
2015-02-24 17:58:40 +00:00
|
|
|
``aiodns`` installed, then the :meth:`slixmpp.clientxmpp.ClientXMPP` method
|
2011-08-14 03:58:53 +00:00
|
|
|
will perform a DNS query to find the appropriate server to connect to for the
|
2015-02-24 17:58:40 +00:00
|
|
|
given JID. If you do not have ``aiodns``, then Slixmpp will attempt to
|
2011-08-14 03:58:53 +00:00
|
|
|
connect to the hostname used by the JID, unless an address tuple is supplied
|
2014-08-17 19:53:34 +00:00
|
|
|
to :meth:`slixmpp.clientxmpp.ClientXMPP`.
|
2011-08-14 03:58:53 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
# .. option parsing & echo bot configuration
|
|
|
|
|
|
|
|
if xmpp.connect():
|
|
|
|
xmpp.process(block=True)
|
|
|
|
else:
|
|
|
|
print('Unable to connect')
|
|
|
|
|
2014-07-17 12:19:04 +00:00
|
|
|
To begin responding to messages, you'll see we called :meth:`slixmpp.basexmpp.BaseXMPP.process`
|
2011-08-14 03:58:53 +00:00
|
|
|
which will start the event handling, send queue, and XML reader threads. It will also call
|
2014-09-21 16:51:06 +00:00
|
|
|
the :meth:`slixmpp.plugins.base.BasePlugin.post_init` method on all registered plugins. By
|
2014-08-17 19:53:34 +00:00
|
|
|
passing ``block=True`` to :meth:`slixmpp.basexmpp.BaseXMPP.process` we are running the
|
2014-07-17 12:19:04 +00:00
|
|
|
main processing loop in the main thread of execution. The :meth:`slixmpp.basexmpp.BaseXMPP.process`
|
|
|
|
call will not return until after Slixmpp disconnects. If you need to run the client in the background
|
2011-08-14 03:58:53 +00:00
|
|
|
for another program, use ``block=False`` to spawn the processing loop in its own thread.
|
|
|
|
|
2014-08-17 19:53:34 +00:00
|
|
|
.. note::
|
2011-08-14 03:58:53 +00:00
|
|
|
|
2014-07-17 12:19:04 +00:00
|
|
|
Before 1.0, controlling the blocking behaviour of :meth:`slixmpp.basexmpp.BaseXMPP.process` was
|
2011-08-14 03:58:53 +00:00
|
|
|
done via the ``threaded`` argument. This arrangement was a source of confusion because some users
|
2014-07-17 12:19:04 +00:00
|
|
|
interpreted that as controlling whether or not Slixmpp used threads at all, instead of how
|
2011-08-14 03:58:53 +00:00
|
|
|
the processing loop itself was spawned.
|
|
|
|
|
|
|
|
The statements ``xmpp.process(threaded=False)`` and ``xmpp.process(block=True)`` are equivalent.
|
|
|
|
|
|
|
|
|
|
|
|
.. _echobot_complete:
|
|
|
|
|
|
|
|
The Final Product
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
Here then is what the final result should look like after working through the guide above. The code
|
2019-02-12 10:34:57 +00:00
|
|
|
can also be found in the Slixmpp `examples directory <https://lab.louiz.org/poezio/slixmpp/tree/master/examples>`_.
|
2011-08-14 03:58:53 +00:00
|
|
|
|
|
|
|
.. compound::
|
|
|
|
|
|
|
|
You can run the code using:
|
|
|
|
|
|
|
|
.. code-block:: sh
|
|
|
|
|
|
|
|
python echobot.py -d -j echobot@example.com
|
|
|
|
|
|
|
|
which will prompt for the password and then begin echoing messages. To test, open
|
|
|
|
your regular IM client and start a chat with the echo bot. Messages you send to it should
|
|
|
|
be mirrored back to you. Be careful if you are using the same JID for the echo bot that
|
|
|
|
you also have logged in with another IM client. Messages could be routed to your IM client instead
|
|
|
|
of the bot.
|
|
|
|
|
|
|
|
.. include:: ../../examples/echo_client.py
|
|
|
|
:literal:
|