Getting real time alerts from server to phone with Pushbullet
So I wanted to receive alerts from the long-running scripts on my Raspberry Pi. I used to have postfix to send an email but sometimes it took hours to notice the mail.
I already had Pushbullet installed on Android, which is a service that supports notification/file/link sharing between your devices.
Turns out with a little scripting we can make it send error reports/alerts to the phone. The only thing we need is an Access Token.
Then use a simple shell script in PATH with a POST request and call it to send alerts when required.
#!/bin/sh
curl -u ACCESS_TOKEN_HERE: -X POST https://api.pushbullet.com/v2/pushes --header 'Content-Type: application/json' --data-binary '{"type":"note", "title": "Shell", "body": "'"$1"'"}' > /dev/null 2>&1
Following Python library does the same
import os, inspect
class Pypush:
"""A simple wrapper around Pushbullet"""
def __init__(self, key):
self.key = key
self.title = inspect.stack()[1][1]
def post(self, msg, title=None):
title = title or self.title
os.system('curl -u %s: -X POST https://api.pushbullet.com/v2/pushes --header \'Content-Type: application/json\' --data-binary \'{"type":"note", "title": "%s", "body": "%s"}\'' % (self.key, title, msg))
def warn(self, msg, title=None):
self.post('Warning: %s' % msg, title)
def notify(self, msg, title=None):
self.post('Notification: %s' % msg, title)
def error(self, msg, title=None):
self.post('Error: %s' % msg, title)