Wednesday, March 28, 2007

XChat script for Lauren chatterbot





Works on XChat, a popular IRC client for Unix-like systems also available for Microsoft Windows.
If you want to use in instant messaging, there is BitlBee, and IRC Instant messaging gateway.
If you don't want to run your own BitlBee server, here are some of the free public servers.
im.bitlbee.org:6667
testing.bitlbee.org:6667
im.starkast.net:6667


import xchat
import os.path
import urllib2
import urllib
import cookielib
import os
import os.path
import re
from threading import Thread

__module_name__ = "LaurenBot"
__module_version__ = "0.1.1"
__module_description__ = "Lauren says"

# irc bot.


class Lauren(Thread):

      def __init__(self, strtalk, contactname , savefile=None):
              Thread.__init__(self)
              if not os.path.isdir('/tmp/LBot'):
                      os.mkdir('/tmp/LBot')
              self.cookiefile = '/tmp/LBot/'+contactname+'.lwp'
              self.cookiejar = cookielib.LWPCookieJar()
              self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookiejar))
              urllib2.install_opener(self.opener)
              self.url = 'http://lauren.vhost.pandorabots.com/pandora/talk?botid=f6d4afd83e34564d&skin=input&speak=true'
              self.strtalk = strtalk
              self.savefile = savefile
              self.laurenreply =''


      def run(self):
              data = None
              iscookiefile = os.path.isfile(self.cookiefile)

              if iscookiefile:
                      self.cookiejar.load(self.cookiefile)
                      self.strtalk = str(self.strtalk).strip('[]').replace(',','').replace('\'','')
                      data = urllib.urlencode({'message':self.strtalk, 'botcust2':'ea5b96438fa868b7'})

              request = urllib2.Request(self.url, data)

              try:
                      urlobj = urllib2.urlopen(request)
                      self.cookiejar.save(self.cookiefile)
                      if not iscookiefile:
                              self.run()


                      message = urlobj.read()
                      if self.savefile:
                              fileobj = open('/home/vishah/Lauren.html', 'w+')
                              fileobj.write(message)
                              fileobj.close()

                      cmpobj = re.compile(r"LaurenBot:<\/b><\/i>([^>]+?)<br> <br>[\n][\n]<form", re.DOTALL|re.MULTILINE)
                      self.laurenreply = cmpobj.search(message).group(1)

              except IOError, e:
                      if hasattr(e, 'reason'):
                              xchat.prnt("Request not fullfilled by the server. Reason:"+e.reason)
                      elif hasattr(e, 'code'):
                              xchat.prnt("failed to reach server. ErrorCode:"+e.code)



print "loaded %s" % __module_name__


class botte(Thread):
      def __init__(self, word, word_eol, userdata):
              Thread.__init__(self)
              self.word = word
              self.word_eol = word_eol
              self.userdata = userdata

      def run(self):
              he_says = self.word_eol[3][1:]
              contact_name = str(self.word_eol[0]).split('!')[0].strip(':')
              current = Lauren(he_says, contact_name)
              current.start()
              current.join()
              whatlaurensays = current.laurenreply


              if whatlaurensays:
                      xchat.command("MSG %s %s" % (contact_name, str(whatlaurensays)))
              return xchat.EAT_NONE

def priv_cb(word, word_eol, userdata):
      botte = duck(word, word_eol, userdata)
      botte.start()

xchat.hook_server("PRIVMSG", priv_cb)

No comments: