Skip to content

Instantly share code, notes, and snippets.

@scarabaeus
Last active February 11, 2023 03:14
Show Gist options
  • Save scarabaeus/d7aef0b88cf7703ad5c2cd10eaffb741 to your computer and use it in GitHub Desktop.
Save scarabaeus/d7aef0b88cf7703ad5c2cd10eaffb741 to your computer and use it in GitHub Desktop.
Parsing multiple-part Gmail responses for Slack via JSONata
{
'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------------------------------'
}
}
]
}
/**
* 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------------------------------'
}
}
]
}
@scarabaeus
Copy link
Author

Produces this in Slack:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment