Skip to main content

EchoBot

In a previous blog I started discussing Xmpp and showed how to set up an Xmpp server and connecting to it via Python. In this blog I will dig deeper and show how to implement a simple echo bot.
The code for this lives on Github: https://github.com/snorristurluson/xmpp-chatbot

Connecting

First, let's wrap the network layer. I've picked the Python 3 asyncio for this task.
Let's start by looking at firstconnection.py. I've created a class called FirstConnection that inherits from asyncio.Protocol.
class FirstConnection(asyncio.Protocol):
    def __init__(self, host):
        self.host = host
        self.transport = None

    def connect(self):
        loop = asyncio.get_event_loop()
        handler = loop.create_connection(lambda: self, self.host, 5222)
        loop.create_task(handler)

    def connection_made(self, transport):
        logger.debug("Connection made")
        self.transport = transport

        cmd = "<?xml version='1.0'?><stream:stream to='localhost' " \
              "version='1.0' xmlns='jabber:client' " \
              "xmlns:stream='http://etherx.jabber.org/streams'>"
        self.write(cmd)

        cmd = "<junk/>"
        self.write(cmd)

    def connection_lost(self, exc):
        logger.debug("Connection lost")

    def write(self, data):
        logger.debug("Send: %s", data)
        self.transport.write(data.encode())

    def data_received(self, data):
        logger.debug("Received %d bytes\n%s", len(data), data.decode())
Let's instantiate this class and connect:
    loop = asyncio.get_event_loop()

    bot = FirstConnection("localhost")
    bot.connect()
    loop.run_forever()
If the connect call fails, make sure you have Prosody running. If it is, you should see output similar to this:
DEBUG:__main__:FirstConnection is starting
DEBUG:asyncio:Using selector: KqueueSelector
DEBUG:__main__:Connection made
DEBUG:__main__:Send: <?xml version='1.0'?><stream:stream to='localhost' version='1.0' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>
DEBUG:__main__:Send: <junk/>
DEBUG:__main__:Received 605 bytes
<?xml version='1.0'?><stream:stream xmlns:stream='http://etherx.jabber.org/streams' version='1.0' from='localhost' id='743f47de-6f34-4458-afcc-558f872b949a' xml:lang='en' xmlns='jabber:client'><stream:features><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>PLAIN</mechanism><mechanism>SCRAM-SHA-1</mechanism><mechanism>DIGEST-MD5</mechanism></mechanisms><auth xmlns='http://jabber.org/features/iq-auth'/></stream:features><stream:error><unsupported-stanza-type xmlns='urn:ietf:params:xml:ns:xmpp-streams'/></stream:error></stream:stream>
DEBUG:__main__:Connection lost
This is a small step, but an important one. We're talking to the server and have initiated the handshake. The server sent us back its part of the handshake - then we sent it junk and it slammed the door on us. The important bit is that we're doing this in a more managed fashion than before and this allows us to build something useful on top.

Handling XMPP

Before we can actually log in we need to think about how to process the incoming XMPP commands. XMPP is XML based so it makes sense to use an XML parser, rather than working with the raw text.
Python offers several different options for parsing XML, but keeping in mind that we are not dealing with a full XML document, the xml.sax module seems appropriate.
The connection to the XMPP server is stateful - the server expects an initial handshake at the beginning with the right commands being issued in the right order. To manage this, I use a class named XmppHandler. It holds an instance of an xml.sax parser, on which I set the content handler to an XmppContentHandler.
The XmppContentHandler responds to the incoming parser events and builds a queue of XmppElement objects. The XmppHandler pulls from this queue, allowing it to work on a somewhat higher level, with whole XMPP stanzas.
Data received from the server is sent to the handle_raw_response method of the XmppHandler, which passes it on to the feed method of the parser. Once the parser has been fed, I pull the elements out of the queue, processing them one by one until the queue is empty.

Logging in

The processing of elements depends on the state of the handler. The state machine is not implemented in any formal way - it's simply a state attribute and a series of if statements - crude but effective.
The ConnectBot object in connectbot.py takes us a step further. If you run it, you should see it connecting. Note that you need to add an echobot user on the server before it lets you log in. The password should be echobot as well.
prosodyctl adduser echobot@localhost
Once you have added the user, you should see the bot successfully logging in. The log output from connectbot.pyshows you all the XMPP commands that flow between the bot an the server, and the Prosody logs show you the progress as well.

Echo, echo

Finally we can start doing something useful (by some definition of useful). The EchoBot in echobot.py adds the handle_xmpp_message method. I monkey-patch the handler so that it calls this method whenever a message stanza is received. This method simply pulls the body text from the XMPP element and sends that back to the sender as a message.
Try this out with some XMPP chat client - I've been using Swift. Start a chat with echobot@localhost and you should see everything you say echoed back.

What next?

The hard part is done now - building on this framework we can start focusing on the bots themselves. More on that later!

Comments

Popular posts from this blog

Mnesia queries

I've added search and trim to my expiring records module in Erlang. This started out as an in-memory key/value store, that I then migrated over to using Mnesia and eventually to a replicated Mnesia table. The fetch/1 function is already doing a simple query, with match_object. Result=mnesia:match_object(expiring_records, #record{key=Key, value='_', expires_at='_'}, read) The three parameters there are the name of the table - expiring_records, the matching pattern and the lock type (read lock). The fetch/1 function looks up the key as it was added to the table with store/3. If the key is a tuple, we can also do a partial match: Result=mnesia:match_object(expiring_records, #record{key= {'_', "bongo"}, value='_', expires_at='_'}, read) I've added a search/1 function the module that takes in a matching pattern and returns a list of items where the key matches the pattern. Here's the test for the search/1 function: search_partial_…

Working with Xmpp in Python

Xmpp is an open standard for messaging and presence, used for instant messaging systems. It is also used for chat systems in several games, most notably League of Legends made by Riot Games.

Xmpp is an xml based protocol. Normally you work with xml documents - with Xmpp you work with a stream of xml elements, or stanzas - see https://tools.ietf.org/html/rfc3920 for the full definitions of these concepts. This has some implications on how best to work with the xml.

To experiment with Xmpp, let's start by installing a chat server based on Xmpp and start interacting with it. For my purposes I've chosen Prosody - it's nice and simple to install, especially on macOS with Homebrew:

brew tap prosody/prosody
brew install prosody

Start the server with prosodyctl - you may need to edit the configuration file (/usr/local/etc/prosody/prosody.cfg.lua on the Mac), adding entries for prosody_user and pidfile. Once the server is up and running we can start poking at it to get a feel for h…

Expiring records in Erlang

I'm continuing my experiments with Erlang - this time trying out gen_server with a simple key/value store with a twist - the values have an expiration date. As a first iteration I'm simply using a dictionary to store the values, and only expiring records when they are looked up. My plan is to extend this later on so that this can be a global key/value store across multiple Erlang nodes but for now I'm focusing on two things - get something going using gen_server, and try out the common_test testing framework. The code is here: https://github.com/snorristurluson/erl-expiring-records Let's first take a look at a couple of the test functions, to show the usage of this: get_non_expired_record(Config) ->Pid=?config(pid, Config), Record= {"bingo", "bongo", erlang:system_time(second) +3600}, ok=gen_server:call(Pid, {add, Record}), {ok, "bongo"} =gen_server:call(Pid, {fetch, "bingo"}). get_expired_record(Config) ->Pid=…