def post(self): try: secret_equal = Spark.compare_secret( self.request.body, self.request.headers.get('X-Spark-Signature'), Settings.secret_phrase) if secret_equal or Settings.secret_phrase.lower() == "none": webhook = json.loads(self.request.body) if webhook['data']['personId'] != Settings.bot_id: print("MainHandler Webhook Received:") print(webhook) response = yield self.application.settings[ 'spark'].get_with_retries_v2( 'https://api.ciscospark.com/v1/messages/{0}'. format(webhook['data']['id'])) print("response.BODY:{0}".format(response.body)) reply_msg = 'You said, "{0}".'.format( response.body.get('text')) if reply_msg != '': yield self.application.settings[ 'spark'].post_with_retries( 'https://api.ciscospark.com/v1/messages', { 'markdown': reply_msg, 'roomId': webhook['data']['roomId'] }) else: print("CardsHandler Secret does not match") except Exception as e: print("CardsHandler General Error:{0}".format(e)) traceback.print_exc()
def post(self): try: secret_equal = Spark.compare_secret(self.request.body, self.request.headers.get('X-Spark-Signature'), Settings.secret_phrase) if secret_equal or Settings.secret_phrase.lower()=="none": webhook = json.loads(self.request.body) print("CardsHandler Webhook Attachment Action Received:") print(webhook) attachment = yield self.application.settings['spark'].get_with_retries_v2('https://api.ciscospark.com/v1/attachment/actions/{0}'.format(webhook['data']['id'])) print("attachment.BODY:{0}".format(attachment.body)) message_id = attachment.body['messageId'] room_id = attachment.body['roomId'] person_id = attachment.body['personId'] inputs = attachment.body.get('inputs', {}) print("messageId:{0}".format(message_id)) print("roomId:{0}".format(room_id)) print("inputs:{0}".format(inputs)) room = yield self.application.settings['spark'].get_with_retries_v2('https://api.ciscospark.com/v1/rooms/{0}'.format(room_id)) room_type = room.body.get('type') person = "you" if room_type == "group": person = "<@personId:{0}|>".format(person_id) reply_msg = '' person_obj = yield self.application.settings['spark'].get_with_retries_v2('https://api.ciscospark.com/v1/people/{0}'.format(person_id)) display_name = person_obj.body.get('displayName') if inputs.get('submit') in ["ack","ack_res"] and inputs.get('id') not in [None, ""]: message = "Incident ID: <b>{0}</b> has been acknowledged by ".format(inputs.get('id')) comment = {"author":"System", "message":message + display_name} result = self.application.settings['db'].insert(person_id, comment=comment) reply_msg = message + person + ". View it [here]({0}).".format(inputs["url"]) elif inputs.get('submit') == "inc": if inputs.get('comment') in [None, ""]: reply_msg = "Comment cannot be blank." else: comment = {"author":display_name, "message":inputs.get('comment')} result = self.application.settings['db'].insert(person_id, comment=comment) reply_msg = "Comment added. View it at {0}".format(inputs["url"]) if reply_msg != '': yield self.application.settings['spark'].post_with_retries('https://api.ciscospark.com/v1/messages', {'markdown':reply_msg, 'roomId':room_id}) else: print("CardsHandler Secret does not match") except Exception as e: print("CardsHandler General Error:{0}".format(e)) traceback.print_exc()
def main(): try: parse_command_line() static_dir = os.path.join(os.path.dirname(__file__), "static") app = tornado.web.Application( [ (r"/", MainHandler), (r"/auth", AuthHandler), (r"/cards", CardsHandler), (r"/comments", CommentsHandler), (r"/login", LoginHandler), (r"/toggle", ToggleHandler), (r"/css/(.*)", tornado.web.StaticFileHandler, {'path': os.path.join(static_dir, "css")}), (r"/fonts/(.*)", tornado.web.StaticFileHandler, {'path': os.path.join(static_dir, "fonts")}), (r"/images/(.*)", tornado.web.StaticFileHandler, {'path': os.path.join(static_dir, "images")}), (r"/js/(.*)", tornado.web.StaticFileHandler, {'path': os.path.join(static_dir, "js")}), ], cookie_secret="asdfuerhkcxcuqwena;fsdiaxwuejadls", xsrf_cookies=False, debug=options.debug, template_path=static_dir ) app.settings['db'] = MongoController() app.settings['db'].comments.create_index([("timestamp", 1)], expireAfterSeconds=14400)#14400 == 4 hours app.settings['db'].issues.create_index([("timestamp", 1)], expireAfterSeconds=14400)#14400 == 4 hours app.settings['debug'] = options.debug app.settings['settings'] = Settings app.settings['spark'] = Spark(Settings.token) server = tornado.httpserver.HTTPServer(app) print("Serving... on port {0}".format(Settings.port)) server.bind(Settings.port) # port print("Debug: {0}".format(app.settings["debug"])) server.start() tornado.ioloop.IOLoop.instance().start() print('Done') except Exception as e: traceback.print_exc()
def main(): try: parse_command_line() app = tornado.web.Application( [ (r"/cards", CardsHandler), #(r"/test", TestHandler), ], cookie_secret="CHANGE_THIS_TO_SOME_RANDOM_VALUE", xsrf_cookies=False, debug=options.debug, ) app.settings['debug'] = options.debug app.settings['settings'] = Settings app.settings['spark'] = Spark(Settings.token) server = tornado.httpserver.HTTPServer(app) print("Serving... on port {0}".format(Settings.port)) server.bind(Settings.port) # port print("Debug: {0}".format(app.settings["debug"])) server.start() tornado.ioloop.IOLoop.instance().start() print('Done') except Exception as e: traceback.print_exc()
def post(self): try: secret_equal = Spark.compare_secret( self.request.body, self.request.headers.get('X-Spark-Signature'), Settings.secret_phrase) if secret_equal or Settings.secret_phrase.lower() == "none": webhook = json.loads(self.request.body) print("CardsHandler Webhook Attachment Action Received:") print(webhook) attachment = yield self.application.settings[ 'spark'].get_with_retries_v2( 'https://api.ciscospark.com/v1/attachment/actions/{0}'. format(webhook['data']['id'])) print("attachment.BODY:{0}".format(attachment.body)) message_id = attachment.body['messageId'] room_id = attachment.body['roomId'] person_id = attachment.body['personId'] inputs = attachment.body.get('inputs', {}) print("messageId:{0}".format(message_id)) print("roomId:{0}".format(room_id)) print("inputs:{0}".format(inputs)) room = yield self.application.settings[ 'spark'].get_with_retries_v2( 'https://api.ciscospark.com/v1/rooms/{0}'.format( room_id)) room_type = room.body.get('type') person = "you" if room_type == "group": person = "<@personId:{0}|>".format(person_id) reply_msg = '' if inputs.get('submit') == "ack" and inputs.get('id') not in [ None, "" ]: yield self.application.settings['spark'].delete( 'https://api.ciscospark.com/v1/messages/{0}'.format( message_id)) reply_msg = "Event ID: **{0}** has been acknowledged by {1}.".format( inputs.get('id'), person) elif inputs.get('submit') == "inc": if inputs.get('title') in [None, ""]: reply_msg = "You must enter a title for a new incident." else: resp = yield self.declare_incident( inputs.get('title'), inputs.get('hostname')) print("incident create resp:{0}".format(resp.body)) jbody = json.loads(resp.body) inc_id = jbody['data']['attributes']['public_id'] reply_msg = "Incident created by {0}, view it [here](https://app.datadoghq.com/incidents/{1}).".format( person, inc_id) yield self.application.settings['spark'].delete( 'https://api.ciscospark.com/v1/messages/{0}'. format(message_id)) if reply_msg != '': yield self.application.settings['spark'].post_with_retries( 'https://api.ciscospark.com/v1/messages', { 'markdown': reply_msg, 'roomId': room_id }) else: print("CardsHandler Secret does not match") except Exception as e: print("CardsHandler General Error:{0}".format(e)) traceback.print_exc()