Last active
February 11, 2023 03:14
-
-
Save scarabaeus/d7aef0b88cf7703ad5c2cd10eaffb741 to your computer and use it in GitHub Desktop.
Parsing multiple-part Gmail responses for Slack via JSONata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
'channel': $.channel, | |
'blocks': [ | |
{ | |
'type': 'section', | |
'text': { | |
'type':'mrkdwn', | |
'text': '------------------------------\n*You have mail!*' | |
} | |
}, | |
$map($filter($.mailMessageSuccess.payload.headers, function($v, $i, $a) { | |
/* Filter out all headers that aren't From, Subject, or Date for display in the Slack notification */ | |
$v.name = 'From' or $v.name = 'Subject' or $v.name = 'Date' | |
}), function($v, $i, $a) { | |
{ | |
'type': 'section', | |
'text': { | |
'type': 'mrkdwn', | |
'text': '*' & $v.name & ':* ' & $v.value | |
} | |
} | |
}), | |
$map($filter($.mailMessageSuccess.payload.parts, function($v, $i, $a) { | |
/* TODO: Find the decode strategy for body parts that not base64 encoded */ | |
$v.mimeType = 'text/plain' | |
}), function($v, $i, $a) { | |
{ | |
'type': 'section', | |
'text': { | |
'type': 'mrkdwn', | |
'text': $replace($base64decode($v.body.data), '\r\n', '\n') | |
} | |
} | |
}), | |
{ | |
'type': 'section', | |
'text': { | |
'type':'mrkdwn', | |
'text': '(Sent with ❤️ from instanceId: ' & $._.instanceId & ')\n------------------------------' | |
} | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Potential improvment that filters, not by mime-type, but by whether the body can be $base64decode()'ed | |
**/ | |
{ | |
'channel': $.channel, | |
'blocks': [ | |
{ | |
'type': 'section', | |
'text': { | |
'type':'mrkdwn', | |
'text': '------------------------------\n*You have mail!*' | |
} | |
}, | |
$map($filter($.mailMessageSuccess.payload.headers, function($v, $i, $a) { | |
$v.name = 'From' or $v.name = 'Subject' or $v.name = 'Date' | |
}), function($v, $i, $a) { | |
{ | |
'type': 'section', | |
'text': { | |
'type': 'mrkdwn', | |
'text': '*' & $v.name & ':* ' & $v.value | |
} | |
} | |
}), | |
$map($filter($.mailMessageSuccess.payload.parts, function($v, $i, $a) { | |
$contains($v.body.data, /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$/) | |
}), function($v, $i, $a) { | |
{ | |
'type': 'section', | |
'text': { | |
'type': 'mrkdwn', | |
'text': $replace($base64decode($v.body.data), '\r\n', '\n') | |
} | |
} | |
}), | |
{ | |
'type': 'section', | |
'text': { | |
'type':'mrkdwn', | |
'text': '(Sent with ❤️ from instanceId: ' & $._.instanceId & ')\n------------------------------' | |
} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Produces this in Slack: