Skip to content

Instantly share code, notes, and snippets.

@yyolk
Forked from richardbenson/sns-pushover.php
Created November 8, 2015 00:45
Show Gist options
  • Save yyolk/58783aa614e0bba02f16 to your computer and use it in GitHub Desktop.
Save yyolk/58783aa614e0bba02f16 to your computer and use it in GitHub Desktop.
Simple script to send SNS notifications to your smartphone via Pushover. Requires signing up for Pushover and getting an API key. Then host anywhere and add the URL as subscription to your topic.
<?php
define('PUSHOVER_API_KEY', '<API KEY>'); //Replace with your API Key
$userKey = (isset($_GET['u']) ? $_GET['u'] : '<DEFAULT USER ID>'); //Default user or group
$response = json_decode(file_get_contents('php://input'), true);
switch ($response["Type"]) {
case "SubscriptionConfirmation":
//For this use case, we will just automatically subscribe, we could forward this via email,
//urls are too long to fit in a pushover message.
$subResponse = file_get_contents($response["SubscribeURL"]);
break;
case "Notification":
//Append the topic name to the subject for clarity on source
$topicName = substr($response["TopicArn"], strrpos($response["TopicArn"], ':') + 1);
//Build the message if it's Cloudwatch JSON
$messageBody = json_decode($response["Message"], true);
if($messageBody != null) {
//There is JSON data in there, assume it's CloudWatch, adapt if you are sending other JSON data
$message = "Alarm: {$messageBody["AlarmName"]}\n".
"State: {$messageBody["OldStateValue"]} -> {$messageBody["NewStateValue"]}\n".
"Reason: {$messageBody["NewStateReason"]}";
} else {
$message = $response["Message"];
}
curl_setopt_array($ch = curl_init(), array(
CURLOPT_URL => "https://api.pushover.net/1/messages.json",
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => array(
"token" => PUSHOVER_API_KEY,
"user" => $userKey,
"message" => $message,
"title" => $response["Subject"] . " ($topicName)"
),
CURLOPT_SSL_VERIFYPEER => false
));
curl_exec($ch);
curl_close($ch);
break;
default:
echo "NO TYPE";
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment