Update to work with Python 3 (only)
Also bump the version to 0.2
This commit is contained in:
parent
51a67b3451
commit
ba182c025c
|
@ -1,5 +1,5 @@
|
|||
THIS IS
|
||||
A Python wrapper around the SMS gateway from CPSMS <https://www.cpsms.dk/>.
|
||||
A Python 3 wrapper around the SMS gateway from CPSMS <https://www.cpsms.dk/>.
|
||||
|
||||
THIS IS NOT
|
||||
Some sort of magic, free SMS gateway.
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
"""The cpsms package."""
|
||||
|
||||
from .cpsms import Gateway
|
133
cpsms/cpsms.py
133
cpsms/cpsms.py
|
@ -1,8 +1,5 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-'
|
||||
|
||||
"""
|
||||
Copyright 2010, 2011 Mikkel Munch Mortensen <3xm@detfalskested.dk>.
|
||||
Copyright 2010-2021 Mikkel Munch Mortensen <3xm@detfalskested.dk>.
|
||||
|
||||
This file is part of SMS Gateway.
|
||||
|
||||
|
@ -20,89 +17,105 @@ You should have received a copy of the GNU General Public License
|
|||
along with SMS Gateway. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
|
||||
import urllib, getopt
|
||||
import getopt
|
||||
from urllib.parse import urlencode
|
||||
from urllib.request import urlopen
|
||||
from typing import Any, Tuple
|
||||
|
||||
class Gateway():
|
||||
'''
|
||||
|
||||
class Gateway:
|
||||
"""
|
||||
A Python wrapper around the SMS gateway from CPSMS <https://www.cpsms.dk/>.
|
||||
|
||||
Please look at the README for further description.
|
||||
'''
|
||||
default_options = {
|
||||
'recipients' : [],
|
||||
'message' : '', # Should be a unicode string.
|
||||
'callback_url' : '',
|
||||
'timestamp' : '', # For delaying messages. Format: YYYYMMDDHHMM.
|
||||
'utf8' : 1,
|
||||
'flash' : 0,
|
||||
'group' : 0,
|
||||
'gateway_base_url' : 'https://www.cpsms.dk/sms/?'
|
||||
}
|
||||
options = {}
|
||||
"""
|
||||
|
||||
def __init__(self, username, password, sender_name, options = None):
|
||||
'''
|
||||
Initialize SMS gateway, with some options.
|
||||
'''
|
||||
options: dict[str, Any] = {}
|
||||
|
||||
@property
|
||||
def default_options(self) -> dict[str, Any]:
|
||||
return {
|
||||
"recipients": [],
|
||||
"message": "",
|
||||
"callback_url": "",
|
||||
"timestamp": "", # For delaying messages. Format: YYYYMMDDHHMM.
|
||||
"utf8": True,
|
||||
"flash": False,
|
||||
"group": False,
|
||||
"gateway_base_url": "https://www.cpsms.dk/sms/?",
|
||||
}
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
username: str,
|
||||
password: str,
|
||||
sender_name: str,
|
||||
options: dict[str, Any] = None,
|
||||
) -> None:
|
||||
"""Initialize SMS gateway, with some options."""
|
||||
self.options = self.default_options
|
||||
if options != None:
|
||||
if options is not None:
|
||||
self.options.update(options)
|
||||
|
||||
self.options['username'] = username
|
||||
self.options['password'] = password
|
||||
self.options['from'] = sender_name
|
||||
self.options["username"] = username
|
||||
self.options["password"] = password
|
||||
self.options["from"] = sender_name
|
||||
|
||||
def add_recipient(self, number):
|
||||
'''
|
||||
Add a number to the list of recipients.
|
||||
'''
|
||||
self.options['recipients'].append(number)
|
||||
def add_recipient(self, number: str) -> None:
|
||||
"""Add a number to the list of recipients."""
|
||||
self.options["recipients"].append(number)
|
||||
|
||||
def send(self, message = None):
|
||||
'''
|
||||
Send the message specified in self.options to all recipients. Optionally, override the message to be sent.
|
||||
'''
|
||||
# Decide what to send
|
||||
if message == None:
|
||||
message = self.options['message']
|
||||
def send(self, message: str = None) -> Tuple[bool, str]:
|
||||
"""
|
||||
Send the message specified in self.options to all recipients.
|
||||
|
||||
Optionally, override the message to be sent.
|
||||
"""
|
||||
# Decide what to send.
|
||||
if message is None:
|
||||
message = self.options["message"]
|
||||
|
||||
# Raise error if message is empty.
|
||||
if len(message) == 0:
|
||||
raise ValueError('Message empty.')
|
||||
if message == "":
|
||||
raise ValueError("Message empty.")
|
||||
|
||||
# Raise error if message is too long.
|
||||
if len(message) > 459:
|
||||
raise ValueError('Message not allowed to be more than 459 characters. Current message is %i characters.' % len(message))
|
||||
raise ValueError(
|
||||
"Message not allowed to be more than 459 characters."
|
||||
"Current message is %i characters." % len(message)
|
||||
)
|
||||
|
||||
# Raise error if recipients is empty.
|
||||
if len(self.options['recipients']) == 0:
|
||||
raise ValueError('No recipients.')
|
||||
if len(self.options["recipients"]) == 0:
|
||||
raise ValueError("No recipients.")
|
||||
|
||||
# Construct gateway URL.
|
||||
options = [
|
||||
('username', self.options['username']),
|
||||
('password', self.options['password']),
|
||||
('message', message.encode('utf-8')),
|
||||
('from', self.options['from']),
|
||||
('utf8', self.options['utf8']),
|
||||
('flash', self.options['flash']),
|
||||
('group', self.options['group']),
|
||||
('url', self.options['callback_url']),
|
||||
("username", self.options["username"]),
|
||||
("password", self.options["password"]),
|
||||
("message", message),
|
||||
("from", self.options["from"]),
|
||||
("utf8", int(self.options["utf8"])),
|
||||
("flash", int(self.options["flash"])),
|
||||
("group", int(self.options["group"])),
|
||||
("url", self.options["callback_url"]),
|
||||
]
|
||||
|
||||
for r in self.options['recipients']:
|
||||
options.append(('recipient[]', r))
|
||||
for r in self.options["recipients"]:
|
||||
options.append(("recipient[]", r))
|
||||
|
||||
if self.options['timestamp'] != '':
|
||||
options.append(('timestamp', self.options['timestamp']))
|
||||
if self.options["timestamp"] != "":
|
||||
options.append(("timestamp", self.options["timestamp"]))
|
||||
|
||||
url = self.options['gateway_base_url'] + urllib.urlencode(options)
|
||||
url = self.options["gateway_base_url"] + urlencode(options)
|
||||
print(url)
|
||||
|
||||
# Send SMS.
|
||||
remote_call = urllib.urlopen(url)
|
||||
result = remote_call.read()
|
||||
remote_call = urlopen(url)
|
||||
result = remote_call.read().decode()
|
||||
remote_call.close()
|
||||
if result.find('<succes>') > -1:
|
||||
if result.find("<succes>") > -1:
|
||||
return True, result
|
||||
else:
|
||||
return False, result
|
||||
|
|
26
examples.py
26
examples.py
|
@ -1,8 +1,5 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-'
|
||||
|
||||
"""
|
||||
Copyright 2010, 2011 Mikkel Munch Mortensen <3xm@detfalskested.dk>.
|
||||
Copyright 2010-2021 Mikkel Munch Mortensen <3xm@detfalskested.dk>.
|
||||
|
||||
This file is part of SMS Gateway.
|
||||
|
||||
|
@ -23,13 +20,18 @@ along with SMS Gateway. If not, see <http://www.gnu.org/licenses/>.
|
|||
import cpsms
|
||||
|
||||
# The easiest way to send a single message.
|
||||
gateway1 = cpsms.Gateway('username', 'password', 'SMS Test')
|
||||
gateway1.add_recipient('+4512345678')
|
||||
print gateway1.send(u'One way of sending a massage.')
|
||||
gateway1 = cpsms.Gateway("username", "password", "Sender Name")
|
||||
gateway1.add_recipient("+4512345678")
|
||||
print(gateway1.send("One way of sending a massage."))
|
||||
|
||||
# The easiest way to send a message to multiple recipients.
|
||||
gateway2 = cpsms.Gateway('username', 'password', 'SMS Test', {
|
||||
'recipients' : ['+4512345678', '+4587654321'],
|
||||
'message' : u'Another way of sending a message.',
|
||||
})
|
||||
print gateway2.send()
|
||||
gateway2 = cpsms.Gateway(
|
||||
"username",
|
||||
"password",
|
||||
"SMS Test",
|
||||
{
|
||||
"recipients": ["+4512345678", "+4587654321"],
|
||||
"message": "Another way of sending a message.",
|
||||
},
|
||||
)
|
||||
print(gateway2.send())
|
||||
|
|
32
setup.py
32
setup.py
|
@ -2,20 +2,22 @@ from distutils.core import setup
|
|||
|
||||
|
||||
setup(
|
||||
name='cpsms',
|
||||
packages=['cpsms'],
|
||||
version='0.1',
|
||||
author='Mikkel Munch Mortensen',
|
||||
author_email='3xm@detfalskested.dk',
|
||||
url='https://github.com/decibyte/cpsms',
|
||||
description='A Python wrapper around the SMS gateway ' \
|
||||
'from CPSMS <https://www.cpsms.dk/>.',
|
||||
name="cpsms",
|
||||
packages=["cpsms"],
|
||||
version="0.2",
|
||||
author="Mikkel Munch Mortensen",
|
||||
author_email="3xm@detfalskested.dk",
|
||||
url="https://github.com/decibyte/cpsms",
|
||||
description=(
|
||||
"A Python wrapper around the SMS gateway from "
|
||||
"CPSMS <https://www.cpsms.dk/>."
|
||||
),
|
||||
classifiers=[
|
||||
'Programming Language :: Python',
|
||||
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
|
||||
'Operating System :: OS Independent',
|
||||
'Development Status :: 4 - Beta',
|
||||
'Intended Audience :: Developers',
|
||||
'Topic :: Communications :: Telephony',
|
||||
]
|
||||
"Programming Language :: Python",
|
||||
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
|
||||
"Operating System :: OS Independent",
|
||||
"Development Status :: 4 - Beta",
|
||||
"Intended Audience :: Developers",
|
||||
"Topic :: Communications :: Telephony",
|
||||
],
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue