Skip to content

Commit

Permalink
improved voipms:// phone parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
caronc committed Oct 27, 2024
1 parent 78f16ad commit dbb0841
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions apprise/plugins/voipms.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

class NotifyVoipms(NotifyBase):
"""
A wrapper for Voipms Notifications
A wrapper for VoIPms Notifications
"""

# The default descriptive name associated with the Notification
Expand All @@ -62,12 +62,15 @@ class NotifyVoipms(NotifyBase):
# A URL that takes you to the setup/help of the specific protocol
setup_url = 'https://github.com/caronc/apprise/wiki/Notify_voipms'

# Voipms uses the http protocol with JSON requests
# VoIPms uses the http protocol with JSON requests
notify_url = 'https://voip.ms/api/v1/rest.php'

# The maximum length of the body
body_maxlen = 160

# The supported country code by VoIP.ms
voip_ms_country_code = '1'

# A title can not be used for SMS Messages. Setting this to zero will
# cause any title (if defined) to get placed into the message body.
title_maxlen = 0
Expand Down Expand Up @@ -122,12 +125,11 @@ class NotifyVoipms(NotifyBase):

def __init__(self, email, source=None, targets=None, **kwargs):
"""
Initialize Voipms Object
Initialize VoIPms Object
"""
super().__init__(**kwargs)

# Validate our params here.

if self.password is None:
msg = 'Password has to be specified.'
self.logger.warning(msg)
Expand All @@ -136,7 +138,7 @@ def __init__(self, email, source=None, targets=None, **kwargs):
# User is the email associated with the account
result = is_email(email)
if not result:
msg = 'An invalid Voipms user email: ' \
msg = 'An invalid VoIPms user email: ' \
'({}) was specified.'.format(email)
self.logger.warning(msg)
raise TypeError(msg)
Expand All @@ -145,15 +147,16 @@ def __init__(self, email, source=None, targets=None, **kwargs):
# Validate our source Phone #
result = is_phone_no(source)
if not result:
msg = 'An invalid Voipms source phone # ' \
msg = 'An invalid VoIPms source phone # ' \
'({}) was specified.'.format(source)
self.logger.warning(msg)
raise TypeError(msg)

# Source Phone # only supports +1 country code
# Allow 7 digit phones (presume they're local with +1 country code)
if result['country'] and result['country'] != '1':
msg = 'Voipms only supports +1 country code ' \
if result['country'] \
and result['country'] != self.voip_ms_country_code:
msg = 'VoIPms only supports +1 country code ' \
'({}) was specified.'.format(source)
self.logger.warning(msg)
raise TypeError(msg)
Expand All @@ -170,9 +173,10 @@ def __init__(self, email, source=None, targets=None, **kwargs):
result = is_phone_no(target)

# Target Phone # only supports +1 country code
if result['country'] != '1':
if result['country'] \
and result['country'] != self.voip_ms_country_code:
self.logger.warning(
'Dropped invalid phone # '
'Ignoring invalid phone # '
'({}) specified.'.format(target),
)
continue
Expand All @@ -188,12 +192,12 @@ def __init__(self, email, source=None, targets=None, **kwargs):

def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
"""
Perform Voipms Notification
Perform VoIPms Notification
"""

if len(self.targets) == 0:
# There were no services to notify
self.logger.warning('There were no Voipms targets to notify.')
self.logger.warning('There were no VoIPms targets to notify.')
return False

# error tracking (used for function return)
Expand Down Expand Up @@ -228,9 +232,9 @@ def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
payload['dst'] = target

# Some Debug Logging
self.logger.debug('Voipms GET URL: {} (cert_verify={})'.format(
self.logger.debug('VoIPms GET URL: {} (cert_verify={})'.format(
self.notify_url, self.verify_certificate))
self.logger.debug('Voipms Payload: {}' .format(payload))
self.logger.debug('VoIPms Payload: {}' .format(payload))

# Always call throttle before any remote server i/o is made
self.throttle()
Expand Down Expand Up @@ -262,7 +266,7 @@ def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
r.status_code)

self.logger.warning(
'Failed to send Voipms notification to {}: '
'Failed to send VoIPms SMS notification to {}: '
'{}{}error={}.'.format(
target,
status_str,
Expand All @@ -276,12 +280,12 @@ def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
has_error = True
continue

# Voipms sends 200 OK even if there is an error
# VoIPms sends 200 OK even if there is an error
# check if status in response and if it is not success

if response is not None and response['status'] != 'success':
self.logger.warning(
'Failed to send Voipms notification to {}: '
'Failed to send VoIPms SMS notification to {}: '
'status: {}, message: {}'.format(
target, response['status'], response['message'])
)
Expand All @@ -291,12 +295,12 @@ def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
continue
else:
self.logger.info(
'Sent Voipms notification to %s' % target)
'Sent VoIPms SMS notification to %s' % target)

except requests.RequestException as e:
self.logger.warning(
'A Connection error occurred sending Voipms:%s '
'notification.' % target
'A Connection error occurred sending VoIPms:%s '
'SMS notification.' % target
)
self.logger.debug('Socket Exception: %s' % str(e))

Expand Down Expand Up @@ -331,9 +335,11 @@ def url(self, privacy=False, *args, **kwargs):
schema=self.secure_protocol,
email=self.email,
password=self.pprint(self.password, privacy, safe=''),
from_phone='1' + self.pprint(self.source, privacy, safe=''),
from_phone=self.voip_ms_country_code +
self.pprint(self.source, privacy, safe=''),
targets='/'.join(
['1' + NotifyVoipms.quote(x, safe='') for x in self.targets]),
[self.voip_ms_country_code + NotifyVoipms.quote(x, safe='')
for x in self.targets]),
params=NotifyVoipms.urlencode(params))

def __len__(self):
Expand Down

0 comments on commit dbb0841

Please sign in to comment.