Skip to content

Commit

Permalink
Added mentions for Adaptive Cards #17
Browse files Browse the repository at this point in the history
  • Loading branch information
PrzemyslawKlys committed Dec 4, 2021
1 parent d8bc648 commit d1fa61f
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 19 deletions.
8 changes: 7 additions & 1 deletion Examples/Adaptive Card/AdaptiveCard-Native05.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,11 @@ New-AdaptiveCard -Uri $Env:TEAMSPESTERID -VerticalContentAlignment center {
New-AdaptiveTextBlock -Size 'Medium' -Text 'Test Card Title 1' -Color Warning
New-AdaptiveTextBlock -Size 'Medium' -Text 'Test Card Title 1' -Color Good
}
New-AdaptiveColumn {
New-AdaptiveTextBlock -Size 'Medium' -Text 'Test Card Title 1 <at>Name</at>' -Color Warning
New-AdaptiveTextBlock -Size 'Medium' -Text 'Test Card Title 1 <at>Zenon Jaskuła</at>' -Color Warning
}
}
} -SelectAction Action.OpenUrl -SelectActionUrl 'https://evotec.xyz' -Verbose
New-AdaptiveMention -Text 'Zenon Jaskuła' -UserPrincipalName '[email protected]'
New-AdaptiveMention -Text 'Name' -UserPrincipalName '[email protected]'
} -Verbose #-SelectAction Action.OpenUrl -SelectActionUrl 'https://evotec.xyz'
56 changes: 38 additions & 18 deletions Public/New-AdaptiveCard.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
[switch] $FullWidth,
[switch] $AllowImageExpand
)
$Mentions = [System.Collections.Generic.List[System.Collections.Specialized.OrderedDictionary]]::new()
$Wrapper = [ordered]@{
"type" = "message"
"attachments" = @(
Expand All @@ -114,7 +115,14 @@
version = "1.2" # Currently maximum supported is 1.2 for Teams, available is 1.3
body = @(
if ($Body) {
& $Body
$OutputBody = & $Body
foreach ($B in $OutputBody) {
if ($B.type -eq 'mention') {
$Mentions.Add($B)
} else {
$B
}
}
}
)
actions = @(
Expand Down Expand Up @@ -168,25 +176,37 @@
title = $SelectActionTitle
url = $SelectActionUrl
}
<#
# this somewhat works, except it doesn't
$Wrapper['attachments'][0]['content']["msteams"] = @{
"entities" = @(
@{
"type" = "mention"
"text" = "<at>przemyslaw.klys</at>"
"mentioned" = @{
#"id" = "8:orgid:49f7e27a-ce6c-45ef-9936-6ef3e940583d"
#"id" = '29:49f7e27a-ce6c-45ef-9936-6ef3e940583d'
#"id" = '29:orgid:49f7e27a-ce6c-45ef-9936-6ef3e940583d'
"id" = '19:b6b525a2187848ddb257f59e374363bd'
#"id" = 'orgid:49f7e27a-ce6c-45ef-9936-6ef3e940583d'
#"id" = '49f7e27a-ce6c-45ef-9936-6ef3e940583d'
"name" = "przemyslaw.klys"

if ($Mentions.Count -gt 0) {
# this somewhat works, except it doesn't
$Wrapper['attachments'][0]['content']["msteams"] = @{
"entities" = @(
foreach ($Mention in $Mentions) {
$Mention
}
}
)
<#
@{
"type" = "mention"
"text" = "<at>przemyslaw.klys</at>"
"mentioned" = @{
#"id" = "8:orgid:49f7e27a-ce6c-45ef-9936-6ef3e940583d"
#"id" = '29:49f7e27a-ce6c-45ef-9936-6ef3e940583d'
#"id" = '29:orgid:49f7e27a-ce6c-45ef-9936-6ef3e940583d'
#"id" = '19:b6b525a2187848ddb257f59e374363bd'
#"id" = 'orgid:49f7e27a-ce6c-45ef-9936-6ef3e940583d'
#"id" = '49f7e27a-ce6c-45ef-9936-6ef3e940583d'
#"name" = "przemyslaw.klys"
id = '[email protected]'
name = 'Przemysław Kłys'
}
}
#>
)
}
}
<#
#>
<# this doesn't work, but tested
$Wrapper["msteams"] = @{
Expand Down
70 changes: 70 additions & 0 deletions Public/New-AdaptiveMention.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
function New-AdaptiveMention {
<#
.SYNOPSIS
Allows Adaptive Card to mention specific person in the notification.
.DESCRIPTION
Allows Adaptive Card to mention specific person in the notification.
Currently Adaptive Card in incoming webhook notifications allows you to define a mention property, but it doesn't notify the user on notification.
It's supposed to be enabled in future by Microsoft.
.PARAMETER Text
Provide the text to be mentioned by using <at>person</at> tag. You can provide tags or skip them. Should work either way.
.PARAMETER UserPrincipalName
Provide the user principal name of the person to be mentioned.
.PARAMETER Name
Provide the name of the person to be mentioned. This is optional.
.EXAMPLE
New-AdaptiveMention -Text '<at>przemyslaw.klys</at>' -UserPrincipalName '[email protected]'
.EXAMPLE
New-AdaptiveMention -Text 'przemyslaw.klys' -UserPrincipalName '[email protected]' -Name 'Przemysław Kłys'
.EXAMPLE
New-AdaptiveCard -Uri $URI -VerticalContentAlignment center {
New-AdaptiveTextBlock -Size ExtraLarge -Weight Bolder -Text 'Test' -Color Attention -HorizontalAlignment Center
New-AdaptiveColumnSet {
New-AdaptiveColumn {
New-AdaptiveTextBlock -Size 'Medium' -Text 'Test Card Title 1' -Color Dark
New-AdaptiveTextBlock -Size 'Medium' -Text 'Test Card Title 1' -Color Light
}
New-AdaptiveColumn {
New-AdaptiveTextBlock -Size 'Medium' -Text 'Test Card Title 1' -Color Warning
New-AdaptiveTextBlock -Size 'Medium' -Text 'Test Card Title 1' -Color Good
}
New-AdaptiveColumn {
New-AdaptiveTextBlock -Size 'Medium' -Text 'Test Card Title 1 <at>Name</at>' -Color Warning
New-AdaptiveTextBlock -Size 'Medium' -Text 'Test Card Title 1 <at>Zenon Jaskuła</at>' -Color Warning
}
}
New-AdaptiveMention -Text 'Zenon Jaskuła' -UserPrincipalName '[email protected]'
New-AdaptiveMention -Text 'Name' -UserPrincipalName '[email protected]'
} -Verbose
.NOTES
More information here: https://github.com/EvotecIT/PSTeams/issues/17
#>
[cmdletBinding()]
param(
[parameter(Mandatory)][string] $Text,
[parameter(Mandatory)][string] $UserPrincipalName,
[string] $Name
)

$Mention = [ordered] @{
"type" = "mention"
"text" = if ($Text -like "*<at>*") { $Text } else { "<at>$Text</at>" }
"mentioned" = @{
id = $UserPrincipalName
name = $Name
}
}
if ($Name) {
$Mention.mentioned.name = $Name
}
$Mention
}

0 comments on commit d1fa61f

Please sign in to comment.