def show_achievement(self, num, kind): details = GwApi.achievement(num) details['kind'] = kind s = self.config['format'].format(**details) print("Sending: " + s) self.send_to(Message(body=s), [self.config['dest']]) time.sleep(self.config.get('rate_limit', 0))
def do_POST(self): module = self.server.module config = module.config # filter out non-github events if not 'X-Github-Event' in self.headers: # Ignore and timeout module.log.info( 'Received something that is not a github event, ignoring.') return length = int(self.headers['Content-Length']) data = self.rfile.read(length) # Do hmac verification if enabled # Enabling recommend so people don't send random garbage to the endpoint if 'secret' in config: h = hmac.new(config['secret'].encode(), msg=data, digestmod=hashlib.sha1) expect = 'sha1=' + h.hexdigest() if expect != self.headers['X-Hub-Signature']: module.log.warning('HMAC signature mismatch!') return # TODO read charset from header payload = json.loads(data.decode('utf-8')) event = self.headers['X-Github-Event'] action = payload.get('action', None) module.log.debug('Received {} {} event.'.format(event, action)) # ignore events we didn't ask for events = module.events if event in events and action in events[event]: report = make_report(event, payload) # Ignore events we don't know how to handle if report != None: msg = Message(body=report, author='halibot') module.log.debug('Reporting event to ' + config['dest']) module.send_to(msg, [config['dest']]) else: module.log.warning('Could not form report for "{} {}"'.format( event, action)) self.send_response(204) self.end_headers()
def command(self, args, msg=None): # TODO When containers/routes become more integrated, just look at the # container the message was sent to, which shoudl work for the # default container as well. target = self.config.get('target', self._hal.objects.modules.keys()) target = [t for t in target if t.split('/')[0] != self.name ] # Can't sync send to this module if args == '': self.reply(msg, body=self.general_help(target)) else: hmsg = Message(body=args.split(' '), type='help', origin=self.name) replies = self.sync_send_to(hmsg, target) if len(replies) > 0: # TODO check for discrepancies among replies self.reply(msg, body=list(replies.values())[0][0].body)
def do_POST(self): module = self.server.module config = module.config # filter out non-gitlab events if not 'X-Gitlab-Event' in self.headers: # Ignore and timeout module.log.info( 'Received something that is not a gitlab event, ignoring.') return length = int(self.headers['Content-Length']) data = self.rfile.read(length) if 'secret' in config: if config['secret'] != self.headers['X-Hub-Signature']: module.log.warning('Wrong secret key!') return # TODO read charset from header payload = json.loads(data.decode('utf-8')) event = payload['object_kind'] action = payload['object_attributes']['state'] module.log.debug('Received {} {} event.'.format(event, action)) # ignore events we didn't ask for events = module.events if event in events and action in events[event]: report = make_report(event, action, payload) # Ignore events we don't know how to handle if report != None: msg = Message(body=report, author='halibot') module.log.debug('Reporting event to ' + config['dest']) module.send_to(msg, [config['dest']]) else: module.log.warning('Could not form report for "{} {}"'.format( event, action)) self.send_response(204) self.end_headers()
def general_help(self, target): hmsg = Message(body=[], type='help', origin=self.name) replies = self.sync_send_to(hmsg, target) text = '''The help command gives useful help messages. Syntax: !help <topic> [<subtopic> ...] Available topics: ''' # Collect the available topics topics = [] for r in replies.values(): topics += [t for t in r[0].body if not t in topics] # Append the available topics to the list c = 2 line = ' ' max_column = 80 first = True for t in topics: if c + len(t) + 2 > max_column: text += line + '\n' c = 2 line = ' ' first = True if first: first = False else: line += ', ' c += 2 line += t c += len(t) # Append final line if line.strip() != '': text += line + '\n' return text
async def on_message(message): org = agent.name + "/" + str(message.channel.id) msg = Message(body=message.content, author=str(message.author), origin=org) agent.dispatch(msg)
def send_updates(self, new): msg = Message(context=Context(agent="irc",whom=self.target)) for d in new: msg.body = self.outform(d) self.send_to(msg, ["irc"]) # TODO: Not need this
async def on_private_message(self, by, text): org = self.agent.name + '/' + by msg = Message(body=text, author=by, identity=self.identity(by), origin=org) self.agent.dispatch(msg)
async def on_channel_message(self, target, by, text): org = self.agent.name + '/' + target msg = Message(body=text, author=by, identity=self.identity(by), origin=org) # Send the Halibot-friendly message to the Halibot base for module processing self.agent.dispatch(msg)
def loop_(self): self.task = self.eventloop.call_later(3600,self.loop_) if self.cxt: m = Message(context=self.cxt) self.display(m)
def input_loop(self): while self.running: inp = input('>> ') msg = Message(body=inp, origin=self.name) self.dispatch(msg) time.sleep(.1) # Kludge so responses appear before the prompt