Need To Parse Email Alerts For Their Data

I have emails coming from Absolute DDS and want to parse the fields out of them for automation. Any ideas?


Identifier: 6abca32a Device Name: [devicenamehere] Username: [test\testy] Department: []_______________________________________________________________

Can you get those from the headers array?

If not, is that the absolute string you’re trying to parse?

This looks like parsing data from the body of the e-mail. I’d do it in two passes:

  1. parse the body of the e-mail with the pattern-match step capturing the entire entry like so:
    {{items:/Identifier:\s+\S+\s+Device Name:\s+\S+\s+Username:\s+\S+\s+Department:\s+\S+\s+}}
    and make sure you have selected “return multiple matches”. This should cause the pattern-match step to produce an array of items. Loop over this array.
    Inside the loop, parse out the individual fields with another pattern-match step:
    Identifier:\s+{{identifier:/\S+/}}\s+Device Name:\s+[{{device_name:/\S+/}}]\s+Username:\s+[{{username:/\S+/}}]\s+Department:\s+[{{department:/\S+/}}]
    and do not select return multiple matches. This should give you 4 strings output from the pattern match step: identifier, device_name, username, and department
    Now you have the 4 fields parsed out into variables you can proceed as normal.
1 Like

Note: this regex was developed against the sample in the original message which MAY NOT BE ACCURATE. I’d recommend having ICON trigger on one of these e-mails, copying the body output from the trigger into regex101.com, and develop/test the regex there per Developing and Debugging Regex for the Pattern Match Step

I’d use a python step for this, I’m not a fan of regex.

input = "Identifier: 6abca32a Device Name: [devicenamehere] Username: [test\testy] Department: []_______________________________________________________________"


def run(params={}):
    input = params.get("input")
    split_string = input.split(":")
    output_dict = {}
    for s in split_string:
        split_element = s.strip().split(" ")
        if len(split_element) > 1:
            output_dict[split_element[0]]=" ".join(split_element[1:])

    return output_dict

print(run({"input":input}))

You can get more elaborate and strip the “[” “]” out off the keys as well.

1 Like

The regex does this as well - it all depends on where you put the [ in the pattern!

If you do it with a python step, don’t forget to match up the step output schema to match your output object so you can use it in the variable picker later.

1 Like

maybe I am making a simple mistake here… it is friday afternoon.

function:def run(params={}):
input = params.get(“input”)
split_string = input.split(":")
output_dict = {}
for s in split_string:
split_element = s.strip().split(" “)
if len(split_element) > 1:
output_dict[split_element[0]]=” ".join(split_element[1:])

return output_dict

print(run({“input”:input}))
input:Identifier: 6abca32a Device Name: [devicenamehere] Username: [test\testy] Department: []

and ERRRORRRRR

(‘action input JSON was invalid’, <ValidationError: “‘Identifier: 6abca32a Device Name: [devicenamehere] Username: [test\\testy] Department: []’ is not of type ‘object’”>)
Traceback (most recent call last):
File “/usr/local/lib/python3.7/site-packages/komand-1.0.1-py3.7.egg/komand/plugin.py”, line 380, in start_step
step.input.validate(params)
File “/usr/local/lib/python3.7/site-packages/komand-1.0.1-py3.7.egg/komand/variables.py”, line 24, in validate
validate(parameters, self.schema)
File “/usr/local/lib/python3.7/site-packages/jsonschema-2.3.0-py3.7.egg/jsonschema/validators.py”, line 432, in validate
cls(schema, *args, **kwargs).validate(instance)
File “/usr/local/lib/python3.7/site-packages/jsonschema-2.3.0-py3.7.egg/jsonschema/validators.py”, line 117, in validate
raise error
jsonschema.exceptions.ValidationError: ‘Identifier: 6abca32a Device Name: [devicenamehere] Username: [test\testy] Department: []’ is not of type ‘object’

Failed validating ‘type’ in schema[‘properties’][‘input’]:
{‘description’: 'Input object to be passed as params={} to the run
‘function’,
‘order’: 2,
‘title’: ‘Input’,
‘type’: ‘object’}

On instance[‘input’]:
('Identifier: 6abca32a Device Name: [devicenamehere] Username: ’
‘[test\testy] Department: []’)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/usr/local/lib/python3.7/site-packages/komand-1.0.1-py3.7.egg/komand/plugin.py”, line 311, in handle_step
output = self.start_step(input_message[‘body’], ‘action’, logger, log_stream, is_test, is_debug)
File “/usr/local/lib/python3.7/site-packages/komand-1.0.1-py3.7.egg/komand/plugin.py”, line 387, in start_step
raise ClientException(’{} input JSON was invalid’.format(step_key), e)
komand.exceptions.ClientException: (‘action input JSON was invalid’, <ValidationError: “‘Identifier: 6abca32a Device Name: [devicenamehere] Username: [test\\testy] Department: []’ is not of type ‘object’”>)

I failed on the first pass, no matches for me.

@hayden_redd, can you try this in pattern match?

Identifier: {{identifier:/([^\s]*)/}} Device Name: {{device_name:/\[([^\]]*)\]/}} Username: {{username:/\[([^\]]*)\]/}} Department: {{department:/\[([^\]]*)\]/}}

image
image

1 Like

Just use this:

def run(params={}):
    input = params.get("input")
    split_string = input.split(":")
    output_dict = {}
    for s in split_string:
        split_element = s.strip().split(" ")
        if len(split_element) > 1:
            output_dict[split_element[0]]=" ".join(split_element[1:])

    return output_dict

I think you may have included the print statement at the end which will cause that to fail. I left it there for debugging if you’re writing this outside of ICON in a python editor of some sort.

The error I believe is telling you it’s trying to parse that print statement.

Identifier: 6abca32a Device Name: [devicenamehere] Username: [test@test.com] Department: []

So found out sometimes it is like this and it breaks the regex!

@hayden_redd, I’m having trouble figuring out what about that is breaking it. It looks like the only thing different is that @ in the username. Each of the[^\]] should ensure that everything is matched that is not a closing square bracket. Testing on Regex 101 matches it successfully. (Sorry if I missed some other difference)

Link to the regex match below:
https://regex101.com/r/21hO9m/1