Add auto-split long SMS feature

This commit is contained in:
Benjamin Renard 2017-11-14 17:28:48 +01:00
parent 60fc16756a
commit 698d7502b0

View file

@ -123,6 +123,26 @@ def mail_address_to_phone_number(txt):
return False return False
def send_sms(recipient, text): def send_sms(recipient, text):
if len(text) > 160 and options.split:
logging.debug('Text contain more than 160 caracteres : split in multiple SMS')
start=0
while True:
if start > len(text)-1:
break
if start==0:
stop=start+157
msg=text[start:stop]+u'...'
else:
stop=start+154
if stop>=(len(text)-1):
msg=u'...'+text[start:]
else:
msg=u'...'+text[start:stop]+u'...'
if not send_sms(recipient, msg):
return False
start=stop
return True
url_params={ url_params={
'phone': recipient, 'phone': recipient,
'text': text.encode('utf8') 'text': text.encode('utf8')
@ -139,6 +159,7 @@ def send_sms(recipient, text):
'' ''
] ]
url=urlparse.urlunparse(url_parts) url=urlparse.urlunparse(url_parts)
logging.debug(u'Send SMS using url : %s' % url)
try: try:
request=urllib2.urlopen(url, timeout=options.smstimeout) request=urllib2.urlopen(url, timeout=options.smstimeout)
data=request.read() data=request.read()
@ -200,6 +221,12 @@ parser.add_option('-l',
dest="logfile", dest="logfile",
help="Log file path") help="Log file path")
parser.add_option('-s',
'--auto-split',
action="store_true",
dest="split",
help="Auto split long SMS by small SMS of 160 characters")
parser.add_option('-b', parser.add_option('-b',
'--backup-mail', '--backup-mail',
action="store", action="store",