Skip to content

Instantly share code, notes, and snippets.

@aanndryyyy
Created December 30, 2022 09:55
Show Gist options
  • Save aanndryyyy/1d07f235bd95fced5d470c3a54ce765a to your computer and use it in GitHub Desktop.
Save aanndryyyy/1d07f235bd95fced5d470c3a54ce765a to your computer and use it in GitHub Desktop.
Ntfy.sh Powershell Module
$Uri = "https://ntfy.sh"
$Topic = "powershell_ntfy"
function Send-Ntfy {
param (
[Parameter(Mandatory)] [String]
# Message title.
$Title,
[Parameter(Mandatory)] [String]
# Message body. Set to "triggered" if empty or not passed.
$Message,
[Int]
# Message priority with 1=min, 3=default and 5=max.
$Priority,
[Array]
$Tags,
[String]
$Click,
[Object]
$Actions,
[String]
$Attach,
[String]
$Email
)
$Body = New-Object PSCustomObject
$Body | Add-Member -NotePropertyName "topic" -NotePropertyValue $Topic
$Body | Add-Member -NotePropertyName "title" -NotePropertyValue $Title
$Body | Add-Member -NotePropertyName "message" -NotePropertyValue $Message
if ( $Priority ) {
$Body | Add-Member -NotePropertyName "priority" -NotePropertyValue $Priority
}
if ( $Tags ) {
$Body | Add-Member -NotePropertyName "tags" -NotePropertyValue $Tags
}
if ( $Click ) {
$Body | Add-Member -NotePropertyName "click" -NotePropertyValue $Click
}
if ( $Actions ) {
$Body | Add-Member -NotePropertyName "actions" -NotePropertyValue $Actions
}
if ( $Attach ) {
$Body | Add-Member -NotePropertyName "attach" -NotePropertyValue $Attach
}
if ( $Email ) {
$Body | Add-Member -NotePropertyName "email" -NotePropertyValue $Email
}
if ( [System.Text.Encoding]::UTF8.GetByteCount( $Message ) -gt 4096 ) {
Write-Warning -Message "Body contents too long (>4096 bytes), https://docs.ntfy.sh/publish/#limitations";
}
$Body = ConvertTo-Json $Body
Write-Debug $Body
Invoke-RestMethod -Method 'Post' -Uri $Uri -Body $Body -ContentType "application/json" -UseBasicParsing
}
Export-ModuleMember -Function Send-Ntfy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment