From 400958e540150b65cbf59467ea61aa4e654f4542 Mon Sep 17 00:00:00 2001 From: yvesf Date: Fri, 4 Dec 2009 23:40:36 +0000 Subject: aiml bot git-svn-id: http://xapek.org/svn/common/omegle@1473 d0e8fea9-7529-0410-93fb-d39fd5b9c1dd --- pyaiml_bot.py | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 pyaiml_bot.py (limited to 'pyaiml_bot.py') diff --git a/pyaiml_bot.py b/pyaiml_bot.py new file mode 100644 index 0000000..f6348ac --- /dev/null +++ b/pyaiml_bot.py @@ -0,0 +1,110 @@ +import logging +from threading import Event +import random + +import time +from omegle import OmegleChat + +import aiml +k=aiml.Kernel() +k.bootstrap(learnFiles="aiml_files/std-startup.xml", commands="load aiml b") + + +class MegahalBot(OmegleChat): + def __init__(self,disconnect_event,name,host="omegle.com",color=0): + self.disconnect_event = disconnect_event + self.name=name + self.color = color + OmegleChat.__init__(self,name=" Megahal",host=host) + self.idlecount = 0 + + + def c(self,str,bold=False,color=0): + if color == 0: + color = self.color + if bold: + return "\033[1m\033[%sm%s\033[0m\033[0m" %(color, str) + else: + return "\033[%sm%s\033[0m" %(self.color, str) + + + def respond(self,message,prepend=""): + r=k.respond(message.encode("utf-8")) + print self.c("%s [MSG] %s: %s" %(time.strftime("%H:%M:%S"), self.name, r),bold=True) + try: + self.send(r) #.decode("utf-8")) + except Exception,e: + print e + self.disconnect_event.set() + + def on_idle(self): + self.idlecount += 1 + if self.idlecount > 8: + self.logger.info("Idle > 8. disconnect()") + self.disconnect() + self.disconnect_event.set() + else: + self.logger.info("Idle count %s/8" % self.idlecount) + if self.is_confirmed: + self.respond("hey?") #restart the conv. + + def on_message(self,message): + self.logger.info("Stranger: %s" % message) + print self.c("%s [MSG] %s: %s" %( + time.strftime("%H:%M:%S"), "Stranger", message),bold=True,color=32) + self.idlecount = 0 + time.sleep(0.05) + self.logger.info("Megahal simulate typing") +# typestr = "%s [EVT] %s: typing" %(time.strftime("%H:%M:%S"),self.name) + #print self.c(typestr,bold=True,color=32) + try: + self.typing() + except Exception,e: + try: + self.disconnect() + except: + self.disconnect_event.set() + return + time.sleep(random.randint(2,5)) + self.respond(message) #prepend="\033[1A\033[2K\033[%sD"%(len(typestr))) #move up 1; delete line; move left %s + + def on_connect(self): + print self.c("%s [EVT] %s Connection confirmed" % (time.strftime("%H:%M:%S"), self.name)) + + + def on_disconnect(self): + print self.c("%s [EVT] %s disconnect" % (time.strftime("%H:%M:%S"), self.name)) + self.disconnect_event.set() + + + +if __name__ == "__main__": + import getopt + import sys + host = "omegle.com" + local=False + + logging.basicConfig(level=logging.DEBUG) + if local: + print "CTRL-C\n>>> ", + while True: + x=sys.stdin.readline().strip() + print "<<< %s\n>>> " % k.respond(x), + else: + print "press ctrl-c to abort" + event = Event() + bot=MegahalBot(event,"A",host=host,color=31) + + bot.start() + + try: + while True: + event.wait(0.5) + if event.isSet(): + print "disconnect_event set" + break + except KeyboardInterrupt: + print "CTRL-C pressed, exiting" + + if bot.is_connected: bot.disconnect() + -- cgit v1.2.1