From Hive13 Wiki
Revision as of 16:44, 20 November 2009 by (talk)
Jump to navigation Jump to search

This is documenting the bot that I'm working on. The code is ancient; contributions are welcome.


This code uses an IRC bot library called available here.

How it works

Basic Bot

BasicBot - A Basic IRC Bot
License: GPL 2; share and enjoy!
Sean B. Palmer,
   Suw Charman,
Augmented by:
   Dave Menninger

import ircbot, pickle, sys

def basicbot(host, port, channels, nick):
   p = ircbot.Bot(nick=nick, channels=channels)
   address_pattern = "(?:%s[:, ]+)" % p.nick

   def f_hi(m, origin, (cmd, channel), text, p=p):
      p.msg(origin.sender, 'hi %s' % origin.nick)
   p.rule(f_hi, 'hi', r"(?i)^Hi %s(\!)?$" % p.nick)

   def f_chop_by_length( longline, length):
      results = []
      while len(longline) > length:
         results.append( longline[:length] )
         longline = longline[length:]
      return results
   def f_basicBotHelp(m, origin, (cmd, channel), text, p=p):
      p.msg(origin.sender, "Commands I understand:")
      p.msg(origin.sender, "help, ")
   p.rule(f_basicBotHelp, 'commands', "^%s(help|commands)$" % address_pattern)
   p.rule(f_basicBotHelp, 'commands', "^(help|commands) %s$" % p.nick), port)

def main(args):

   if args:
      nickname, ircserver = args[:3]
      rooms = args[3:]
      nickname = 'Hive13Bot'
      ircserver = ''
      rooms = ['#hive13']
   basicbot(ircserver, 6667, rooms, nick=nickname )

if __name__=='__main__':

Adding new functionality

To make new functionality you need two basic things: first, a rule that tells the bot when to react to something that someone says in the IRC channel, and second a function to call that some some action whenever that rule is activated. Look at the "hi" function above for an example.

The rule function looks like this:

p.rule(f_hi, 'hi', r"(?i)^Hi %s(\!)?$" % p.nick)

The rule has three arguments in this case. First, the name of the function to call: "f_hi". Second, a name for the command. Last, a regular expression that describes when the rule should activate. Writing a good regular expression can be tricky, see here for more on how they work: Whenever someone says something in the IRC channel that matches the regular expression, it will call the function specified.

The function that does the action looks like this:

def f_hi(m, origin, (cmd, channel), text, p=p):
   p.msg(origin.sender, 'hi %s' % origin.nick)

Using the p.msg function is how you say things back into the room. The first argument tells where to say it. The second argument is just a string of what to say. Using the variable origin.nick will insert the name of the person that said the message that fired the rule.

More complex examples to come...

Example: Search MediaWiki

   def f_hive13BotWiki(m, origin, (cmd, channel), text, p=p):
      search_term =
      search_url = '' % search_term
      results = simplejson.load(urllib.urlopen(search_url))
      titles = results['query']['search']
      num_titles = len(titles)
      p.msg(origin.sender, "%s results..." % num_titles )
      if num_titles > 0:
         if num_titles < 5:
            for t in titles:
               article_title = "%s" % t['title']
               article_title = article_title.replace(' ','_')
               title_url = "" % article_title
               p.msg(origin.sender, "%s" % title_url )
            p.msg(origin.sender, "too many results. click:" % search_term )
         p.msg(origin.sender, "no results..." )

   p.rule(f_hive13BotWiki, 'wiki', "^%s(wiki) ([^\s]+)$" % address_pattern)
   p.rule(f_hive13BotWiki, 'wiki', "^(wiki) ([^\s]+) %s$" % p.nick)