Skip to content

Instantly share code, notes, and snippets.

@ezeeetm
Last active November 5, 2015 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ezeeetm/a1ce792d0b91a83f6582 to your computer and use it in GitHub Desktop.
Save ezeeetm/a1ce792d0b91a83f6582 to your computer and use it in GitHub Desktop.
slackDailyDigest.ps1
<#
.SYNOPSIS
Dumps the last 24h of message history from all [NM]Slack channels
to [NM]Google Group in a daily digest email.
Run it nightly as a scheduled task.
.NOTES
AUTHOR: ezee
VERSION: 1.0.1
DATE: 19FEB2015
DEPENDS: None
COMMENTS:
- Does not accept parameters or pipes. Use config/init section below
- Enable this for use with gmail accounts: https://www.google.com/settings/security/lesssecureapps. System.Net.Mail.SmtpClient sucks.
- CHANGELOG at end of script
#>
# config/init
$token = "your_slack_token"
$msgCount = 1000 # value from 1-1000, just leave it @ 1000 unless you want to deal w/ ""has_more": true" in the resp and paging. I don't.
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "sender@gmail.com"
$Password = 'sender_pw'
$to = "someone@somedomain.com"
$subject = "[NM] Slack Digest for $(Get-Date -format "dd-MMM-yyyy")"
# LOGGING & ERROR HANDLING BOILERPLATE
clear
# create .\<ScriptName>Logs\ dir if it does not exit
$path = Split-Path $MyInvocation.MyCommand.Path
Set-Location -Path $path
$ScriptName = "slackDailyDigest"
$logPath = ".\"+$ScriptName+"Logs"
if((Test-Path $logPath) -eq $false){md -Name $logPath | out-null} #suppress stdout
# start new log file w/ timestamp filename and date header
$logStartDateTime = Get-Date -f yyyy-MM-dd
$logHeader = "Log File Started: "+(Get-Date).ToString()+"`r`n-------------------------------------"
$logHeader | Out-File ".\$logPath\$logStartDateTime.log" -Append
function log ($logLevel, $logData)
{
$logEntry = (Get-Date).ToString()+"`t"+$logLevel+"`t"+$logData
Write-Host $logEntry
$logEntry | Out-File ".\$logPath\$logStartDateTime.log" -Append
}
function HandleException($function,$exceptionMessage)
{
log WARNING "Exception in $($function): $exceptionMessage"
}
# /LOGGING & ERROR HANDLING BOILERPLATE
function getChannels
{
try
{
log INFO "getChannels: STARTED"
$channelsList = @{}
$channels = Invoke-WebRequest "https://slack.com/api/channels.list?token=$token"
$channelsJson = $channels.Content | ConvertFrom-Json
foreach ($channel in $($channelsJson.channels))
{
$id = $channel.id
$name = $channel.name
$channelsList.Add($id, $name)
}
}
Catch [Exception]
{
$exMsg = $_.Exception.Message
HandleException "getChannels" $exMsg
}
log INFO "getChannels: COMPLETED"
return $channelsList
}
function getChannelHistory ($channelId)
{
Try
{
log INFO "getChannelHistory for $($channelId): STARTED"
$epochStart = Get-Date -Date "01/01/1970"
$epochNow = Get-Date
$unixTimeStampNow = (New-TimeSpan -Start $epochStart -End $epochNow).TotalSeconds
$oldest = $unixTimeStampNow - (24*60*60)
$history = Invoke-WebRequest "https://slack.com/api/channels.history?token=$token&channel=$channelId&count=$msgCount&oldest=$oldest"
$historyJson = $history | ConvertFrom-Json
}
Catch [Exception]
{
$exMsg = $_.Exception.Message
HandleException "getChannelHistory" $exMsg
}
log INFO "getChannelHistory for $($channelId): COMPLETED"
return $historyJson
}
function buildEmailBody ($channels)
{
Try
{
log INFO "buildEmailBody: STARTED"
$userIds = @()
$emailBody = ""
foreach ($channelId in $($channels.Keys))
{
$channelName = $channels.Get_Item($channelId)
$emailBody += "BEGIN DIGEST FOR $channelId / $channelName`n"
$emailBody += "#################################################################`n"
$messages = getChannelHistory $channelId
$messages = $messages.messages | Sort-Object ts # otherwise lists in rev msg order
foreach ($message in $messages)
{
if ($message.text.contains("has joined the channel")){continue}
if (!$userIds.Contains($($message.user))) {$userIds += $message.user}
$emailBody += "$($message.user): $($message.text)`n"
}
$emailBody += "`n"
}
#replace userId GUIDs w usernames
foreach ($userId in $userIds)
{
$user = Invoke-WebRequest "https://slack.com/api/users.info?token=$token&user=$userId"
$userJson = $user | ConvertFrom-Json
$currName = $userJson.user.name
$emailBody = $emailBody.Replace($userId,$currName)
}
}
Catch [Exception]
{
$exMsg = $_.Exception.Message
HandleException "buildEmailBody" $exMsg
}
log INFO "buildEmailBody: COMPLETED"
return $emailBody
}
function sendEmail ($emailBody)
{
Try
{
log INFO "sendEmail: STARTED"
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $emailBody
$message.to.add($to)
$message.from = $username
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message)
}
Catch [Exception]
{
$exMsg = $_.Exception.Message
HandleException "sendEmail" $exMsg
}
log INFO "sendEmail: COMPLETED"
}
# ----------------------- main -----------------------
log INFO "slackDailyDigest: STARTED"
$channels = getChannels
$emailBody = buildEmailBody $channels
sendEmail $emailBody
log INFO "slackDailyDigest: COMPLETED, script exiting"
<#
CHANGELOG
1.0.0 19FEB2015 Initial commit
1.0.1 19FEB2015 Fixed message ordering in email body
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment