Last active
January 14, 2020 06:01
-
-
Save PatelUtkarsh/56500891d659a1cdefbfa2c42721e377 to your computer and use it in GitHub Desktop.
Send pushbullet msg on website hit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addEventListener('fetch', event => { | |
event.respondWith(handleRequest(event.request)) | |
}) | |
/** | |
* Respond to the request | |
* @param {Request} request | |
*/ | |
async function handleRequest(request) { | |
const url = new URL(request.url) | |
const msg = url.searchParams.get('msg'); | |
const title = url.searchParams.get('title'); | |
const token = 'REPLACE_ME'; | |
if (!msg || !title) { | |
return new Response('Oops!', { status: 404 }); | |
} | |
const payload = { | |
'body': msg, | |
'title': title, | |
'type': 'note', | |
}; | |
const init = { | |
method: 'POST', | |
headers: { | |
'Access-Token': token, | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify(payload), | |
}; | |
response = await fetch('https://api.pushbullet.com/v2/pushes', init) | |
console.log(JSON.stringify(response)); | |
if (response.ok) { | |
return new Response('Okay!', { status: 200 }); | |
} | |
return new Response('Oops!', { status: 400 }); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Send pushbullet msg on website hit. | |
* Author: Utkarsh Patel (github.com/patelutkarsh) | |
* | |
* Usage: push.example.com?msg=webpush&title=Awesome&secret=SecretForAuthReplaceME | |
*/ | |
// If unauthorized user kick him. | |
if ( ! isset( $_GET['secret'] ) || 'SecretForAuthReplaceME' !== $_GET['secret'] ) { | |
http_response_code( 401 ); | |
exit; | |
} | |
// Get token from pushbullet: https://www.pushbullet.com/#settings/account | |
$token = ''; | |
$ch = curl_init(); | |
$default = array( | |
'body' => '', | |
'title' => '', | |
'type' => 'note', | |
); | |
$keys = array( | |
'msg', | |
'title', | |
'type', | |
); | |
$data = array(); | |
foreach ( $keys as $key ) { | |
$value = filter_input( INPUT_GET, $key, FILTER_SANITIZE_STRING ); | |
if ( ! empty( $value ) ) { | |
$data[ $key ] = $value; | |
} | |
} | |
if ( ! array_filter( $data ) ) { | |
http_response_code( 404 ); | |
exit; | |
} | |
// Change msg to body. See https://docs.pushbullet.com/#create-push | |
if ( ! empty( $data['msg'] ) ) { | |
$data['body'] = $data['msg']; | |
unset( $data['msg'] ); | |
} | |
$data = array_merge( $default, $data ); | |
curl_setopt( $ch, CURLOPT_URL, 'https://api.pushbullet.com/v2/pushes' ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); | |
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $data ) ); | |
curl_setopt( $ch, CURLOPT_POST, 1 ); | |
$headers = array(); | |
$headers[] = "Access-Token: $token"; | |
$headers[] = 'Content-Type: application/json'; | |
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); | |
$result = curl_exec( $ch ); | |
if ( curl_errno( $ch ) ) { | |
echo 'Error:' . curl_error( $ch ); | |
http_response_code( 500 ); | |
} else { | |
echo 'Done'; | |
http_response_code( 200 ); | |
} | |
curl_close( $ch ); | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated usage.