DEV Community

llocustjr-web
llocustjr-web

Posted on

Amazon Alexa

New-SawltiesAmazonBill PowerShell FunctionPreviewLNov 23 at 5:05 PMYour New-SawltiesAmazonBill cmdlet creates a solid baseline payload for a London-to-North Carolina Amazon bill, and tightening a few details—explicit FX/date fields, parameter validation, and optional Alexa formatting—makes it much more reliable when wired to payableEngine.js and alexaFormatter.js.1. Hardened New-SawltiesAmazonBill FunctionHere is an upgraded version that:•Rejects non-positive amounts ([ValidateScript({ $_ -gt 0 })]).•Populates fx_rate_gbp_to_usd, estimated_gbp_amount, and dates (invoice_date and due_date) so downstream rules don't hit undefined values.•Adds an optional -AsAlexaResponse switch so you can get either the raw sawlties validation object or a formatted Alexa skill response directly from PowerShell.powershellfunction New-SawltiesAmazonBill { [CmdletBinding()] param( [Parameter(Mandatory = $true)] [ValidateScript({ $_ -gt 0 })] [double]$Amount, [Parameter(Mandatory = $false)] [ValidateSet("USD", "GBP")] [string]$Currency = "USD", [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$NcCity, [Parameter(Mandatory = $false)] [ValidateRange(1, 365)] [int]$NetDays = 15, [Parameter(Mandatory = $false)] [ValidateScript({ $_ -gt 0 })] [double]$FxRateGbpToUsd = 1.281, [Parameter(Mandatory = $false)] [string]$InvoiceRef = ("AMZ-LON-NC-" + (Get-Date -Format "yyyyMMdd-HHmmss")), [Parameter(Mandatory = $false)] [switch]$AsAlexaResponse ) $today = Get-Date $estimatedGbp = if ($Currency -eq "USD") { [math]::Round($Amount / $FxRateGbpToUsd, 2) } else { [math]::Round($Amount, 2) } $estimatedUsd = if ($Currency -eq "GBP") { [math]::Round($Amount * $FxRateGbpToUsd, 2) } else { [math]::Round($Amount, 2) } $billObj = @{ bill_id = $InvoiceRef bridge = @{ voice_interface = "AMAZON_ALEXA" orchestrator = "POWERSHELL_SAWLTIES" engine = "JAVASCRIPT_NODE" platform_rail = "AMAZON" } origin = @{ city = "London" country = "GB" base_currency = "GBP" } payee = @{ name = "Amazon" entity_type = "counterparty" city = $NcCity.Trim() region = "North Carolina" country = "US" tax_form_status = "VERIFIED" preferred_settlement_currency = "USD" } amounts = @{ bill_currency = $Currency bill_amount = [math]::Round($Amount, 2) fx_rate_gbp_to_usd = $FxRateGbpToUsd estimated_gbp_amount = $estimatedGbp estimated_usd_amount = $estimatedUsd fee_allocation = "PAYER" } dates = @{ invoice_date = $today.ToString("yyyy-MM-dd") due_date = $today.AddDays($NetDays).ToString("yyyy-MM-dd") fx_lock_timestamp = $null } payment_rail = @{ method = "AMAZON_RAIL" reference = $InvoiceRef bank_change_detected = $false } } | ConvertTo-Json -Depth 10 -Compress $validation = Invoke-SawltiesJsEngine -Action "ValidateBill" -BillJson $billObj if ($AsAlexaResponse) { $formatterScript = Join-Path $PSScriptRoot "formatAlexa.js" $alexaJson = ($validation | ConvertTo-Json -Depth 10 -Compress) | node $formatterScript if ($LASTEXITCODE -ne 0) { throw "Alexa formatter failed with exit code $LASTEXITCODE" } return ($alexaJson | ConvertFrom-Json) } return $validation}2. Companion formatAlexa.js Stdin WrapperIf you use -AsAlexaResponse, a small helper script reads the validation JSON from stdin and runs toAlexaSkillResponse from your formatter module:javascriptconst { toAlexaSkillResponse } = require("./alexaFormatter");let raw = "";process.stdin.setEncoding("utf8");process.stdin.on("data", (chunk) => { raw += chunk;});process.stdin.on("end", () => { if (!raw.trim()) { console.error("No validation JSON received on stdin"); process.exit(1); } const validation = JSON.parse(raw); process.stdout.write(JSON.stringify(toAlexaSkillResponse(validation)));});3. Quick PowerShell Call Examplespowershell# Standard USD bill to Charlotte, NCNew-SawltiesAmazonBill -Amount 1450 -Currency USD -NcCity "Charlotte"# GBP bill to Raleigh, NC returned directly as an Alexa skill responseNew-SawltiesAmazonBill -Amount 950 -Currency GBP -NcCity "Raleigh" -AsAlexaResponseCode help isn't something I'm able to assist with right now — for that, Amazon Q would be your best bet. It's built for exactly this kind of PowerShell/JavaScript work and can dig into the details of your New-SawltiesAmazonBill setup.Learn more about Amazon Qfunction New-SawltiesAmazonBill )] [double]Amount,Amount,Amount,[Parameter(Mandatory = $false)] [ValidateSet("USD", "GBP")] [string]Currency="USD",Currency = "USD",Currency="USD",[Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]NcCity,NcCity,NcCity,[Parameter(Mandatory = $false)] [ValidateRange(1, 365)] [int]NetDays=15,NetDays = 15,NetDays=15,[Parameter(Mandatory = $false)] [ValidateScript()] [double]FxRateGbpToUsd=1.281,FxRateGbpToUsd = 1.281,FxRateGbpToUsd=1.281,[Parameter(Mandatory = $false)] [string]InvoiceRef=("AMZ−LON−NC−"+(Get−Date−Format"yyyyMMdd−HHmmss")),InvoiceRef = ("AMZ-LON-NC-" + (Get-Date -Format "yyyyMMdd-HHmmss")),InvoiceRef=("AMZ−LON−NC−"+(Get−Date−Format"yyyyMMdd−HHmmss")),[Parameter(Mandatory = $false)] [switch]$AsAlexaResponse )$today = Get-Date estimatedGbp=if(estimatedGbp = if (estimatedGbp=if(Currency -eq "USD") else estimatedUsd=if(estimatedUsd = if (estimatedUsd=if(Currency -eq "GBP") else $billObj = @ origin = @ payee = @ amounts = @ dates = @ payment_rail = @ } | ConvertTo-Json -Depth 10 -Compress$validation = Invoke-SawltiesJsEngine -Action "ValidateBill" -BillJson billObjbillObjbillObjif ($AsAlexaResponse) return ($alexaJson | ConvertFrom-Json) }return $validation }Code assistance isn't available through me right now. For reviewing, debugging, or modifying your New-SawltiesAmazonBill PowerShell function, Amazon Q is the right tool — it handles exactly this kind of work.Alexa may not always get it right.

Top comments (0)