Functions.psm1


function Send-PhoneCall {
      <#
        .SYNOPSIS
         Sends a message request to Ntfy which will in turn invokes a voice call to the number you specifi.
 
        .DESCRIPTION
        You will need the details from a valid Ntfy paid account with the Phone number already verified.
 
        .PARAMETER Topic
        This is the Topic being used to submit and list the message sent.
        Please ensure you specify the full path, e.g. https://ntfy.sh/topic
 
        .PARAMETER APIKey
         This is the API Key being used to Authorise posts with the Ntfy Account which owns the Topic.
         If you own the topic, you can set the access options here https://ntfy.sh/account
 
        .PARAMETER PhoneNumber
        This is the Phone Number which will receive the message you are sending, ensure the international format +44.... is used.
        This number must be already registered with the Ntfy Topic Account.
 
        .PARAMETER Message
        The string message you're sending the the Topic and intended Phone recipent.
 
        .INPUTS
        All strings for all parameters
 
        .OUTPUTS
        Returns 0 if successful, -1 if not. Will also display an error message. You may also ver Verbose to view extra execution details.
 
        .EXAMPLE
        PS> Send-PhoneCall -Topic "https://ntfy.sh/topic" -APIKey "rfdsdfrtgytgf-dsxcf" -PhoneNumber "+447970499522" -Message "Hello World!"
         
        .LINK
        Online version: https://www.powershellgallery.com/profiles/GarryGBain
 
        #>

    [Cmdletbinding()] 
    param( 
        [Parameter(Mandatory=$true, Position=0, HelpMessage="The Server and Topic; http://ntfy.sh/thetopic")] $Topic,
        [Parameter(Mandatory=$true, Position=1, HelpMessage="The API Key you create https://ntfy.sh/account" )] $APIKey,
        [Parameter(Mandatory=$true, Position=2, HelpMessage="This will only work with registered numbers. https://ntfy.sh/account")] $PhoneNumber,
        [Parameter(Mandatory=$true, Position=3, HelpMessage="The message you wish to send.")] $Message
        ) 
        
        $Return=0

        Write-Verbose "Setting up request"

        $Request = @{
            Method = "POST"
            URI = $Topic
            Headers = @{
              Authorization = "Bearer $APIKey"
              Call = $PhoneNumber
            }
            Body = $Message
          }
          Write-Verbose "Sending request"

          try {
              Invoke-RestMethod @Request
          }
          catch {
            $Return=-1
            Write-Error "Error occurred: $_"
          }
          
          return $Return

} Export-ModuleMember Send-PhoneCall



function Send-Message {
  <#
    .SYNOPSIS
     Sends a message request to a Ntfy Topic.
 
    .DESCRIPTION
    You will need the details from a valid Ntfy paid account with the Phone number already verified.
 
    .PARAMETER Topic
    This is the Topic being used to submit and list the message sent.
    Please ensure you specify the full path, e.g. https://ntfy.sh/topic
 
    .PARAMETER Message
    The string message you're sending the the Topic.
 
    .PARAMETER APIKey
    This is the API Key being used to Authorise posts with the Ntfy Account which owns the Topic.
    If you own the topic, you can set the access options here https://ntfy.sh/account
 
    .INPUTS
    All strings for all parameters
 
    .OUTPUTS
    Returns 0 if successful, -1 if not. Will also display an error message. You may also ver Verbose to view extra execution details.
 
    .EXAMPLE
    PS> Send-Message -Topic "https://ntfy.sh/topic" -Message "Hello World!"
     
    .LINK
    Online version: https://www.powershellgallery.com/profiles/GarryGBain
 
    #>

[Cmdletbinding()] 
param( 
    [Parameter(Mandatory=$true, Position=0, HelpMessage="The Server and Topic; http://ntfy.sh/thetopic")] $Topic,
    [Parameter(Mandatory=$true, Position=1, HelpMessage="The message you wish to send.")] $Message,
    [Parameter(Mandatory=$false, Position=2, HelpMessage="Used to authorise your posts, if the Topic is restricted" )] $APIKey
    ) 
    
    $Return=0

    Write-Verbose "Setting up request"

    if( $APIKey -ne "" ){
        $Headers = @{
          Authorization = "Bearer $APIKey"
        }
      }

    $Request = @{
      Method = "POST"
      URI = $Topic
      Body = $Message
      }
      Write-Verbose "Sending request"

      try {
          Invoke-RestMethod @Request -Headers $Headers
      }
      catch {
        $Return=-1
        Write-Error "Error occurred: $_"
      }
      
      return $Return

} Export-ModuleMember Send-Message