Pushbullet notifications on WayScript with Python

Until WayScript adds Pushbullet, you can use the snippet of code utilizing Pushbullet API in the Python module to get your notifications sent to all devices.

We made a Wayscript Forex notification sender last time to get the forex rate every day. However, I didn’t want it as an email, just a push notification as it’s merely a reminder. Wayscript doesn’t have a pushbullet module yet, but it has a Python module so you could do many things. I decided to make a simple push notifier in Python and use it in the Wayscript’s Python module. It’s not that complicated, and all you need is an Access-Token and your email address. we already posted a method to get your Access-Token from Pushbullet.

Demonstrates the push notification delivered state in Pushbullet window alongside Wayscript Pushbullet notification received after running the Wayscript Python module

The code uses the requests module to post a set of data to Pushbullet API to create a notification to your account. Your account identifier is your email address. Email address is a more straightforward method, but it sends push notification to all your devices. You can specify a device or a channel, but I wanted to make it simple for clarity. Pushbullet API documentation is excellent, so they aren’t hard to create. You can extend on the current code to use device ID or channel. I may post them later when I can.

The variables you have to edit before you begin are :accToken and :email. The access token is in your Pushbullet account settings, and email is the one you used to sign up. The title, body, and link are placeholders which you can edit or gather from other modules using WayScript variables.

#Only for push notes and links
import requests
import json

# The access token is at Pushbullet account settings. 
accToken = '__ACCESS-TOKEN__'  #replace __ACCESS-TOKEN__
email = '__email@jucktion.com__' #replace __email@jucktion.com__


# The title, body and link can be static or dynamic depending on your requirement

title = "Hello World!"
body = "A big hello to the world"
link = "https://www.jucktion.com"

#two separate header tokens, for other pushbullet requests only need a access-token header
auth = {'Access-Token': accToken}
ctype = {'Content-Type': 'application/json'}
pushes = 'https://api.pushbullet.com/v2/pushes'

#Function to push the content using requests
def pushIt(type, content):
    resp = requests.Session()
    auth.update(ctype) #add content-type header
    resp.headers.update(auth)
    if type == 'n':
        content['type'] = 'note'
    elif type == 'l':
        content['type'] = 'link'
    else:
        print('Wrong format!')
    out = resp.post(pushes, data=json.dumps(content))
    # print(out.text)

#filling in the content for the push message
content = {}
content['title'] = title
content['body'] = body
content['url'] = link
content['email'] = email

#pushIt('l', content) #for link
pushIt('n', content) #for note

 

The push function is a standard function that takes a type (string), and content (dictionary). Type is a link “l” or note “n” with quotes. The variable content (dictionary) contains the body (title,type, body, and link) of the data to be sent to the pushbullet API. The function is better left untouched if you do not know what you are doing. It could be condensed, if you have an idea would love to hear it.

View post on imgur.com

Here’s a recording of it running, Let us know what you think about it in the comments.

2 Comments

Leave a Reply