Skip to content

Instantly share code, notes, and snippets.

@gesslar
Last active April 13, 2021 20:38
Show Gist options
  • Save gesslar/ca54afed2fcee4fed90c6597391cdba1 to your computer and use it in GitHub Desktop.
Save gesslar/ca54afed2fcee4fed90c6597391cdba1 to your computer and use it in GitHub Desktop.
Daily Email Summary posts to Google Chat webhook - Number of emails, labels used, unique senders;
const dateBoundaryFactory = (today = new Date()) => {
const yesterday = new Date(today)
const yesternight = new Date(today)
// set yesterday to 00:00:00
yesterday.setDate(today.getDate() - 1)
yesterday.setHours(0)
yesterday.setMinutes(0)
yesterday.setSeconds(0)
// set yesternight to 23:59:59
yesternight.setDate(today.getDate() - 1)
yesternight.setHours(23)
yesternight.setMinutes(59)
yesternight.setSeconds(59)
return {
yesterdayStart: yesterday,
yesterdayEnd: yesternight,
todayQueryString: `${today.getFullYear()}/${today.getMonth() + 1}/${today.getDate()}`,
yesterdayQueryString: `${yesterday.getFullYear()}/${yesterday.getMonth() + 1}/${yesterday.getDate()}`
}
}
function bot() {
const webhook = "<PASTE CHAT WEBHOOK HERE>"
const dateInfo = dateBoundaryFactory(new Date())
const query = "newer:"+ dateInfo.yesterdayQueryString + " older:" + dateInfo.todayQueryString + " -in:sent -in:drafts -in:chats -in:spam"
const threads = GmailApp.search(query)
let allLabels = {}, allSenders = {}, messageCount = 0
threads.forEach( thread => {
const labels = thread.getLabels()
labels.forEach( label => {
const labelName = label.getName()
if(allLabels.hasOwnProperty(labelName)) allLabels[labelName]++
else allLabels[labelName] = 1
})
const messages = thread.getMessages()
messages.forEach( message => {
const messageTime = message.getDate().getTime()
if(messageTime >= dateInfo.yesterdayStart.getTime() && messageTime <= dateInfo.yesterdayEnd.getTime()) {
messageCount ++
const senderInfo = { name: "", email: "" }
const sender = message.getFrom().replace(/\"/g, "")
const emails = sender.match(/[^@<\s]+@[^@\s>]+/g)
senderInfo.email = emails[0]
let names = sender.split(/\s+/)
if(names.length > 1) {
names.pop()
senderInfo.name = names.join(" ").replace(/"/g, "")
}
if(allSenders.hasOwnProperty[senderInfo.email]) allSenders[senderInfo.email]++
else allSenders[senderInfo.email] = 1
}
})
})
// NOW FOR THE STATS
const theLabels = Object.keys(allLabels)
const labelString = theLabels.sort().join("\n")
const theSenders = Object.keys(allSenders)
const senderString = theSenders.sort().join("\n")
const payload = {
sender: {
displayName: "Robot",
avatarUrl: "https://goo.gl/8oz4jG"
},
cards: [
{
header: {
title: `Email Summary for ${dateInfo.yesterdayStart.toDateString()}`
},
sections: [
{
widgets: [
{
keyValue: {
topLabel: "Number of messages",
content: messageCount.toString()
}
},
{
keyValue: {
topLabel: "Number of labels",
content: theLabels.length.toString()
}
},
{
keyValue: {
topLabel: "Labels used",
content: labelString
}
},
{
keyValue: {
topLabel: "Number of unique senders",
content: theSenders.length.toString()
}
},
{
keyValue: {
topLabel: "Senders",
content: senderString
}
}
]
}
]
}
]
}
const response = UrlFetchApp.fetch(webhook, {
headers: {
'Content-Type': 'application/json; charset=UTF-8'
},
method: "POST",
payload: JSON.stringify(payload)
})
Logger.log(response.getContentText())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment