Allow multiple Pushover users

This commit is contained in:
darcy (iris system) 2021-09-02 12:47:15 +12:00
parent bb7cf80704
commit b8374a27d9
2 changed files with 40 additions and 36 deletions

View File

@ -8,46 +8,50 @@ use Serhiy\Pushover\Api\Message\Message as PushoverMessage;
use Serhiy\Pushover\Api\Message\Notification as PushoverNotification; use Serhiy\Pushover\Api\Message\Notification as PushoverNotification;
final class ApplicationAlerters { final class ApplicationAlerters {
/** /**
* @param string[] $key Hook key (unused) * @param string[] $key Hook key (unused)
* @param mixed[] $params Array of [user, tilde, message] * @param mixed[] $params Array of [user, tilde, message]
* @return mixed[] Array of [user, tilde, message] * @return mixed[] Array of [user, tilde, message]
*/ */
public static function pushover(array $key, array $params): array { public static function pushover(array $key, array $params): array {
list($user, $tilde, $message) = $params; list($user, $tilde, $message) = $params;
$application = new Pushover\Application($_ENV[IX_ENVBASE . '_PUSHOVER_API_TOKEN']); $application = new Pushover\Application($_ENV[IX_ENVBASE . '_PUSHOVER_API_TOKEN']);
$recipient = new Pushover\Recipient($_ENV[IX_ENVBASE . '_PUSHOVER_USER_KEY']); $pushovermessage = new PushoverMessage(
$pushovermessage = new PushoverMessage( "from {$user}@{$tilde}\n> {$message}",
"from {$user}@{$tilde}\n> {$message}", 'new neotel application',
'new neotel application', );
);
$notification = new PushoverNotification($application, $recipient, $pushovermessage);
$response = $notification->push();
return [$user, $tilde, $message]; $pushoverusers = explode(',', $_ENV[IX_ENVBASE . '_PUSHOVER_USER_KEYS']);
} foreach ($pushoverusers as $userkey) {
$recipient = new Pushover\Recipient($userkey);
$notification = new PushoverNotification($application, $recipient, $pushovermessage);
$response = $notification->push();
}
/** return [$user, $tilde, $message];
* @param string[] $key Hook key (unused) }
* @param mixed[] $params Array of [user, tilde, message]
* @return mixed[] Array of [user, tilde, message]
*/
public static function discord(array $key, array $params): array {
list($user, $tilde, $message) = $params;
$data = json_encode([ /**
"content" => "From `{$user}@{$tilde}` \n> {$message}", * @param string[] $key Hook key (unused)
]); * @param mixed[] $params Array of [user, tilde, message]
* @return mixed[] Array of [user, tilde, message]
*/
public static function discord(array $key, array $params): array {
list($user, $tilde, $message) = $params;
$curl_opts = [ $data = json_encode([
CURLOPT_HTTPHEADER => ["Content-Type: application/json"], "content" => "From `{$user}@{$tilde}` \n> {$message}",
CURLOPT_POST => 1, ]);
CURLOPT_POSTFIELDS => $data,
];
$response = CurlHelpers::fetchUrl($_ENV[IX_ENVBASE . "_DISCORD_WEBHOOK_URL"], $curl_opts, true); $curl_opts = [
CURLOPT_HTTPHEADER => ["Content-Type: application/json"],
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $data,
];
return [$user, $tilde, $message]; $response = CurlHelpers::fetchUrl($_ENV[IX_ENVBASE . "_DISCORD_WEBHOOK_URL"], $curl_opts, true);
}
return [$user, $tilde, $message];
}
} }

View File

@ -44,7 +44,7 @@ class Configuration {
// Pushover application and user keys // Pushover application and user keys
$dotenv->required(IX_ENVBASE . '_PUSHOVER_API_TOKEN')->notEmpty(); $dotenv->required(IX_ENVBASE . '_PUSHOVER_API_TOKEN')->notEmpty();
$dotenv->required(IX_ENVBASE . '_PUSHOVER_USER_KEY')->notEmpty(); $dotenv->required(IX_ENVBASE . '_PUSHOVER_USER_KEYS')->notEmpty();
// Discord webhook URL // Discord webhook URL
$dotenv->required(IX_ENVBASE . '_DISCORD_WEBHOOK_URL')->notEmpty(); $dotenv->required(IX_ENVBASE . '_DISCORD_WEBHOOK_URL')->notEmpty();