Skip to content

Instantly share code, notes, and snippets.

@alexander-yu-shamin
Last active February 10, 2022 12:40
Show Gist options
  • Save alexander-yu-shamin/6866a5b8fc52fae05ab4e7083ddd4775 to your computer and use it in GitHub Desktop.
Save alexander-yu-shamin/6866a5b8fc52fae05ab4e7083ddd4775 to your computer and use it in GitHub Desktop.
Small script for sending messages in slack.
<#
# How install
1. Go to https://api.slack.com/apps -> Select workspace -> Create App -> create app from manifest
1.1 Or go to <Workspace> -> Administration -> Manage Apps -> Build -> Create App -> create app from manifest
```yaml
display_information:
name: Shell Services
oauth_config:
scopes:
user:
- chat:write
settings:
org_deploy_enabled: false
socket_mode_enabled: false
token_rotation_enabled: false
```
2. Go to "QAuth&Permission" -> "Install to Workspace"
3. Copy "User OAuth Token" to variable $token
4. Set up your settings: workingTime, restingTime, messages...
# How using
- from powershell: `ashamin-tr-send-slack-message.ps1 swt` -> send to channel work time
- from powershell: `ashamin-tr-send-slack-message.ps1 rest` -> send to channel resting up/down
#>
$token = ""
$workingTime = New-TimeSpan -Hours 8 -Minutes 0
$restingTime = New-TimeSpan -Hours 1 -Minutes 0
$messageRestingUp = "Перерыв начался"
$messageRestingDown = "Перерыв закончен"
$tempFileName = ".temp_of_script_send_slack_messages"
$tempFile = Join-Path -Path $env:USERPROFILE -ChildPath $tempFileName
$channelId = "C011LEDV3MH"
function Send-Slack{
# Add the "Incoming WebHooks" integration to get started: https://slack.com/apps/A0F7XDUAZ-incoming-webhooks
param (
[Parameter(Mandatory=$true, Position=0)]$Text,
$Url="https://hooks.slack.com/services/xxxxx", #Put your URL here so you don't have to specify it every time.
# Parameters below are optional and will fall back to the default setting for the webhook.
$Channel # Channel to post message. Can be in the format "@username" or "#channel"
)
$postSlackMessage = @{token=$token;channel=$Channel; text=$Text}
Invoke-RestMethod -Method Post -Uri "https://slack.com/api/chat.postMessage" -Body $postSlackMessage | Out-Null
}
Function Send-Work-Time{
$currentDateTime = Get-Date
$endDateTime = $currentDateTime + $workingTime + $restingTime
$messageToSlack = $currentDateTime.ToString("HH:mm") + " - " + $endDateTime.ToString("HH:mm")
Send-Slack -Text $messageToSlack -Channel $channelId
}
Function Send-Rest{
if(Test-Path $tempFile){
Remove-Item -Path $tempFile
$messageToSlack = $messageRestingDown
}
else
{
New-Item -Path $env:USERPROFILE -Name $tempFileName -ItemType "file" | Out-Null
$messageToSlack = $messageRestingUp
}
if(-not([string]::IsNullOrEmpty($messageToSlack))){
Send-Slack -Text $messageToSlack -Channel $channelId
}
}
if([string]::IsNullOrEmpty($token)){
Write-Host "You need set up the script. Read a header in the script."
Return
}
if([string]::IsNullOrEmpty($args[0])){
Write-Host "Input your choice:
- [swt]: send work time to slack
- [rest]: send status of rest up/down"
$userChoice = Read-Host
if([string]::IsNullOrEmpty($userChoice)){ Return }
}
else
{
$userChoice = $args[0]
}
if($userChoice -eq "swt") { Send-Work-Time; Return }
if($userChoice -eq "rest") { Send-Rest; Return }
"I don't know this command"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment