Friday, May 18, 2007

XChat script for Lauren chatterbot



Download

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>([^>]+?)

[\n][\n]
", re.DOTALL|re.MULTILINE).search(message)
if cmpobj:
self.laurenreply = cmpobj.group(1)

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




print "loaded %s" % __module_name__


class xchatLauren(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(':')

if contact_name != "root":
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):
obxcLauren = xchatLauren(word, word_eol, userdata)
obxcLauren.start()

xchat.hook_server("PRIVMSG", priv_cb)

1 comment:

Vishah said...

atleast give a link back to the original content.