IMPyBot: An XMPP Bot and Plug-in Framework in Python

Kefei Lu, klu1024 AT gmail.com
01/26/2010
Project Homepage (Download link) User Guide Project Wiki Source Codes Discussion Forum

Table Of Contents

1. Introduction

IMPyBot (impybot) is a bot (IM automatic response) framework for XMPP protocol. It provides a plug-in system, so that the implementation of a utility is isolated from implementation of the bot. This provides great convenience to manage the utilities without touching the bot itself. The plug-in system is sophisticated that it enables developer to write really large and powerful plug-ins. Meanwhile, it also provides a simple interface such that users with little Python experience can write useful utilities as well.

The figure below illustrates the structure of IMPyBot framework. As shown, the plug-ins are isolated from the bot itself. Plugin developers who are only interested in writing utilities like weather querier or calculators does not need to know the details of the bot at all. What is more important, this provides an easier way to organize and manage all the plugins.

     +------------------------------------------------------------+
     |                                                            |
     |                     The Plug-ins                           |
     |       +--------------------------------------------+       |
     |       |  +---------+ +------------+ +------------+ |       |
     |       |  | weather | | dictionary | | calculator | |       |
     |       |  +---------+ +------------+ +------------+ |       |
     |       +--------------------------------------------+       |
     |                            |                               |
     |                            | plug in                       |
     |                            v                               |
     |       +--------------------------------------------+       |
     |       |         The XMPP Bot Framework             |       |
     |       +--------------------------------------------+       |
     |                           | ^                              |
     |                           | |                              |
     |                           v |                              |
     |       +--------------------------------------------+       |
     |       |                 Internet                   |       |
     |       +--------------------------------------------+       |
     |                                                            |
     |                                                            |
     +------------------------------------------------------------+
            Figure 1. Illustration of the IMPyBot Framework

2. Installation

  1. Download the package from https://sourceforge.net/projects/impybot
  2. Unzip the package.
  3. Install to the system.

2.1. Use Without Installation

You can also use IMPyBot without installing it to the system location.

  1. Download the package from https://sourceforge.net/projects/impybot
  2. Unzip the package.
  3. Open a terminal inside the unzipped directory, do:
      run_bot.py -j "username" -p "password"
    

3. A Guide for The Impatients

To write your own utilities (we call it a plugin), simply follow these TWO steps:

3.1. Write a plugin module

Create a text file called my_first_plugin.py, with codes look like the following:

 1 import impybot  # make sure it's properly installed
 2
 3 class MyFirstPlugin(impybot.SimplePlugin):
 4
 5     # If a message has lines begin with ANY of these words,
 6     # handle_match() will be called.
 7     command = ('echo', 'display')
 8     
 9     def handle_match( self, matched, sender_jid ):
10         msg = 'The strings after the matched commands:'
11
12         # If a line in a message matches ANY words in ``command'',
13         # the string after the matched word of that line goes into
14         # the ``matched'' tuple.
15         for m in matched:
16             msg = msg + '\n' + m
17
18         # The returned string will be sent back as a reply.
19         return msg
20
21 # DON'T FORGET TO REGISTER THE CLASS!!!
22 impybot.register(MyFirstPlugin)

3.2. Invoke the bot

Invoke the command line tool to run a bot and tell it where your plugin is:

  python -m run_bot -j "jid@server.com" -p "password" -m my_first_plugin.py

3.3. That's it!

Congratulations on your first Instant Messaging Application!