Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit to Master #3

Merged
merged 2 commits into from
Jun 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.1.0 (Jun 30, 2017)

- Initial release
Binary file modified HipChatPS/HipChatPS.psd1
Binary file not shown.
10 changes: 10 additions & 0 deletions HipChatPS/Private/ConvertTo-Json.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#Required only for Powershell 2
if ($PSVersionTable.PSVersion.Major -lt 3 ){

function ConvertTo-Json([object] $item){
add-type -assembly system.web.extensions
$ps_js=new-object system.web.script.serialization.javascriptSerializer
return $ps_js.Serialize($item)
}

}
92 changes: 92 additions & 0 deletions HipChatPS/Public/Send-HipChat.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
function Send-HipChat {
<#
.SYNOPSIS
Sends messages to a Hipchat room.
.DESCRIPTION
Send-HipChat can be used within a script or at the console to send a message to a HipChat room.
.EXAMPLE
Send-Hipchat -Message 'Hello' -Color 'Green' -Notify -ApiToken myapitoken -Room MyRoom -Retry 5 -RetrySec 10

This sends a message of 'Hello' highlighted green in to a room named MyRoom. Users in the room will be notified
in their clients that a new message has been recevied. If it cannot successfully send the message it will retry
5 times, at 10 second intervals.
#>
[CmdletBinding()]
[OutputType([Boolean])]
Param(
#Required. The message body. 10,000 characters max.
[Parameter(Mandatory = $True)]
[string]$message,

#The background colour of the HipChat message. One of "yellow", "green", "red", "purple", "gray", or "random". (default: gray)
[ValidateSet('yellow', 'green', 'red', 'purple', 'gray','random')]
[string]$color = 'gray',

#Set whether or not this message should trigger a notification for people in the room. (default: false)
[switch]$notify,

#Required. This must be a HipChat API token created by a Room Admin for the room you are sending notifications to.
[Parameter(Mandatory = $True)]
[string]$apitoken,

#Required. The id or URL encoded name of the HipChat room you want to send the message to.
[Parameter(Mandatory = $True)]
[string]$room,

#The number of times to retry sending the message (default: 0)
[int]$retry = 0,

#The number of seconds to wait between tries (default: 30)
[int]$retrysecs = 30
)

$messageObj = @{
"message" = $message
"color" = $color
"notify" = [string]$notify
}

$uri = "https://api.hipchat.com/v2/room/$room/notification?auth_token=$apitoken"
$Body = ConvertTo-Json $messageObj
$Post = [System.Text.Encoding]::UTF8.GetBytes($Body)

$Retrycount = 0

While($RetryCount -le $retry){
try {
if ($PSVersionTable.PSVersion.Major -gt 2 ){
$Response = Invoke-WebRequest -Method Post -Uri $uri -Body $Body -ContentType "application/json" -ErrorAction SilentlyContinue
}else{
$Request = [System.Net.WebRequest]::Create($uri)
$Request.ContentType = "application/json"
$Request.ContentLength = $Post.Length
$Request.Method = "POST"

$requestStream = $Request.GetRequestStream()
$requestStream.Write($Post, 0,$Post.length)
$requestStream.Close()

$Response = $Request.GetResponse()

$stream = New-Object IO.StreamReader($Response.GetResponseStream(), $Response.ContentEncoding)
$stream.ReadToEnd() | Out-Null
$stream.Close()
$Response.Close()
}
Write-Verbose "'$message' sent!"
Return $true

} catch {
Write-Error "Could not send message: `r`n $_.Exception.ToString()"

If ($retrycount -lt $retry){
Write-Verbose "retrying in $retrysecs seconds..."
Start-Sleep -Seconds $retrysecs
}
}
$Retrycount++
}

Write-Verbose "Could not send after $Retrycount tries. I quit."
Return $false
}
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# HipChatPS

[![Build status](https://ci.appveyor.com/api/projects/status/qfeei0wai40h8f3u?svg=true)](https://ci.appveyor.com/project/markwragg/hipchatps)

A module for PowerShell with functions for interacting with the team chat tool "Hipchat" by Atlassian. The module utilises Hipchat API v2: https://www.hipchat.com/docs/apiv2.

## Commands

The following functions are available:

#### 1. Send-HipChat

For sending notifications into a room. Before you can use this you need to create an API v2 token for the room that you want to send notifications to.

To do this:

1. Go to https://yourdomain.hipchat.com/admin/rooms/ and select the room you wish to notify.
2. Go to Tokens.
3. Create a Send Notification token. Note the "Label" you define will be included with the notification.

> **Beware, tokens here: https://yourdomain.hipchat.com/admin/api will not work, these are for API v1.**

##### Example

Attempt to send a message to a room named "My Room" coloured green. Will retry 5 additional times if it fails, waiting 30 seconds between each attempt. Will write verbose output to console.

Send-HipChat -message "my message" -room "My%20Room" -apitoken a1b2c3d4e5f6a1b2c3d4e5f6 -color green -verbose -retry 5 -retrysec 30
Copy link

@beaudryj beaudryj Jun 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not have the room get encoded in the code instead of making user URL encode- https://gallery.technet.microsoft.com/scriptcenter/Encoding-and-Decoding-URL-99dc4256

#4

2 changes: 1 addition & 1 deletion Tests/HipChatPS.Help.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function Get-ParametersDefaultFirst {

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$projectRoot = Split-Path -Parent $here
$moduleName = "JIraPS"
$moduleName = "HipChatPS"
$moduleRoot = "$projectRoot\$moduleName"

# Removes all versions of the module from the session before importing
Expand Down
56 changes: 56 additions & 0 deletions Tests/Send-HipChat.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$projectRoot = Split-Path -Parent $here
$moduleRoot = "$projectRoot\HipChatPS"

. "$moduleRoot\Public\Send-HipChat.ps1"

Describe 'Send-HipChat' {

Mock Invoke-WebRequest { Import-Clixml "$here\Send-HipChat.invoke-webrequest.xml" }

It "should return true" {

$params = @{
message = "Pester test message"
room = "Test"
apitoken = "c6cS2qXSv1zRyUUXpPsu3bebVF43wx8bvPQK5vg6"
}

Send-HipChat @params | Should Be $true
}

It "should reject the colour blue" {

$params = @{
message = "Pester test message"
room = "Test"
apitoken = "fakefalsetoken"
color = "blue"
}

{send-hipchat @params} | Should Throw
}
}


Describe "send-hipchat timeouts" {

Mock Invoke-WebRequest {Throw}

It "should retry 3 additional times" {

$params = @{
message = "Pester test message"
room = "Test"
apitoken = "fakefalsetoken"
retry = 3
retrysecs = 1
ErrorAction = "SilentlyContinue"
}

send-hipchat @params | Should be $false
Assert-MockCalled Invoke-WebRequest -Exactly 4 -ModuleName $moduleName -Scope It

}

}
Loading