Skip to content

Commit

Permalink
!deploy v2.13.0 to resolve #77 and update commands to work with PoshB…
Browse files Browse the repository at this point in the history
…ot.GChat.Backend

#### 2.13.0

* Fixed: Private list functions to check if a value is actually returned before adding members to the returned objects ([Issue #77](#77))
* Added: `Update-GSChatMessage` to allow updating existing messages in Chat (i.e. on Card Clicked events)
* Updated: Order of parameters in `Get-GSToken` to place `Scopes` first, as it's the only required parameter
* Updated: `Get-GSChatSpace` now updates the config with Space names/shortnames for ease of use
* Updated: `Send-GSChatMessage` to also support calling the REST API as an additional option. This is necessary for PoshBot due to the deserialization of objects passed back to result parser breaking the Google SDK type references
* Updated: `Get-GSChatConfig` to always fetch the latest config if no ConfigName is passed instead of using `Show-PSGSuiteConfig`
* Updated: `Set-PSGSuiteConfig` to refresh the Spaces dictionary each time in order to remove stale spaces (i.e. on removal of bot from a Room or DM)
* Fixed: `Add-GSChatOnClick` now properly builds the hashtable for the Webhook object
* Updated: `Get-GSUser` to allow passing User ID's instead of emails by checking if value passed is a `decimal` before concatenating the domain name.
  • Loading branch information
scrthq authored Aug 15, 2018
2 parents 41c05c7 + d82f503 commit 032e160
Show file tree
Hide file tree
Showing 19 changed files with 532 additions and 56 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ TestPad.ps1
**.insyncdl
.vscode
API-to-Function-Map.md
QueueExample.ps1
QueueExample.ps1
ChatBotLoop.ps1
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<!-- TOC -->

- [Changelog](#changelog)
- [2.13.0](#2130)
- [2.12.1](#2121)
- [2.12.0](#2120)
- [2.11.0](#2110)
Expand Down Expand Up @@ -45,6 +46,18 @@

<!-- /TOC -->

## 2.13.0

* Fixed: Private list functions to check if a value is actually returned before adding members to the returned objects ([Issue #77](https://github.com/scrthq/PSGSuite/issues/77))
* Added: `Update-GSChatMessage` to allow updating existing messages in Chat (i.e. on Card Clicked events)
* Updated: Order of parameters in `Get-GSToken` to place `Scopes` first, as it's the only required parameter
* Updated: `Get-GSChatSpace` now updates the config with Space names/shortnames for ease of use
* Updated: `Send-GSChatMessage` to also support calling the REST API as an additional option. This is necessary for PoshBot due to the deserialization of objects passed back to result parser breaking the Google SDK type references
* Updated: `Get-GSChatConfig` to always fetch the latest config if no ConfigName is passed instead of using `Show-PSGSuiteConfig`
* Updated: `Set-PSGSuiteConfig` to refresh the Spaces dictionary each time in order to remove stale spaces (i.e. on removal of bot from a Room or DM)
* Fixed: `Add-GSChatOnClick` now properly builds the hashtable for the Webhook object
* Updated: `Get-GSUser` to allow passing User ID's instead of emails by checking if value passed is a `decimal` before concatenating the domain name.

## 2.12.1

* Fixed: `Get-GSDrivePermission` now returns all fields (including EmailAddress)
Expand Down
2 changes: 1 addition & 1 deletion PSGSuite/PSGSuite.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'PSGSuite.psm1'

# Version number of this module.
ModuleVersion = '2.12.1'
ModuleVersion = '2.13.0'

# ID used to uniquely identify this module
GUID = '9d751152-e83e-40bb-a6db-4c329092aaec'
Expand Down
8 changes: 4 additions & 4 deletions PSGSuite/Private/Legacy/Get-GSToken.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ function Get-GSToken {
#>
Param
(
[parameter(Mandatory = $false,HelpMessage = "What is the full path to your Google Service Account's P12 key file?")]
[ValidateNotNullOrEmpty()]
[String]
$P12KeyPath = $Script:PSGSuite.P12KeyPath,
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string[]]
$Scopes,
[parameter(Mandatory = $false,HelpMessage = "What is the full path to your Google Service Account's P12 key file?")]
[ValidateNotNullOrEmpty()]
[String]
$P12KeyPath = $Script:PSGSuite.P12KeyPath,
[parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[String]
Expand Down
4 changes: 3 additions & 1 deletion PSGSuite/Private/ListPrivate/Get-GSGroupListPrivate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ function Get-GSGroupListPrivate {
[int]$i = 1
do {
$result = $request.Execute()
$result.GroupsValue | Add-Member -MemberType ScriptMethod -Name ToString -Value {$this.Email} -PassThru -Force
if ($null -ne $result.GroupsValue) {
$result.GroupsValue | Add-Member -MemberType ScriptMethod -Name ToString -Value {$this.Email} -PassThru -Force
}
$request.PageToken = $result.NextPageToken
[int]$retrieved = ($i + $result.GroupsValue.Count) - 1
Write-Verbose "Retrieved $retrieved groups..."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ function Get-GSGroupMemberListPrivate {
[int]$i = 1
do {
$result = $request.Execute()
$result.MembersValue | Add-Member -MemberType NoteProperty -Name 'Group' -Value $Id -PassThru | Add-Member -MemberType ScriptMethod -Name ToString -Value {$this.Email} -PassThru -Force
if ($null -ne $result.MembersValue) {
$result.MembersValue | Add-Member -MemberType NoteProperty -Name 'Group' -Value $Id -PassThru | Add-Member -MemberType ScriptMethod -Name ToString -Value {$this.Email} -PassThru -Force
}
$request.PageToken = $result.NextPageToken
[int]$retrieved = ($i + $result.MembersValue.Count) - 1
Write-Verbose "Retrieved $retrieved members..."
Expand Down
5 changes: 4 additions & 1 deletion PSGSuite/Private/ListPrivate/Get-GSShortUrlListPrivate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ function Get-GSShortUrlListPrivate {
try {
Write-Verbose "Getting Short Url list for User '$U'"
$request = $service.Url.List()
$request.Execute() | Select-Object -ExpandProperty Items | Add-Member -MemberType NoteProperty -Name 'User' -Value $U -PassThru
$result = $request.Execute()
if ($null -ne $result.Items) {
$result.Items | Add-Member -MemberType NoteProperty -Name 'User' -Value $U -PassThru
}
}
catch {
if ($ErrorActionPreference -eq 'Stop') {
Expand Down
5 changes: 4 additions & 1 deletion PSGSuite/Private/ListPrivate/Get-GSUserASPListPrivate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
}
Write-Verbose "Getting ASP list for User '$U'"
$request = $service.Asps.List($U)
$request.Execute() | Select-Object -ExpandProperty Items | Add-Member -MemberType NoteProperty -Name 'User' -Value $U -PassThru
$result = $request.Execute()
if ($null -ne $result.Items) {
$result.Items | Add-Member -MemberType NoteProperty -Name 'User' -Value $U -PassThru
}
}
catch {
if ($ErrorActionPreference -eq 'Stop') {
Expand Down
5 changes: 4 additions & 1 deletion PSGSuite/Private/ListPrivate/Get-GSUserSchemaListPrivate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ function Get-GSUserSchemaListPrivate {
Process {
try {
$request = $service.Schemas.List($Script:PSGSuite.CustomerId)
$request.Execute() | Select-Object -ExpandProperty SchemasValue
$result = $request.Execute()
if ($null -ne $result.SchemasValue) {
$result.SchemasValue
}
}
catch {
if ($ErrorActionPreference -eq 'Stop') {
Expand Down
5 changes: 4 additions & 1 deletion PSGSuite/Private/ListPrivate/Get-GSUserTokenListPrivate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
}
Write-Verbose "Getting Token list for User '$U'"
$request = $service.Tokens.List($U)
$request.Execute() | Select-Object -ExpandProperty Items | Add-Member -MemberType NoteProperty -Name 'User' -Value $U -PassThru
$result = $request.Execute()
if ($null -ne $result.Items) {
$result.Items | Add-Member -MemberType NoteProperty -Name 'User' -Value $U -PassThru
}
}
catch {
if ($ErrorActionPreference -eq 'Stop') {
Expand Down
16 changes: 12 additions & 4 deletions PSGSuite/Public/Chat/Get-GSChatSpace.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,23 @@ function Get-GSChatSpace {
}
End {
Write-Verbose "Updating PSGSuite Config with Space list"
$spaceHash = $spaceArray | ForEach-Object {
$spaceHashArray = @()
$spaceArray | ForEach-Object {
if ($_.DisplayName) {
Set-PSGSuiteConfig -Space @{$_.DisplayName = $_.Name} -Verbose:$false
$spaceHashArray += @{$_.DisplayName = $_.Name}

}
else {
Set-PSGSuiteConfig -Space @{DM = $_.Name} -Verbose:$false
$member = Get-GSChatMember -Space $_.Name -Verbose:$false
$id = $member.Member.Name
$primaryEmail = (Get-GSUser -User ($id.Replace('users/',''))).PrimaryEmail
$spaceHashArray += @{
$id = $_.Name
$member.Member.DisplayName = $_.Name
$primaryEmail = $_.Name
}
}
}

Set-PSGSuiteConfig -Space $spaceHashArray -Verbose:$false
}
}
96 changes: 76 additions & 20 deletions PSGSuite/Public/Chat/Send-GSChatMessage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@ function Send-GSChatMessage {
[string[]]
$Parent,
[parameter(Mandatory = $false,ParameterSetName = "SDK")]
[parameter(Mandatory = $false,ParameterSetName = "Rest")]
[string]
$ThreadKey,
[parameter(Mandatory = $true,ParameterSetName = "Rest")]
[string[]]
$RestParent,
[parameter(Mandatory = $true,ParameterSetName = "Webhook")]
[string[]]
$Webhook,
Expand All @@ -142,14 +146,17 @@ function Send-GSChatMessage {
}
})]
[Object[]]
$MessageSegment
$MessageSegment,
[parameter(Mandatory = $true,ParameterSetName = "BodyPassThru")]
[switch]
$BodyPassThru
)
Begin {
$addlSections = @()
$addlCardActions = @()
$addlSectionWidgets = @()
switch ($PSCmdlet.ParameterSetName) {
Webhook {
default {
$body = @{}
foreach ($key in $PSBoundParameters.Keys) {
switch ($key) {
Expand Down Expand Up @@ -224,10 +231,10 @@ function Send-GSChatMessage {
}
Process {
foreach ($segment in $MessageSegment) {
switch ($segment.PSTypeNames[0]) {
'PSGSuite.Chat.Message.Card' {
switch -RegEx ($segment['SDK'].PSTypeNames[0]) {
'(.*?)Google\.Apis\.HangoutsChat\.v1\.Data\.Card' {
switch ($PSCmdlet.ParameterSetName) {
Webhook {
default {
if (!$body['cards']) {
$body['cards'] = @()
}
Expand All @@ -241,21 +248,22 @@ function Send-GSChatMessage {
}
}
}
'PSGSuite.Chat.Message.Card.Section' {
'(.*?)Google\.Apis\.HangoutsChat\.v1\.Data\.Section' {
$addlSections += $segment
}
'PSGSuite.Chat.Message.Card.CardAction' {
'(.*?)Google\.Apis\.HangoutsChat\.v1\.Data\.CardAction' {
$addlCardActions += $segment
}
default {
Write-Verbose "Matched a $($segment['SDK'].PSTypeNames[0]) in the MessageSegments!"
$addlSectionWidgets += $segment
}
}
}
}
End {
switch ($PSCmdlet.ParameterSetName) {
Webhook {
default {
if ($addlCardActions -or $addlSections -or $addlSectionWidgets) {
if (!$body['cards']) {
$cardless = $true
Expand Down Expand Up @@ -290,22 +298,70 @@ function Send-GSChatMessage {
}
}
}
$body = $body | ConvertTo-Json -Depth 15
foreach ($hook in $Webhook) {
try {
if ($hook -notlike "https://chat.googleapis.com/v1/spaces/*") {
$hook = Get-GSChatConfig -WebhookName $hook -ErrorAction Stop
switch ($PSCmdlet.ParameterSetName) {
Webhook {
$body = $body | ConvertTo-Json -Depth 15
foreach ($hook in $Webhook) {
try {
if ($hook -notlike "https://chat.googleapis.com/v1/spaces/*") {
$hook = Get-GSChatConfig -WebhookName $hook -ErrorAction Stop
}
Write-Verbose "Sending Chat Message via Webhook to '$($hook -replace "\?key\=.*",'')'"
Invoke-RestMethod -Method Post -Uri ([Uri]$hook) -Body $body -ContentType 'application/json' -Verbose:$false | Add-Member -MemberType NoteProperty -Name 'Webhook' -Value $hook -PassThru
}
catch {
if ($ErrorActionPreference -eq 'Stop') {
$PSCmdlet.ThrowTerminatingError($_)
}
else {
Write-Error $_
}
}
}
Write-Verbose "Sending Chat Message via Webhook to '$($hook -replace "\?key\=.*",'')'"
Invoke-RestMethod -Method Post -Uri ([Uri]$hook) -Body $body -ContentType 'application/json' -Verbose:$false | Add-Member -MemberType NoteProperty -Name 'Webhook' -Value $hook -PassThru
}
catch {
if ($ErrorActionPreference -eq 'Stop') {
$PSCmdlet.ThrowTerminatingError($_)
Rest {
$body = $body | ConvertTo-Json -Depth 15
foreach ($restPar in $RestParent) {
try {
if ($restPar -notlike "spaces/*") {
try {
$restPar = Get-GSChatConfig -SpaceName $restPar -ErrorAction Stop
}
catch {
$restPar = "spaces/$restPar"
}
}
$header = @{
Authorization = "Bearer $(Get-GSToken -Scopes "https://www.googleapis.com/auth/chat.bot" -Verbose:$false)"
}
$hook = "https://chat.googleapis.com/v1/$($restPar)/messages"
if ($PSBoundParameters.Keys -contains 'ThreadKey') {
$hook = "$($hook)?threadKey=$ThreadKey"
$addlText = " in ThreadKey '$ThreadKey'"
}
else {
$addlText = ""
}
Write-Verbose "Sending Chat Message via REST API to parent '$restPar'$addlText"
Invoke-RestMethod -Method Post -Uri ([Uri]$hook) -Headers $header -Body $body -ContentType 'application/json' -Verbose:$false | Add-Member -MemberType NoteProperty -Name 'RestParent' -Value $restPar -PassThru
}
catch {
if ($ErrorActionPreference -eq 'Stop') {
$PSCmdlet.ThrowTerminatingError($_)
}
else {
Write-Error $_
}
}
}
else {
Write-Error $_
}
BodyPassThru {
$newBody = @{
token = (Get-GSToken -Scopes "https://www.googleapis.com/auth/chat.bot" -Verbose:$false)
body = $body
}
$newBody = $newBody | ConvertTo-Json -Depth 20 -Compress
return $newBody
}
}
}
Expand Down
Loading

0 comments on commit 032e160

Please sign in to comment.