Categories
General Tech Linux Powershell Systems Administration

Making a simple phone notification tool using Microsoft Power Automate (previously Microsoft Flow)

Note – The “Request” trigger used in this post is only available on a Premium Power Automate account. If you want this for personal use, I suggest you look at using IFTTT instead, see my guide here: https://jaytuckey.name/2020/04/21/making-a-simple-phone-notification-tool-using-ifttt-available-on-a-free-account/

Many of us have access through our work to Microsoft Power Automate, previously called Microsoft Flow. With it I’ve made a simple flow and script to call it that will notify me on an event from the command-line or anything that can call a web address. Here’s how.

Create the Flow

  • Go to Flow and make a new flow – “Automated – from blank”
  • Skip the wizard
  • In the trigger, search for “request” and select “When an HTTP request is received”
  • Under the “Request Body JSON Schema”, select “Use sample payload to generate schema”, and enter:
    {"message": "hi"}
    You should end up with this schema:
{
    "type": "object",
    "properties": {
        "message": {
            "type": "string"
        }
    }
}
  • Add a new step, and select “Notifications -> Send me a mobile notification”. Note that you can send an email notification instead using the same Flow step type.
  • In “Text” field, enter an expression:
    coalesce(triggerBody()?['message'], 'no message provided')
    Using the coalesce function means that it will either send the message provided in a POST body, or say ‘no message provided’
  • Save the Flow
  • You should now be able to expand the “Request” node and get the POST URL:

Testing with Curl

You should now be able to send a test notification something like this (replace the URL with your own, of course):

~> curl --header 'Content-Type: application/json' --data '{"message": "test notification"}' 'https://prod-20.australiasoutheast.logic.azure.com:443/myurl'

You should receive a notification on your phone. You will need the Power Automate app installed and logged in to receive the notification.

A Shell Script to send a notification (no escaping)

Note that this script uses the tools curl so it will need to be installed. This script also doesn’t do any escaping of characters that might be passed so " characters will break it easily:

#!/bin/sh

curl --header 'Content-Type: application/json' --data "{\"message\": \"$1\"}" 'https://prod-20.australiasoutheast.logic.azure.com:443/myurl'

Same script but using jq to escape input

Note that this script uses the tools curl and jq so they will need to be installed for it to work:

#!/bin/sh

curl --header 'Content-Type: application/json' --data "$(echo $1 | jq --raw-input '{"message": .}')" 'https://prod-20.australiasoutheast.logic.azure.com:443/myurl'

If called with quoted chars the jq tool will automatically escape them:

~/l/bin_scripts> echo 'hey there jay "the scripter" t' | jq --raw-input '{"message": .}'
{
  "message": "hey there jay \"the scripter\" t"
}

A Python3 Script to send a notification

This script uses only the standard library in Python3:

#!/usr/bin/python3

import json
import urllib.request
import sys

if len(sys.argv) < 2:
    print('need to provide an argument')
    exit(1)

message = ' '.join(sys.argv[1:])
url = 'https://prod-20.australiasoutheast.logic.azure.com:443/myurl'
data = json.dumps({"message": message}).encode()
req = urllib.request.Request(url)
req.add_header('Content-Type', 'application/json')

with urllib.request.urlopen(req, data) as opened_req:
    opened_req.read()  # read back all the data

A Python2 Script to send a notification

Sometimes you don’t have python3 available (eg. MacOS), so you may want to use python2 to send a notification:

#!/usr/bin/python

import json
import urllib2
import sys

if len(sys.argv) < 2:
    print('need to provide an argument')
    exit(1)

message = ' '.join(sys.argv[1:])
url = 'https://prod-20.australiasoutheast.logic.azure.com:443/myurl'
data = json.dumps({"message": message}).encode()
req = urllib2.Request(url)
req.add_header('Content-Type', 'application/json')

opened_req =  urllib2.urlopen(req, data)
opened_req.read()  # read back all the data

A Powershell Script to send a notification

param([String][Parameter(Mandatory=$true)]$Message)

$Uri = 'https://prod-20.australiasoutheast.logic.azure.com:443/myurl'

$_ = Invoke-WebRequest -UseBasicParsing -Method Post -Headers @{'Content-Type'='application/json'} -Body (ConvertTo-Json @{'message'=$Message}) -Uri $Uri

Leave a Reply

Your email address will not be published. Required fields are marked *