def process_line(self): data = self.fifo_pop() reply = None msg = data[1] if msg.split()[0].endswith(':'): msg = ' '.join(msg.split()[1:]) if not data[0]: mh_python.learn(msg) else: reply = mh_python.doreply(msg) if reply: self.send(data[0].split('!')[0].strip() + ': ' + reply) mh_python.cleanup() return
def cleanup(self, irc, msg, args): """takes no argument Saves MegaHAL brain to disk.""" megahal.cleanup() irc.replySuccess()
def unload(bot): mh_python.cleanup()
#!/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 cleanup(self): """clean megahal shutdown""" mh_python.cleanup()
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 learn_message(self, message): """Passes the message to megahal brain to learn.""" mh_python.learn(message) mh_python.cleanup()