def __init__(self, bot): """starts the megahal program""" try: mh_python.setnobanner() mh_python.setdir(datadir) except: pass mh_python.initbrain() self.bot = bot
def __init__(self, name): mongo = MongoClient() db = mongo.lisa.logs msgs = db.find({'event': 'privmsg'}) mh_python.initbrain() for msg in msgs: mh_python.learn(msg['message'].encode('utf-8'))
def __init__(self, irc): # Call Supybot's scripts self.__parent = super(MegaHAL, self) self.__parent.__init__(irc) # Save state saved = (sys.stdout, os.getcwd()) # Create proxy for MegaHAL os.chdir(conf.supybot.directories.data()) sys.stdout = StringIO() # Initialize MegaHAL megahal.initbrain() # Restore state sys.stdout, cwd = saved os.chdir(cwd) random.seed()
def load(bot): mh_python.initbrain()
#!/usr/bin/python # MegaHAL quick learning script. # By Laurent Fousse <*****@*****.**> GPL 2003 # # Usage: quick-learn < text-file # reads text-file linewise and feeds it to megahal import mh_python import sys mh_python.initbrain() while 1: ligne = sys.stdin.readline() if not ligne: break mh_python.learn(ligne) mh_python.cleanup()
def on_load(connection): mh_python.initbrain()
def main(): cfgparser = ConfigParser() success = cfgparser.read('config.cfg') if not success: cfgparser = get_default_config() write_config(cfgparser) response_rate = cfgparser.getfloat('General', 'response rate') argparser = argparse.ArgumentParser( description="Slack chatbot using MegaHAL") argparser.add_argument("-t", "--token", type=str, help="Slack token", required=True) argparser.add_argument("--debug", help="Output raw events to help debug", action="store_true") args = vars(argparser.parse_args()) token = args['token'] debug = args['debug'] sc = SlackClient(token) mh.initbrain() try: if sc.rtm_connect(): name = get_name(sc) print("Detected name: %s" % name) time_of_last_event = datetime.datetime.now() while True: for event in sc.rtm_read(): time_of_last_event = datetime.datetime.now() if debug: print(event) if 'type' in event and event['type'] == 'message' \ and 'text' in event: message = event['text'].encode('ascii', 'ignore') # lowercase message so we can search it # case-insensitively message = message.lower() print("Handling message: %s" % message) match = re.search( "%s, set response rate to [0-9]{2}(%%|)" % name, message) if match: words = match.group().split() num = words[-1] if num[-1] == '%': rate = float(num[:-1]) / 100 else: rate = float(num) response_rate = rate reply(sc, event, "Response rate set to %f" % rate) time.sleep(1) # sleep to avoid rate limit else: match = re.search( "%s, what is your response rate?" % name, message) if match: reply( sc, event, "My response rate is set at %f." % response_rate) time.sleep(1) # sleep to avoid rate limit elif name in message or random.random( ) < response_rate: response = mh.doreply(message) reply(sc, event, response) time.sleep(1) # sleep to avoid rate limit else: mh.learn(message) # avoid being disconnected by activity by pinging inactivity = datetime.datetime.now() - time_of_last_event if inactivity > datetime.timedelta(seconds=5): sc.server.ping() time.sleep(2) else: print("Connection Failed, invalid token?") finally: mh.cleanup() cfgparser.set('General', 'response rate', str(response_rate)) print('Saving config...') write_config(cfgparser)
def __init__(self, server): """Constructor.""" Module.__init__(self, server) mh_python.initbrain()
def __init__(self, parent, name = 'megahal', trigger = None): Module.__init__(self, parent, name, trigger) mh_python.initbrain() return