Giuseppe Parrello

 

Bash - How to send a Pushbullet push


In this page I provide you with script codes in order to send a Pushbullet's push. Please take note of the following requirements:

For further details about Pushbullet's API, refer to this page.


Sending a push "Note"

#!/bin/bash

token_id=""
body="My body"
title="My Title"

type="note"
json="{\"type\":\"$type\",\"body\":\"$body\",\"title\":\"$title\"}"
accesstoken=$(printf "Access-Token: %s" $token_id)

curl_return=$(/opt/bin/curl --header "$accesstoken" \
     --header 'Content-Type: application/json' \
     --data-binary "$json" \
     --request POST \
     https://api.pushbullet.com/v2/pushes)

echo $curl_return

 

Sending a push "Link"

#!/bin/bash

token_id=""
body="My body"
title="My Title"
link="http://www.google.com"

type="link"
json="{\"type\":\"$type\",\"body\":\"$body\",\"title\":\"$title\",\"url\":\"$link\"}"
accesstoken=$(printf "Access-Token: %s" $token_id)

curl_return=$(/opt/bin/curl --header "$accesstoken" \
     --header 'Content-Type: application/json' \
     --data-binary "$json" \
     --request POST \
     https://api.pushbullet.com/v2/pushes)

echo $curl_return

 

Sending a push "File"

To send a file, there are two ways. In the first way, we must have a file already available. In the second way, we have to retrieve a file from Internet.
The following script, retrieves a file already in the same folder of the script.

#!/bin/bash

token_id=""
body="My body"
title="My Title"
file_name="picture.jpg"

file_type=$(file -i -b $file_name)
json="{\"file_name\":\"$file_name\",\"file_type\":\"$file_type\"}"
accesstoken=$(printf "Access-Token: %s" $token_id)

curl_return=$(/opt/bin/curl --header "$accesstoken" \
     --header 'Content-Type: application/json' \
     --data-binary "$json" \
     --request POST \
     https://api.pushbullet.com/v2/upload-request)

file_name=$(echo $curl_return | /opt/bin/grep -oP '"(file_name)":"\K.*?(?=")')
file_url=$(echo $curl_return | /opt/bin/grep -oP '"(file_url)":"\K.*?(?=")')
file_type=$(echo $curl_return | /opt/bin/grep -oP '"(file_type)":"\K.*?(?=")')
upload_url=$(echo $curl_return | /opt/bin/grep -oP '"(upload_url)":"\K.*?(?=")')

/opt/bin/curl -i -X POST $upload_url -F file=@$file_name

type="file"
json1="{\"type\":\"$type\",\"title\":\"$title\",\"body\":\"$body\",\"file_name\":\"$file_name\",\"file_type\":\"$file_type\",\"file_url\":\"$file_url\"}"

ret_curl=$(/opt/bin/curl --header "$accesstoken" \
     --header 'Content-Type: application/json' \
     --data-binary "$json1" \
     --request POST \
     https://api.pushbullet.com/v2/pushes)

echo $ret_curl

The following script, retrieves a file from Internet.

#!/bin/bash

token_id=""
body="My body"
title="My Title"
file_name="pexels.jpg"
file_url="https://images.pexels.com/photos/1681148/pexels-photo-1681148.jpeg"
file_type="image/jpeg"

accesstoken=$(printf "Access-Token: %s" $token_id)
type="file"
json="{\"type\":\"$type\",\"title\":\"$title\",\"body\":\"$body\",\"file_name\":\"$file_name\",\"file_type\":\"$file_type\",\"file_url\":\"$file_url\"}"

curl_return=$(/opt/bin/curl --header "$accesstoken" \
     --header 'Content-Type: application/json' \
     --data-binary "$json" \
     --request POST \
     https://api.pushbullet.com/v2/pushes)
     
echo $curl_return