diff --git a/KEYWORDS b/KEYWORDS index a6b13435..6afdc6b0 100644 --- a/KEYWORDS +++ b/KEYWORDS @@ -74,6 +74,7 @@ Pushjet PushMe Push Notifications Pushover +Pushplus PushSafer Pushy PushDeer diff --git a/README.md b/README.md index 6c0c39dd..724a6a73 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,7 @@ The table below identifies the services this tool supports and some example serv | [Pushed](https://github.com/caronc/apprise/wiki/Notify_pushed) | pushed:// | (TCP) 443 | pushed://appkey/appsecret/
pushed://appkey/appsecret/#ChannelAlias
pushed://appkey/appsecret/#ChannelAlias1/#ChannelAlias2/#ChannelAliasN
pushed://appkey/appsecret/@UserPushedID
pushed://appkey/appsecret/@UserPushedID1/@UserPushedID2/@UserPushedIDN | [PushMe](https://github.com/caronc/apprise/wiki/Notify_pushme) | pushme:// | (TCP) 443 | pushme://Token/ | [Pushover](https://github.com/caronc/apprise/wiki/Notify_pushover) | pover:// | (TCP) 443 | pover://user@token
pover://user@token/DEVICE
pover://user@token/DEVICE1/DEVICE2/DEVICEN
**Note**: you must specify both your user_id and token +| [Pushplus](https://github.com/caronc/apprise/wiki/Notify_pushplus) | pushplus:// | (TCP) 443 | pushplus://Token | [PushSafer](https://github.com/caronc/apprise/wiki/Notify_pushsafer) | psafer:// or psafers:// | (TCP) 80 or 443 | psafer://privatekey
psafers://privatekey/DEVICE
psafer://privatekey/DEVICE1/DEVICE2/DEVICEN | [Pushy](https://github.com/caronc/apprise/wiki/Notify_pushy) | pushy:// | (TCP) 443 | pushy://apikey/DEVICE
pushy://apikey/DEVICE1/DEVICE2/DEVICEN
pushy://apikey/TOPIC
pushy://apikey/TOPIC1/TOPIC2/TOPICN | [PushDeer](https://github.com/caronc/apprise/wiki/Notify_pushdeer) | pushdeer:// or pushdeers:// | (TCP) 80 or 443 | pushdeer://pushKey
pushdeer://hostname/pushKey
pushdeer://hostname:port/pushKey diff --git a/apprise/plugins/pushplus.py b/apprise/plugins/pushplus.py new file mode 100644 index 00000000..7a8b8757 --- /dev/null +++ b/apprise/plugins/pushplus.py @@ -0,0 +1,180 @@ +# -*- coding: utf-8 -*- +# +# BSD 2-Clause License +# +# Apprise - Push Notification Library. +# Copyright (c) 2025, Chris Caron +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +# Details at: +# https://www.pushplus.plus/doc/guide/api.html + +import re +import requests +import json + +from ..utils.parse import validate_regex +from ..url import PrivacyMode +from .base import NotifyBase +from ..locale import gettext_lazy as _ +from ..common import NotifyType + + +class NotifyPushplus(NotifyBase): + """ + A wrapper for Pushplus Notifications + """ + + # The default descriptive name associated with the Notification + service_name = _('Pushplus') + + # The services URL + service_url = 'https://www.pushplus.plus/' + + # The default secure protocol + secure_protocol = 'pushplus' + + # A URL that takes you to the setup/help of the specific protocol + setup_url = 'https://github.com/caronc/apprise/wiki/Notify_pushplus' + + # URL used to send notifications with + notify_url = 'https://www.pushplus.plus/send' + + templates = ( + '{schema}://{token}', + ) + + template_tokens = dict(NotifyBase.template_tokens, **{ + 'token': { + 'name': _('User Token'), + 'type': 'string', + 'private': True, + 'required': True, + 'regex': (r'^[a-z0-9_-]{32,64}$', 'i'), + }, + }) + + def __init__(self, token, **kwargs): + """ + Initialize Pushplus Object + """ + super().__init__(**kwargs) + + self.token = validate_regex( + token, *self.template_tokens['token']['regex'] + ) + if not self.token: + msg = 'The Pushplus token ({}) is invalid.'.format(token) + self.logger.warning(msg) + raise TypeError(msg) + + def url(self, privacy=False, *args, **kwargs): + """ + Returns the URL built dynamically based on specified arguments. + """ + params = self.url_parameters(privacy=privacy, *args, **kwargs) + return '{schema}://{token}/?{params}'.format( + schema=self.secure_protocol, + token=self.pprint(self.token, privacy, mode=PrivacyMode.Secret), + params=self.urlencode(params), + ) + + @property + def url_identifier(self): + """ + Returns a unique identifier for this plugin instance + """ + return (self.secure_protocol, self.token) + + def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs): + """ + Send a Pushplus Notification + """ + payload = { + 'token': self.token, + 'title': title if title else body, + 'content': body, + } + + headers = { + 'User-Agent': self.app_id, + 'Content-Type': 'application/json', + } + + # Always call throttle before any remote server i/o is made + self.throttle() + + try: + response = requests.post( + self.notify_url, + headers=headers, + data=json.dumps(payload), + verify=self.verify_certificate, + timeout=self.request_timeout, + ) + + if response.status_code != requests.codes.ok: + self.logger.warning( + 'Pushplus notification failed: %d - %s', + response.status_code, response.text) + return False + + except requests.RequestException as e: + self.logger.warning(f'Pushplus Exception: {e}') + return False + + self.logger.info('Pushplus notification sent successfully.') + return True + + @staticmethod + def parse_url(url): + """ + Parses the URL and returns arguments to re-instantiate the object + """ + results = NotifyBase.parse_url(url, verify_host=False) + if not results: + return results + + if 'token' in results['qsd'] and results['qsd']['token']: + results['token'] = NotifyPushplus.unquote(results['qsd']['token']) + else: + results['token'] = NotifyPushplus.unquote(results['host']) + + return results + + @staticmethod + def parse_native_url(url): + """ + Parse native Pushplus-style URL + """ + match = re.match( + r'^https://www\.pushplus\.plus/send\?token=([a-z0-9_-]+)$', + url, re.I) + if not match: + return None + + return NotifyPushplus.parse_url( + '{schema}://{token}'.format( + schema=NotifyPushplus.secure_protocol, + token=match.group(1))) diff --git a/packaging/redhat/python-apprise.spec b/packaging/redhat/python-apprise.spec index 037b32a5..fb2c8747 100644 --- a/packaging/redhat/python-apprise.spec +++ b/packaging/redhat/python-apprise.spec @@ -47,12 +47,12 @@ MacOSX, Mailgun, Mastodon, Mattermost, Matrix, MessageBird, Microsoft Windows, Microsoft Teams, Misskey, MQTT, MSG91, MyAndroid, Nexmo, Nextcloud, NextcloudTalk, Notica, Notifiarr, Notifico, ntfy, Office365, OneSignal, Opsgenie, PagerDuty, PagerTree, ParsePlatform, Plivo, PopcornNotify, Prowl, -Pushalot, PushBullet, Pushjet, PushMe, Pushover, PushSafer, Pushy, PushDeer, -Revolt, Reddit, Resend, Rocket.Chat, RSyslog, SendGrid, ServerChan, Seven, SFR, -Signal, SimplePush, Sinch, Slack, SMPP, SMSEagle, SMS Manager, SMTP2Go, -SparkPost, Splunk, Spike, Super Toasty, Streamlabs, Stride, Synology Chat, -Syslog, Techulus Push, Telegram, Threema Gateway, Twilio, Twitter, Twist, Vapid, -VictorOps, Voipms, Vonage, WebPush, WeCom Bot, WhatsApp, Webex Teams, +Pushalot, PushBullet, Pushjet, PushMe, Pushover, Pushplus, PushSafer, Pushy, +PushDeer, Revolt, Reddit, Resend, Rocket.Chat, RSyslog, SendGrid, ServerChan, +Seven, SFR, Signal, SimplePush, Sinch, Slack, SMPP, SMSEagle, SMS Manager, +SMTP2Go, SparkPost, Splunk, Spike, Super Toasty, Streamlabs, Stride, Synology +Chat, Syslog, Techulus Push, Telegram, Threema Gateway, Twilio, Twitter, Twist, +Vapid, VictorOps, Voipms, Vonage, WebPush, WeCom Bot, WhatsApp, Webex Teams, Workflows, WxPusher, XBMC} Name: python-%{pypi_name} diff --git a/test/test_plugin_pushplus.py b/test/test_plugin_pushplus.py new file mode 100644 index 00000000..65d52e31 --- /dev/null +++ b/test/test_plugin_pushplus.py @@ -0,0 +1,73 @@ +# -*- coding: utf-8 -*- +# +# BSD 2-Clause License +# +# Apprise - Push Notification Library. +# Copyright (c) 2025, Chris Caron +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import requests +from apprise.plugins.pushplus import NotifyPushplus +from helpers import AppriseURLTester + +import logging +logging.disable(logging.CRITICAL) + +apprise_url_tests = ( + ('pushplus://', { + 'instance': TypeError, + }), + ('pushplus://invalid!', { + 'instance': TypeError, + }), + ('pushplus://abc123def456ghi789jkl012mno345pq', { + 'instance': NotifyPushplus, + 'privacy_url': 'pushplus://****/', + }), + ('pushplus://?token=abc123def456ghi789jkl012mno345pq', { + 'instance': NotifyPushplus, + 'privacy_url': 'pushplus://****/', + }), + ('https://www.pushplus.plus/send?token=abc123def456ghi789jkl012mno345pq', { + 'instance': NotifyPushplus, + }), + ('pushplus://abc123def456ghi789jkl012mno345pq', { + 'instance': NotifyPushplus, + 'response': False, + 'requests_response_code': requests.codes.internal_server_error, + }), + ('pushplus://abc123def456ghi789jkl012mno345pq', { + 'instance': NotifyPushplus, + 'response': False, + 'requests_response_code': 999, + }), + ('pushplus://ffffffffffffffffffffffffffffffff', { + 'instance': NotifyPushplus, + 'test_requests_exceptions': True, + }), +) + + +def test_plugin_pushplus_urls(): + AppriseURLTester(tests=apprise_url_tests).run_all()