IFTTT_Maker_PowerShell.psm1

Set-StrictMode -Version Latest
<#
    .SYNOPSIS
        Triggers an event on the IFTTT Maker Channel.
    .DESCRIPTION
        Send-Maker triggers an event on the IFTTT Maker Channel.
        The event can have up to 3 values that can be passed.
    .PARAMETER EventName
        The name of the event to trigger on the IFTTT Maker Channel.
    .PARAMETER Key
        The secret key you got from IFTTT for triggering events on the Maker Channel.
    .PARAMETER Value1
        First value passed to the event (ingredient Value1).
    .PARAMETER Value2
        Second value passed to the event (ingredient Value2).
    .PARAMETER Value3
        Third value passed to the event (ingredient Value3).
    .PARAMETER Save
        Defines if the current config should be saved.
#>

function Send-IFTTTMaker
{
    [CmdletBinding()]
    param
    (
        [ValidateNotNullOrEmpty()]
        [Parameter(Position=0,HelpMessage="The secret key you got from IFTTT for triggering events on the Maker Channel.")]
        [string] $Key,

        [ValidateNotNullOrEmpty()]
        [Parameter(Position=1,HelpMessage="The name of the event to trigger on the IFTTT Maker Channel")]
        [string] $EventName,
        
        [ValidateNotNullOrEmpty()]
        [Parameter(Position=2,ValueFromPipeline=$true,HelpMessage="First value passed to the event (ingredient Value1).")]
        [string] $Value1,
        
        [ValidateNotNullOrEmpty()]
        [Parameter(Position=3,HelpMessage="Second value passed to the event (ingredient Value2).")]
        [string] $Value2,
        
        [ValidateNotNullOrEmpty()]
        [Parameter(Position=4,HelpMessage="Third value passed to the event (ingredient Value3).")]
        [string] $Value3,

        [Parameter(HelpMessage="Defines if the current config should be saved.")]
        [bool]$Save = $true
    )

    #region Load Config
    $SaveFolder ="$env:APPDATA\Send-IFTTTMaker\"
    New-Item -ItemType Directory -Force -Path $SaveFolder
    $SaveFile ="$SaveFolder\info.data"
    If (Test-Path $SaveFile)
    {
        Write-Verbose "Attempting to load config file..."
        #Get-Content $SaveFile
        $Converted = Get-Content $SaveFile | ConvertFrom-Json  
        if($Key -eq "" -and $Converted.Key -ne "")                 { $Key =$Converted.Key}
        if($EventName -eq "" -and $Converted.EventName -ne "")     { $EventName =$Converted.EventName}
        if($Converted.value1 -eq "" -and $Converted.value1 -ne "") { $Value1 =$Converted.value1}
        if($Converted.value2 -eq "" -and $Converted.value2 -ne "") { $Value2 =$Converted.value2}
        if($Converted.value3 -eq "" -and $Converted.value3 -ne "") { $Value3 =$Converted.value3}
        Write-Verbose $Converted
    }
    #endregion

    $Site="https://maker.ifttt.com/trigger/"
    [uri]$MyURL = "$Site$EventName/with/key/$Key"
    
    $BodyDic= @{}
    if($Value1)
    {   $BodyDic.value1=$Value1
        if($Value2)
        {   $BodyDic.value2=$Value2
            if($Value3)
            {    $BodyDic.value3=$Value3}}}
    
    $BodyJson = ConvertTo-Json $BodyDic
    Write-Verbose $BodyJson

    #region Save Config
    $XMLExport = @{}
    $XMLExport.Key = $Key
    $XMLExport.EventName = $EventName
    $XMLExport.value1= $Value1
    $XMLExport.value2= $Value2
    $XMLExport.value3= $Value3
    
    if(-not($Key)) { Throw �You must supply a value for -Key� }
    if(-not($EventName)) { Throw �You must supply a value for -EventName� }
    if(-not($Site)) { Throw �You must supply a value for -Site� }
    
    if($Save == $true)
    { 
         Write-Verbose "Saving config file..."
         $XMLExport | ConvertTo-Json | Set-Content $SaveFile
    }

    #endregion

    try {
            Write-Verbose "Sending Web Request"
            Invoke-WebRequest -Uri $MyURL -Method Post -ContentType "application/json" -Body $BodyJson 
        }
    catch [System.Net.WebException]
        {
            Write-Verbose "ERROR:"
            Write-Verbose $_
            $err= ConvertFrom-Json -InputObject $_.ErrorDetails 
            $err.errors.message #| Get-Member
        }
}