Skip to content

Wallos: Open-source, self-hostable personal subscription tracker. Visualize your recurring expenses, manage your budget, and save money.

pushplus是在中国一个可玩性很高的专用通知服务,特别是可以直接推送到微信这个使用人数最多的平台 (#911)

xwdjadd646b5a3aa
diff --git a/endpoints/cronjobs/sendnotifications.php b/endpoints/cronjobs/sendnotifications.php
index 8970b49..7d15805 100644
--- a/endpoints/cronjobs/sendnotifications.php
+++ b/endpoints/cronjobs/sendnotifications.php
@@ -131,7 +131,16 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {
$telegram['botToken'] = $row["bot_token"];
$telegram['chatId'] = $row["chat_id"];
}
+ // Check if PushPlus notifications are enabled and get the settings
+ $query = "SELECT * FROM pushplus_notifications WHERE user_id = :userId";
+ $stmt = $db->prepare($query);
+ $stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
+ $result = $stmt->execute();
+ if ($row = $result->fetchArray(SQLITE3_ASSOC)) {
+ $pushplusNotificationsEnabled = $row['enabled'];
+ $pushplus['token'] = $row["token"];
+ }
// Check if Pushover notifications are enabled and get the settings
$query = "SELECT * FROM pushover_notifications WHERE user_id = :userId";
$stmt = $db->prepare($query);
@@ -174,7 +183,7 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {
}
$notificationsEnabled = $emailNotificationsEnabled || $gotifyNotificationsEnabled || $telegramNotificationsEnabled ||
- $webhookNotificationsEnabled || $pushoverNotificationsEnabled || $discordNotificationsEnabled ||
+ $webhookNotificationsEnabled || $pushoverNotificationsEnabled || $discordNotificationsEnabled ||$pushplusNotificationsEnabled||
$ntfyNotificationsEnabled;
// If no notifications are enabled, no need to run
@@ -497,6 +506,69 @@ while ($userToNotify = $usersToNotify->fetchArray(SQLITE3_ASSOC)) {
}
}
+
+ // PushPlus notifications if enabled
+ if ($pushplusNotificationsEnabled) {
+ foreach ($notify as $userId => $perUser) {
+ // Get name of user from household table
+ $stmt = $db->prepare('SELECT * FROM household WHERE id = :userId');
+ $stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
+ $result = $stmt->execute();
+ $user = $result->fetchArray(SQLITE3_ASSOC);
+
+ // 构建消息内容
+ $messageContent = "";
+ if ($user['name']) {
+ $messageContent = $user['name'] . ", the following subscriptions are up for renewal:\n";
+ } else {
+ $messageContent = "The following subscriptions are up for renewal:\n";
+ }
+
+ foreach ($perUser as $subscription) {
+ $dayText = getDaysText($subscription['days']);
+ $messageContent .= $subscription['name'] . " for " . $subscription['formatted_price'] . " (" . $dayText . ")\n";
+ }
+
+ // 准备 PushPlus 数据
+ $data = array(
+ 'token' => $pushplus['token'],
+ 'title' => '订阅续期提醒 - Wallos',
+ 'content' => mb_convert_encoding($messageContent, 'UTF-8', 'auto'),
+ 'template' => 'json'
+ );
+
+ $data_string = json_encode($data);
+
+ $ch = curl_init('https://www.pushplus.plus/send');
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt(
+ $ch,
+ CURLOPT_HTTPHEADER,
+ array(
+ 'Content-Type: application/json'
+ ),
+ );
+
+ $result = curl_exec($ch);
+ $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+
+ if ($result === false) {
+ echo "Error sending PushPlus notifications: " . curl_error($ch) . "<br />";
+ } else {
+ $resultData = json_decode($result, true);
+ if (isset($resultData['code']) && $resultData['code'] == 200) {
+ echo "PushPlus Notifications sent successfully<br />";
+ } else {
+ $errorMsg = isset($resultData['msg']) ? $resultData['msg'] : 'Unknown error';
+ echo "PushPlus API error: " . $errorMsg . "<br />";
+ }
+ }
+ curl_close($ch);
+ }
+ }
+
// Pushover notifications if enabled
if ($pushoverNotificationsEnabled) {
foreach ($notify as $userId => $perUser) {
diff --git a/endpoints/notifications/savepushplusnotifications.php b/endpoints/notifications/savepushplusnotifications.php
new file mode 100644
index 0000000..d12e16b
--- /dev/null
+++ b/endpoints/notifications/savepushplusnotifications.php
@@ -0,0 +1,68 @@
+<?php
+require_once '../../includes/connect_endpoint.php';
+
+if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
+ die(json_encode([
+ "success" => false,
+ "message" => translate('session_expired', $i18n)
+ ]));
+}
+
+if ($_SERVER["REQUEST_METHOD"] === "POST") {
+ $postData = file_get_contents("php://input");
+ $data = json_decode($postData, true);
+
+ if (!isset($data["token"]) || $data["token"] == "") {
+ $response = [
+ "success" => false,
+ "message" => translate('fill_mandatory_fields', $i18n)
+ ];
+ echo json_encode($response);
+ } else {
+ $enabled = $data["enabled"];
+ $token = $data["token"];
+
+ $query = "SELECT COUNT(*) FROM pushplus_notifications WHERE user_id = :userId";
+ $stmt = $db->prepare($query);
+ $stmt->bindParam(":userId", $userId, SQLITE3_INTEGER);
+ $result = $stmt->execute();
+
+ if ($result === false) {
+ $response = [
+ "success" => false,
+ "message" => translate('error_saving_notifications', $i18n)
+ ];
+ echo json_encode($response);
+ } else {
+ $row = $result->fetchArray();
+ $count = $row[0];
+ if ($count == 0) {
+ $query = "INSERT INTO pushplus_notifications (enabled, token, user_id)
+ VALUES (:enabled, :token, :userId)";
+ } else {
+ $query = "UPDATE pushplus_notifications
+ SET enabled = :enabled, token = :token WHERE user_id = :userId";
+ }
+
+ $stmt = $db->prepare($query);
+ $stmt->bindValue(':enabled', $enabled, SQLITE3_INTEGER);
+ $stmt->bindValue(':token', $token, SQLITE3_TEXT);
+ $stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
+
+ if ($stmt->execute()) {
+ $response = [
+ "success" => true,
+ "message" => translate('notifications_settings_saved', $i18n)
+ ];
+ echo json_encode($response);
+ } else {
+ $response = [
+ "success" => false,
+ "message" => translate('error_saving_notifications', $i18n)
+ ];
+ echo json_encode($response);
+ }
+ }
+ }
+}
+?>
\ No newline at end of file
diff --git a/endpoints/notifications/testpushplusnotifications.php b/endpoints/notifications/testpushplusnotifications.php
new file mode 100644
index 0000000..a0fe8eb
--- /dev/null
+++ b/endpoints/notifications/testpushplusnotifications.php
@@ -0,0 +1,85 @@
+<?php
+require_once '../../includes/connect_endpoint.php';
+
+if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
+ die(json_encode([
+ "success" => false,
+ "message" => translate('session_expired', $i18n)
+ ]));
+}
+
+if ($_SERVER["REQUEST_METHOD"] === "POST") {
+ $postData = file_get_contents("php://input");
+ $data = json_decode($postData, true);
+
+ if (!isset($data["token"]) || $data["token"] == "") {
+ $response = [
+ "success" => false,
+ "message" => translate('fill_mandatory_fields', $i18n)
+ ];
+ echo json_encode($response);
+ } else {
+ // Set the message parameters
+ $title = translate('wallos_notification', $i18n);
+ $message = translate('test_notification', $i18n);
+
+ $token = $data["token"];
+
+ $ch = curl_init();
+
+ // Set the URL and other options for PushPlus
+ $postData = [
+ "token" => $token,
+ "title" => "您的订阅到期拉",
+ "content" => $message,
+ "template" => "json"
+ ];
+
+ curl_setopt_array($ch, [
+ CURLOPT_URL => 'https://www.pushplus.plus/send',
+ CURLOPT_RETURNTRANSFER => true,
+ CURLOPT_POST => true,
+ CURLOPT_POSTFIELDS => json_encode($postData),
+ CURLOPT_HTTPHEADER => [
+ 'Content-Type: application/json'
+ ],
+ CURLOPT_TIMEOUT => 10
+ ]);
+
+ // Execute the request
+ $response = curl_exec($ch);
+ $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+ $curlError = curl_error($ch);
+
+ // Close the cURL session
+ curl_close($ch);
+
+ // Check if the message was sent successfully
+ if ($response === false) {
+ die(json_encode([
+ "success" => false,
+ "message" => translate('notification_failed', $i18n) . ": " . $curlError
+ ]));
+ } else {
+ $responseData = json_decode($response, true);
+ if (isset($responseData['code']) && $responseData['code'] == 200) {
+ die(json_encode([
+ "success" => true,
+ "message" => translate('notification_sent_successfuly', $i18n)
+ ]));
+ } else {
+ $errorMsg = isset($responseData['msg']) ? $responseData['msg'] : translate('notification_failed', $i18n);
+ die(json_encode([
+ "success" => false,
+ "message" => $errorMsg
+ ]));
+ }
+ }
+ }
+} else {
+ die(json_encode([
+ "success" => false,
+ "message" => translate("invalid_request_method", $i18n)
+ ]));
+}
+?>
\ No newline at end of file
diff --git a/includes/i18n/zh_cn.php b/includes/i18n/zh_cn.php
index 50bb724..307feb8 100644
--- a/includes/i18n/zh_cn.php
+++ b/includes/i18n/zh_cn.php
@@ -202,6 +202,8 @@ $i18n = [
"telegram" => "Telegram",
"telegram_bot_token" => "Telegram 机器人令牌",
"telegram_chat_id" => "Telegram 聊天 ID",
+ "pushplus" => "Pushplus",
+ "pushplus_token" => "消息令牌或者是用户令牌",
"webhook" => "Webhook",
"webhook_url" => "Webhook URL",
"request_method" => "请求方法",
diff --git a/scripts/notifications.js b/scripts/notifications.js
index ec16edc..bd4b87f 100644
--- a/scripts/notifications.js
+++ b/scripts/notifications.js
@@ -186,6 +186,36 @@ function testNotificationsTelegramButton() {
makeFetchCall('endpoints/notifications/testtelegramnotifications.php', data, button);
}
+function testNotificationsPushPlusButton() {
+ const button = document.getElementById("testNotificationsPushPlus");
+ button.disabled = true;
+
+ const enabled = document.getElementById("pushplusenabled").checked ? 1 : 0;
+ const token = document.getElementById("pushplustoken").value;
+
+ const data = {
+ enabled: enabled,
+ token: token
+ };
+
+ makeFetchCall('endpoints/notifications/testpushplusnotifications.php', data, button);
+}
+
+function saveNotificationsPushPlusButton() {
+ const button = document.getElementById("saveNotificationsPushPlus");
+ button.disabled = true;
+
+ const enabled = document.getElementById("pushplusenabled").checked ? 1 : 0;
+ const token = document.getElementById("pushplustoken").value;
+
+ const data = {
+ enabled: enabled,
+ token: token
+ };
+
+ makeFetchCall('endpoints/notifications/savepushplusnotifications.php', data, button);
+}
+
function saveNotificationsGotifyButton() {
const button = document.getElementById("saveNotificationsGotify");
button.disabled = true;
@@ -205,6 +235,7 @@ function saveNotificationsGotifyButton() {
makeFetchCall('endpoints/notifications/savegotifynotifications.php', data, button);
}
+
function testNotificationsGotifyButton() {
const button = document.getElementById("testNotificationsGotify");
button.disabled = true;
diff --git a/settings.php b/settings.php
index 06f19cd..c59ac9f 100644
--- a/settings.php
+++ b/settings.php
@@ -221,6 +221,25 @@ $userData['currency_symbol'] = $currencies[$main_currency]['symbol'];
$notificationsTelegram['chat_id'] = "";
}
+
+ // PushPlus notifications
+$sql = "SELECT * FROM pushplus_notifications WHERE user_id = :userId LIMIT 1";
+$stmt = $db->prepare($sql);
+$stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);
+$result = $stmt->execute();
+
+$rowCount = 0;
+while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
+ $notificationsPushPlus['enabled'] = $row['enabled'];
+ $notificationsPushPlus['token'] = $row['token'];
+ $rowCount++;
+}
+
+if ($rowCount == 0) {
+ $notificationsPushPlus['enabled'] = 0;
+ $notificationsPushPlus['token'] = "";
+}
+
// Ntfy notifications
$sql = "SELECT * FROM ntfy_notifications WHERE user_id = :userId LIMIT 1";
$stmt = $db->prepare($sql);
@@ -549,6 +568,35 @@ $userData['currency_symbol'] = $currencies[$main_currency]['symbol'];
</div>
</div>
</section>
+
+ <section class="account-notifications-section">
+ <header class="account-notification-section-header" onclick="openNotificationsSettings('pushplus');">
+ <h3>
+ <i class="fa-solid fa-bell"></i>
+ <?= translate('pushplus', $i18n) ?>
+ </h3>
+ </header>
+ <div class="account-notification-section-settings" data-type="pushplus">
+ <div class="form-group-inline">
+ <input type="checkbox" id="pushplusenabled" name="pushplusenabled"
+ <?= $notificationsPushPlus['enabled'] ? "checked" : "" ?>>
+ <label for="pushplusenabled" class="capitalize"><?= translate('enabled', $i18n) ?></label>
+ </div>
+ <div class="form-group-inline">
+ <input type="text" name="pushplustoken" id="pushplustoken"
+ placeholder="<?= translate('pushplus_token', $i18n) ?>"
+ value="<?= $notificationsPushPlus['token'] ? $notificationsPushPlus['token'] : '' ?>" />
+ </div>
+ <div class="buttons">
+ <input type="button" class="secondary-button thin mobile-grow"
+ value="<?= translate('test', $i18n) ?>" id="testNotificationsPushPlus"
+ onClick="testNotificationsPushPlusButton()" />
+ <input type="submit" class="thin mobile-grow" value="<?= translate('save', $i18n) ?>"
+ id="saveNotificationsPushPlus" onClick="saveNotificationsPushPlusButton()" />
+ </div>
+ </div>
+</section>
+
<section class="account-notifications-section">
<header class="account-notification-section-header" onclick="openNotificationsSettings('ntfy');">
<h3>