improved voipms:// phone parsing

pull/1230/head
Chris Caron 4 weeks ago
parent 78f16adbb9
commit dbb0841669

@ -47,7 +47,7 @@ from ..locale import gettext_lazy as _
class NotifyVoipms(NotifyBase): class NotifyVoipms(NotifyBase):
""" """
A wrapper for Voipms Notifications A wrapper for VoIPms Notifications
""" """
# The default descriptive name associated with the Notification # The default descriptive name associated with the Notification
@ -62,12 +62,15 @@ class NotifyVoipms(NotifyBase):
# A URL that takes you to the setup/help of the specific protocol # A URL that takes you to the setup/help of the specific protocol
setup_url = 'https://github.com/caronc/apprise/wiki/Notify_voipms' 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' notify_url = 'https://voip.ms/api/v1/rest.php'
# The maximum length of the body # The maximum length of the body
body_maxlen = 160 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 # 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. # cause any title (if defined) to get placed into the message body.
title_maxlen = 0 title_maxlen = 0
@ -122,12 +125,11 @@ class NotifyVoipms(NotifyBase):
def __init__(self, email, source=None, targets=None, **kwargs): def __init__(self, email, source=None, targets=None, **kwargs):
""" """
Initialize Voipms Object Initialize VoIPms Object
""" """
super().__init__(**kwargs) super().__init__(**kwargs)
# Validate our params here. # Validate our params here.
if self.password is None: if self.password is None:
msg = 'Password has to be specified.' msg = 'Password has to be specified.'
self.logger.warning(msg) self.logger.warning(msg)
@ -136,7 +138,7 @@ class NotifyVoipms(NotifyBase):
# User is the email associated with the account # User is the email associated with the account
result = is_email(email) result = is_email(email)
if not result: if not result:
msg = 'An invalid Voipms user email: ' \ msg = 'An invalid VoIPms user email: ' \
'({}) was specified.'.format(email) '({}) was specified.'.format(email)
self.logger.warning(msg) self.logger.warning(msg)
raise TypeError(msg) raise TypeError(msg)
@ -145,15 +147,16 @@ class NotifyVoipms(NotifyBase):
# Validate our source Phone # # Validate our source Phone #
result = is_phone_no(source) result = is_phone_no(source)
if not result: if not result:
msg = 'An invalid Voipms source phone # ' \ msg = 'An invalid VoIPms source phone # ' \
'({}) was specified.'.format(source) '({}) was specified.'.format(source)
self.logger.warning(msg) self.logger.warning(msg)
raise TypeError(msg) raise TypeError(msg)
# Source Phone # only supports +1 country code # Source Phone # only supports +1 country code
# Allow 7 digit phones (presume they're local with +1 country code) # Allow 7 digit phones (presume they're local with +1 country code)
if result['country'] and result['country'] != '1': if result['country'] \
msg = 'Voipms only supports +1 country code ' \ and result['country'] != self.voip_ms_country_code:
msg = 'VoIPms only supports +1 country code ' \
'({}) was specified.'.format(source) '({}) was specified.'.format(source)
self.logger.warning(msg) self.logger.warning(msg)
raise TypeError(msg) raise TypeError(msg)
@ -170,9 +173,10 @@ class NotifyVoipms(NotifyBase):
result = is_phone_no(target) result = is_phone_no(target)
# Target Phone # only supports +1 country code # 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( self.logger.warning(
'Dropped invalid phone # ' 'Ignoring invalid phone # '
'({}) specified.'.format(target), '({}) specified.'.format(target),
) )
continue continue
@ -188,12 +192,12 @@ class NotifyVoipms(NotifyBase):
def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs): def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
""" """
Perform Voipms Notification Perform VoIPms Notification
""" """
if len(self.targets) == 0: if len(self.targets) == 0:
# There were no services to notify # 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 return False
# error tracking (used for function return) # error tracking (used for function return)
@ -228,9 +232,9 @@ class NotifyVoipms(NotifyBase):
payload['dst'] = target payload['dst'] = target
# Some Debug Logging # 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.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 # Always call throttle before any remote server i/o is made
self.throttle() self.throttle()
@ -262,7 +266,7 @@ class NotifyVoipms(NotifyBase):
r.status_code) r.status_code)
self.logger.warning( self.logger.warning(
'Failed to send Voipms notification to {}: ' 'Failed to send VoIPms SMS notification to {}: '
'{}{}error={}.'.format( '{}{}error={}.'.format(
target, target,
status_str, status_str,
@ -276,12 +280,12 @@ class NotifyVoipms(NotifyBase):
has_error = True has_error = True
continue 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 # check if status in response and if it is not success
if response is not None and response['status'] != 'success': if response is not None and response['status'] != 'success':
self.logger.warning( self.logger.warning(
'Failed to send Voipms notification to {}: ' 'Failed to send VoIPms SMS notification to {}: '
'status: {}, message: {}'.format( 'status: {}, message: {}'.format(
target, response['status'], response['message']) target, response['status'], response['message'])
) )
@ -291,12 +295,12 @@ class NotifyVoipms(NotifyBase):
continue continue
else: else:
self.logger.info( self.logger.info(
'Sent Voipms notification to %s' % target) 'Sent VoIPms SMS notification to %s' % target)
except requests.RequestException as e: except requests.RequestException as e:
self.logger.warning( self.logger.warning(
'A Connection error occurred sending Voipms:%s ' 'A Connection error occurred sending VoIPms:%s '
'notification.' % target 'SMS notification.' % target
) )
self.logger.debug('Socket Exception: %s' % str(e)) self.logger.debug('Socket Exception: %s' % str(e))
@ -331,9 +335,11 @@ class NotifyVoipms(NotifyBase):
schema=self.secure_protocol, schema=self.secure_protocol,
email=self.email, email=self.email,
password=self.pprint(self.password, privacy, safe=''), 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( 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)) params=NotifyVoipms.urlencode(params))
def __len__(self): def __len__(self):

Loading…
Cancel
Save