Forward Customer E-mails to Slack

This source automatically turns a Slack channel (i.e. #support-emails
) into
a notification channel for customer support e-mails within a single Gmail
account. You can use it as an easy way to keep track of incoming customer
requests — perfect for small teams that share a support inbox and need
everybody to be notified of customer problems at the same time.
How It Works
Every minute, this source will make a request to the Gmail messages API
to retrieve a list of all E-mails from, at most, the past 24 hours. It will then
post a list of them in Slack using the the
Slack channels.messages.create API endpoint.
The most recent E-mail already displayed will be saved using the
utils key-value storage API, utils.kv so that
you won't get duplicate messages sent to your Slack channel.
The full steps are in functions/events/scheduler/minutely.js
;
First, we see what the last e-mail we retrieved was...
// Retrieve last e-mail data, if we need it.
// Limit it to last day of e-mail at worst case (1d * 24h * 60m * 60s)
console.log(`Fetching last e-mail info...`);
let lastUpdate = await lib.utils.kv['@0.1.16'].get({
key: EMAIL_UPDATE_KEY,
defaultValue: {
internalDate: ((new Date().valueOf() / 1000) | 0) - (1 * 24 * 60 * 60),
historyId: 0,
messageId: ''
}
});
Next, we grab a list of the most recent e-mails...
// Get Gmail messages after 1s before your last e-mail
// This is incase we got two emails within the same 1s, but somehow missed one
console.log(`Retrieving Gmail messages...`);
let gmailMessages = await lib.gmail.messages['@0.2.1'].list({
query: `after:${internalDate - 1}`,
labelIds: ['UNREAD', 'INBOX'],
includeSpamTrash: false
});
Third, we fetch individual e-mail contents in parallel...
console.log(`Getting e-mail data...`)
let messages = await Promise.all(
gmailMessages.map(msg => {
return new Promise((resolve, reject) => {
lib.gmail.messages['@0.2.1'].retrieve({id: msg.id}, (err, msg) => {
// ... E-mail parsing logic ...
});
})
})
);
Fourth, we send a Slack message containing our e-mails with help from
the Block Kit builder...
// Otherwise, send a Slack message!
// This uses the Block Kit builder: https://app.slack.com/block-kit-builder/
let slackResult = await lib.slack.channels['@0.7.2'].messages.create({
channel: EMAIL_UPDATE_SLACK_CHANNEL,
blocks: messages.reduce((blocks, msg) => {
// compose block
}, [])
});
Finally, we tell the utils.kv API what
our most recent E-mail in the Slack channel is.
// Update our last e-mail settings, to use on next check
await lib.utils.kv['@0.1.16'].set({
key: EMAIL_UPDATE_KEY,
value: {
internalDate: internalDate,
historyId: historyId,
messageId: messageId
}
});
That's it!
Autocode will automatically handle all authentication to Gmail and Slack for you.
You simply need to install this source by forking it, and you're good to go!
You can customize it all you want afterwards in the Autocode editor.
For support, please visit the Autocode Community Slack channel by clicking
Community > Slack in the top nav or by following @AutocodeHQ
on Twitter. Happy hacking!