def run(event, context):
    load_dependency('pydot')
    channel = event.get('channel')
    data    = event.get('dot')

    #slack_message("in dot to svg: {0}".format(event), [], channel)
    log_to_elk("in dot to svg: {0}".format(event))

    import dot_parser

    try:
        (fd, tmp_file) = tempfile.mkstemp('dot)')
        dot_static     = '/tmp/lambdas-dependencies/pydot/dot_static'
        Process.run("chmod", ['+x', dot_static])
        data           = data.replace('&lt;', '<').replace('&gt;','>')  # this solved a really nasty bug caused by the fact that Slack will html encode the < and >

        # graph          = pydot.graph_from_dot_data(data).pop()
        # <from pydot>  use code below (instead of above) to get a better error message from dot parser
        graphparser = dot_parser.graph_definition()
        graphparser.parseWithTabs()
        tokens      = graphparser.parseString(data)
        graph       = list(tokens).pop()
        # </from pydot>
        graph.write_svg(tmp_file, prog=dot_static)
        svg_raw        = Files.contents(tmp_file)
        return base64.b64encode(svg_raw.encode()).decode()
    except Exception as error:
        slack_message("[dot_to_svg] Error: {0} ".format(error), [], channel)
        return None
Example #2
0
 def invoke(self, team_id=None, channel=None, params=None):
     if params is None: params = []
     attachments = []
     if len(params) == 0:
         (text, attachments) = self.help()
     else:
         original_params = list(params)
         command = params.pop(0)  # extract first element from the array
         if command in self.available_methods():
             method = getattr(self.target, command)
             try:
                 start = time()
                 result = method(team_id, channel, params)
                 if type(result).__name__ == 'tuple':
                     text, attachments = result
                 else:
                     text, attachments = result, []
                 if text is None and self._show_duration:
                     duration = time() - start
                     text = 'completed execution in `{0:.0f}` secs'.format(
                         duration)
             except Exception as error:
                 text = ':red_circle: Error processing params `{0}`: _{1}_'.format(
                     original_params, pprint.pformat(error))
                 log_to_elk(
                     "Error in Lambda_Graph.handle_lambda_event :{0}".
                     format(text),
                     level='error')
         else:
             (text, attachments) = self.help(
                 ':red_circle: command not found `{0}`\n\n'.format(command))
     if channel and text is not None:  # if there is a text value, then send it as a slack message
         slack_message(text, attachments, channel, team_id)
     return text, attachments
Example #3
0
 def process_slash_command(self, data):
     try:
         log_to_elk('process_slash_command', data)
         command = data.get('command')
         attachments = []
         #if command =='/cst':
         #    (text, attachments) = Slash_Cst().process_command(data)
         if command == '/jira':
             result = Lambda('gs.jira_dialog').invoke({
                 'type': 'jira_slash_command',
                 'data': data
             })
             if (result                    is not None and \
                 type(result)              is not str  and \
                 result.get('text')        is not None and \
                 result.get('attachments') is not None     ):
                 text = result.get('text')
                 attachments = result.get('attachments')
             else:
                 text = result
         else:
             text = ':point_right: unrecongnised Slash Command: {0}'.format(
                 command)
         return {'text': text, 'attachments': attachments}
     except Exception as error:
         error_message = 'Error in API_Slack_Integration.process slash command: {0}'.format(
             error)
         log_to_elk(error_message, level='error')
         return {'text': error_message, 'attachments': []}
Example #4
0
    def process_dialog_suggestion(self, data, field_name):

        response = {"options": []}
        callback_id = data.get("callback_id")
        if callback_id == "issue-suggestion" or callback_id == 'issue-search-dialog':

            log_to_elk("data received: {0}".format(data))
            max = 50
            payload = {
                "action": "search",
                "data": {
                    "text": data.get('value').strip(),
                    "field": "Summary",
                    "size": max
                }
            }
            results = Lambda('gs.elastic_search').invoke(payload)
            log_to_elk('Got {0} results from elk'.format(len(results)))

            try:
                for item in results:
                    key = item.get("Key")
                    summary = item.get("Summary")
                    if field_name == "label":
                        if len(summary) > 42:
                            summary = summary[0:42] + "..."
                        label_text = "{0:12}   {1}".format(key, summary)
                    else:
                        label_text = summary

                    response['options'].append({
                        field_name: label_text,
                        "value": key
                    })
                if len(response['options']) == max:
                    response['options'].append({
                        field_name:
                        ".... note: max search limit reached: {0}".format(max),
                        "value":
                        "EEEE-1111"
                    })
            except Exception as error:
                response['options'].append({
                    field_name:
                    "Error : {0}".format(error),
                    "value":
                    "AAAAA-1111"
                })
        else:
            response['options'].append({
                field_name:
                "... unsupported  suggestion callback_id: {0}".format(
                    callback_id),
                "value":
                "UUUU-1111"
            })
        #response['options'] = response['options']
        return response
Example #5
0
    def handle_request(self, event):
        try:
            body = event.get('body')
            raw_json = urllib.parse.unquote(body).split('payload=').pop(
            )  # convert the data back to json (need to pick the 2nd parameter
            data = json.loads(raw_json)  # load into Python object
            log_to_elk('osbot.lambdas.slack_callback_handle_request',
                       data=data,
                       index='slack_interaction',
                       category='API_Slack_Interaction')
            return Lambda('osbot_jira.lambdas.slack_actions').invoke(data)

            # if body:
            #     if   'type%22%3A%22interactive_message' in body: return self.process_action            (self.decode_body_with_payload(body)) # this is an interactive_message response
            #     elif 'type%22%3A%22dialog_submission'   in body: return self.process_dialog_submission (self.decode_body_with_payload(body))
            #     elif 'type%22%3A%22message_action'      in body: return self.process_interactive_action(self.decode_body_with_payload(body))
            #     elif 'type%22%3A%22dialog_suggestion'   in body: return self.process_dialog_suggestion (self.decode_body_with_payload(body), "label")
            #     else                                           : return self.process_slash_command     (self.decode_body_form_encoded(body))
        except Exception as error:
            message = 'Sorry could not process request, the error was: {0}'.format(
                error)
            log_to_elk('Error in osbot.lambdas.slack_callback_handle_request',
                       data=message,
                       level='error',
                       index='slack_integrations',
                       category='API_Slack_Interaction')
            log_to_elk(message)
            return message
        return 'Sorry could not process the request'