-
Notifications
You must be signed in to change notification settings - Fork 0
/
UMN-HipChat.psm1
146 lines (122 loc) · 4.09 KB
/
UMN-HipChat.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# TODO: add permenent store for the api server variable for persistance between module loads.
# Global Variables
###
# Copyright 2017 University of Minnesota, Office of Information Technology
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
###
$HipChatAPIServer = "api.hipchat.com"
function Get-HipChatAPIServer {
<#
.SYNOPSIS
Gets the value of the HipChatAPIServer variable in the module
.DESCRIPTION
Gets the value of the HipChatAPIServer variable in the module
.EXAMPLE
Get-HipChatAPIServer
#>
return $script:HipChatAPIServer
}
function Set-HipChatAPIServer {
<#
.SYNOPSIS
Sets the value of the HipChatAPIServer variable in the module.
.DESCRIPTION
Takes in the HipChat API server name and sets it in the module.
.EXAMPLE
Set-HipChatAPIServer -HipChatAPIServer "api.hipchat.com"
.PARAMETER HipChatAPIServer
This is the name of the HipChat API server.
.NOTES
The variable set by this function will reset every time the module is imported.
#>
[CmdletBinding()]
param(
[String]$HipChatAPIServer
)
$script:HipChatAPIServer = $HipChatAPIServer
}
function Send-HipChat {
<#
.SYNOPSIS
This is a function which sends a message to a HipChat room.
.DESCRIPTION
Takes in a message, apitoken and room and sends a message to that HipChat room.
.PARAMETER Message
The string you want to send to the room in hipchat
.PARAMETER Color
Background color of the HipChat message. Can only be yellow, green, red, purple, gray or random.
They must be lowercase.
Default: gray
.PARAMETER Notify
A switch to indicate whether or not to ping everyone in the room about the message.
Default: $false
.PARAMETER APIToken
The API token generated to send messages to the specific room.
hipchat_server_address/rooms
.PARAMETER Room
The ID or html friendly name of the room.
ID can be found on the hipchat_server_address/rooms page
.PARAMETER Retry
How many retries if there's a failure.
Default: 0
.PARAMETER RetrySeconds
How many seconds to wait before retrying.
Default: 30
.NOTES
Name: Send-HipChat
Author: Jeff Bolduan
LASTEDIT: 10/7/2016
Based on code from: https://github.com/markwragg/Powershell-Hipchat
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $True)]
[String]$Message,
[Parameter(Mandatory = $False)]
[ValidateSet('yellow', 'green', 'red', 'purple', 'gray', 'random')]
[String]$Color = 'Gray',
[Parameter(Mandatory = $False)]
[Switch]$Notify = $false,
[Parameter(Mandatory = $True)]
[String]$APIToken,
[Parameter(Mandatory = $True)]
[String]$Room,
[Parameter(Mandatory = $False)]
[int]$Retry = 0,
[Parameter(Mandatory = $False)]
[int]$RetrySeconds = 30
)
# Create the message
$MessageHash = @{
"message" = $Message;
"color" = $Color;
"notify" = [String]$Notify
}
$URI = "https://$($script:HipChatAPIServer)/v2/room/$Room/notification?auth_token=$APIToken"
$Body = ConvertTo-Json -InputObject $MessageHash
#Write-Host $Body
for($RetryCount = 0; $RetryCount -le $Retry; $RetryCount++) {
try {
$Response = Invoke-WebRequest -Method Post -Uri $URI -Body $Body -ContentType "application/json" -ErrorAction SilentlyContinue -UseBasicParsing
#Write-Verbose "$Message has been sent"
return $True
} catch {
Write-Error "Cound not send message: `r`n $($_.Exception.ToString())"
if($RetryCount -lt $Retry) {
Write-Warning -Message "Retrying in $RetrySeconds seconds..."
Start-Sleep -Seconds $RetrySeconds
}
}
}
Write-Warning -Message "Could not send after $RetryCount tries."
return $false
}