-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSend-DiscordMessage.ps1
56 lines (55 loc) · 1.84 KB
/
Send-DiscordMessage.ps1
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
function Send-DiscordMessage {
[CmdletBinding(SupportsShouldProcess)]
param (
[alias('Url', 'Uri')][Uri] $WebHookUrl,
[alias('Embeds', 'Embed', 'Section')][System.Collections.IDictionary[]] $Sections,
[alias('Content', 'Message')][string] $Text,
[alias('Username')] [string] $AvatarName,
[Uri] $AvatarUrl,
[alias('TTS')][switch] $TextToSpeech,
[switch] $CreateConfig,
[string] $ConfigName,
[switch] $OutputJSON
)
if (-not $WebHookUrl) {
$WebHookUrl = Get-DiscordConfig -Name 'Primary'
}
if ($null -eq $WebHookUrl) {
Write-Warning 'Send-DiscordMessage - WebhookUrl is not set. Either provide it as parameter or initialize it with config.'
}
if ($CreateConfig) {
if (-not $ConfigName) {
$ConfigName = 'Primary'
}
Initialize-DiscordConfig -ConfigName $ConfigName -URI $WebHookUrl
}
$FullMessage = [ordered] @{
"embeds" = @()
}
if ($null -ne $Sections) {
foreach ($Section in $Sections) {
$FullMessage.embeds += $Section
}
}
if ($null -ne $Text) {
if ($TextToSpeech) {
# Applies only to Content
$FullMessage.tts = $true
}
$FullMessage.content = $Text
}
if ($null -ne $AvatarName) {
$FullMessage.username = $AvatarName
}
if ($null -ne $AvatarUrl) {
$FullMessage.avatar_url = $AvatarUrl
}
$Body = ConvertTo-Json -Depth 6 -InputObject $FullMessage
Write-Verbose -Message "Send-DiscordMessage - Body: `n$Body"
if ($PSCmdlet.ShouldProcess("$([System.Environment]::NewLine)$Body", 'Invoke-RestMethod')) {
Invoke-RestMethod -Uri $WebHookUrl -Body $Body -Method Post -ContentType "application/json" -Verbose:$false
}
if ($OutputJSON) {
return $Body
}
}