feat: move gotify to ntfy.sh

This commit is contained in:
Gottfried Mayer 2023-11-05 14:01:17 +01:00
parent 59cdcc0fdb
commit b88d69069f
6 changed files with 101 additions and 64 deletions

View File

@ -1,7 +1,9 @@
<?php
include_once($_SERVER['DOCUMENT_ROOT'] . '/inc/funcs.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/inc/config.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/inc/gotify.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/inc/Ntfy.php');
use ApiMayerLife\Ntfy;
$debug = true;
if (isset($_GET["c"]) && $_GET["c"] == "1") {
@ -87,12 +89,12 @@ if ($hasOutput == false && $debug) {
}
if (($debug == false) && $pushMsg !== '') {
$push = new Gotify();
$push = new Ntfy($config['ntfy-topic'], $config['ntfy-token']);
$pushMsg .= "\n\n[details](https://api.mayer.life/gogs/check_gogs_changes.php)";
if ($push->send($config["gotify-url"], $config["gotify-token"], "gogs template changed", $pushMsg)) {
if ($push->publish("gogs template changed", $pushMsg)) {
print "++OK";
} else {
print "--ERROR " . $gotify->getLastStatus();
print "--ERROR";
}
}

78
inc/Ntfy.php Normal file
View File

@ -0,0 +1,78 @@
<?php
namespace ApiMayerLife;
use Exception;
class Ntfy
{
private string $topic;
private ?string $token;
private string $serverUrl = 'https://ntfy.sh';
public function __construct(?string $topic = null, ?string $token = null, ?string $serverUrl = null)
{
// topic: set priorities: 1. $topic, 2. environment NTFY_TOPIC, 3. throw exception
if ($topic) {
$this->topic = $topic;
}
if (!$this->topic && isset($_ENV['NTFY_TOPIC'])) {
$envTopic = $_ENV['NTFY_TOPIC'];
if ($envTopic) {
$this->topic = $envTopic;
}
}
if ($this->topic === null) {
throw new Exception("Ntfy.sh topic must be set!");
}
// token: set priorities: 1. $token, 2. environment NTFY_TOKEN, 3. unset.
$this->token = $token;
if (!$this->token && isset($_ENV['NTFY_TOKEN'])) {
$envToken = $_ENV['NTFY_TOKEN'];
if ($envToken) {
$this->token = $envToken;
}
}
// url: set priorities: 1. $serverUrl parameter, 2. environment NTFY_URL, 3. default url.
if (isset($_ENV['NTFY_URL'])) {
$envUrl = $_ENV['NTFY_URL'];
if ($envUrl) {
$this->serverUrl = $envUrl;
}
}
if ($serverUrl) {
$this->serverUrl = $serverUrl;
}
}
public function publish(
string $title,
string $text,
?string $tags = null,
?string $url = null,
?string $urlTitle = null
): bool {
$headers = ["Content-Type: text/markdown"];
if ($title) {
$headers[] = "Title: $title";
}
if ($this->token) {
$headers[] = "Authorization: Bearer $this->token";
}
if ($tags) {
$headers[] = "Tags: $tags";
}
if ($url) {
$urlTitle = $urlTitle ?? "Open url";
$headers[] = "Actions: view, $urlTitle, $url";
}
$header = join("\r\n", $headers);
$ret = file_get_contents("$this->serverUrl/$this->topic", false, stream_context_create([
'http' => [
'method' => 'POST',
'header' => $header,
'content' => $text
]
]));
return $ret ? true : false;
}
}

View File

@ -27,6 +27,9 @@ $config = array(
'gotify-token' => "token-from-gotify",
'gogs-last-date' => new DateTime('2020-01-01 00:00:00'),
'ntfy-token' => "aaabbbcccc",
'ntfy-topic' => "notification",
'gotify-token-workout' => "token-from-gotify-for-workout",
'workout' => array( // workout plan. Includes description, start date, interval in days (f.E. repeats every 6 days), workout days (f.E. interval=6, workout=4 ==> 4 days workout, 2 days rest),
// repetitions: either integer or array(startDay, startReps, midDay, midReps, endDay, endReps)

View File

@ -1,51 +0,0 @@
<?php
class Gotify
{
private $laststatus;
public function getLastStatus()
{
return $this->laststatus;
}
public function send($gotifyurl, $token, $title, $message)
{
$data = [
"title" => $title,
"message" => $message,
"priority" => 5,
"extras" => [
"client::display" => [
"contentType" => "text/markdown"
]
]
];
$dataString = json_encode($data);
$url = $gotifyurl . "?token=$token";
$headers = [
"Content-Type: application/json; charset=utf-8"
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$this->laststatus = $code;
if ($code == "200") {
return true;
} else {
return false;
}
}
}

View File

@ -1,5 +1,10 @@
<?php
include('../inc/config.php');
include('../inc/Ntfy.php');
use ApiMayerLife\Ntfy;
// script runtime...
$rustart = getrusage();
@ -94,12 +99,11 @@ if ($hasChanges && !$DEBUG) {
$verboseMsg .= "$i Dateien, $noinfo ohne Beschreibung\n";
if (!$verbose && $noinfo > 0) {
include('../inc/gotify.php');
$gotify = new Gotify();
if ($gotify->send($config['gotify-url'],$config['gotify-token'], "kodi diashow", $verboseMsg)) {
$push = new Ntfy($config['ntfy-topic'], $config['ntfy-token']);
if ($push->publish("kodi diashow", $verboseMsg, "framed_picture")) {
print "ok.";
} else {
print "failed to gotify! " . $gotify->getLastStatus();
print "failed to ntfy!";
}
}
@ -149,7 +153,7 @@ function debugEcho(string $msg)
// Script runtime...
function rutime(array|false $ru, array|false $rus, string $index)
{
if($ru === false || $rus === false) {
if ($ru === false || $rus === false) {
return 0;
}
return ($ru["ru_$index.tv_sec"] * 1000 + intval($ru["ru_$index.tv_usec"] / 1000))

View File

@ -2,8 +2,9 @@
require_once('workoutdefinition.class.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/inc/config.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/inc/gotify.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/inc/Ntfy.php');
use ApiMayerLife\Ntfy;
$debug = false;
@ -50,11 +51,11 @@ if ($debug) {
echo "<pre>" . $todo . "</pre>";
} else {
if ($todo != '') {
$push = new Gotify();
if ($push->send($config["gotify-url"], $config["gotify-token-workout"], "Workout!", $todo)) {
$ntfy = new Ntfy($config["ntfy-topic"], $config["ntfy-token"]);
if ($ntfy->publish("Workout!", $todo, "cartwheeling")) {
print "ok";
} else {
print "error " . $gotify->getLastStatus();
print "error publishing on ntfy.sh";
}
} else {
print "ok";