2018-02-08

Yo

Sometimes you just want to set something and forget it. Other times, you want to set something and get a notification when it finishes. There are a ton of different notification mechanisms that have been devised over the years, from email to pagers to texts to tweets.

And those are all fine, but they can be overkill. You may not even have a machine with access to a mail server if you are draconian about how you security partition your mail relays on your network. So I started playing with Yo.

Yo is a minimalist social media platform. It's Twitter without the tweets. It's a binary notification mechanism: you either get a "yo" or you don't. The content, and therefore the meaning, of the yo is left to your interpretation. This is perfect for our needs.

The Yo API is trivial and can be triggered with curl or any HTTP POST method-capable tool of your choice. On Windows, I like to use Powershell. If I have something I'm running on Windows and I just want to get a yo when it's done, I can do so with a Yo account, the API key, and some scripting:

# Convert Yo API key to a secure string object and save it to disk
$yo_file_name   = [System.IO.Path]::Combine(${Env:UserProfile}, 'Desktop', 'yo_key.xml')
$yo_target      = 'MY_YO_ACCOUNT'

$sec_str        = ConvertTo-SecureString -AsPlainText -Force -String 'YO_API_KEY_HERE'
Export-CliXml -InputObject $sec_str -Force -Encoding 'UTF8' -Path $yo_file_name

$yo_key_sec_str = Import-CliXml -Path (Get-Item -Path $yo_file_name).FullName
$yo_key_sec_ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($yo_key_sec_str)
$yo_key         = [System.Runtime.InteropServices.Marshal]::PtrToSTringBSTR($yo_key_sec_ptr)

$yo_send_properties = @{
  'Body'   = "api_token={0}&username={1}" -f ($yo_key, $yo_target);
  'Method' = 'POST';
  'Uri'    = 'http://api.justyo.co/yo/';
}
Clear-Variable -Name yo_key
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($yo_key_sec_ptr)

$err = $null
Do-Something -ErrorVariable err
if ([String]::IsNullOrEmpty($err)) {
  $yo_send_properties.Body += '&link=ok' # including a link is optional
} else {
  $yo_send_properties.Body += '&link=fail'
}
Invoke-RestMethod @yo_send_properties
Clear-Variable -Name yo_send_properties

Including a link (well, a "link") along with the yo isn't necessary — and potentially very un-yolike — but it lets me know if the thing I'm letting run succeeded or not. If I only want to know it's finished and I don't care about the result, or if there is no result to know, I could simply do the thing and then send the yo:

Do-Something
Invoke-RestMethod @yo_send_properties

If I want to run a program, it might look like this:

$proc = Start-Process -FilePath 'myprogram.exe' -ArgumentList $program_args -PassThru
$proc.WaitForExit()
Invoke-RestMethod @yo_send_properties

Yo is threatening to go offline soon if you don't join their Patreon, so this advice may be short-lived.

No comments: