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!

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_…

Replicated Mnesia

I'm still working on my expiring records module in Erlang (see here and here for my previous posts on this). Previously, I had started using Mnesia, but only a RAM based table. I've now switched it over to a replicated disc based table. That was easy enough, but it took a while to figure out how to do, nonetheless. I had assumed that simply adding ... {disc_copies, [node()]} ... to the arguments to mnesia:create_table would be enough. This resulted in an error: {app_test,init_per_testcase, {{badmatch, {aborted, {bad_type,expiring_records,disc_copies,nonode@nohost}}}, ... After some head-scratching and lots of Googling I realized that I was missing a call to mnesia:create_schema to allow it to create disc based tables. My tests for this module are done with common_test so I set up a per suite initialization function like this: init_per_suite(Config) ->mnesia:create_schema([node()]), mnesia:start(…

Waiting for an answer

I want to describe my first iteration of exsim, the core server for the large scale simulation I described in my last blog post. A Listener module opens a socket for listening to incoming connections. Once a connection is made, a process is spawned for handling the login and the listener continues listening for new connections. Once logged in, a Player is created, and a Solarsystem is started (if it hasn't already). The solar system also starts a PhysicsProxy, and the player starts a Ship. These are all GenServer processes. The source for this is up on GitHub: https://github.com/snorristurluson/exsim Player The player takes ownership of the TCP connection and handles communication with the game client (or bot). Incoming messages are parsed in handle_info/2 and handled by the player or routed to the ship, as appropriate. The player creates the ship in its init/1 function. The state for the player holds the ship and the name of the player. Ship The ship holds the state of the ship - …