Update to work with current CPSMS API

Also bump the version.
This commit is contained in:
Mikkel Munch Mortensen 2021-11-03 14:25:53 +01:00
parent ba182c025c
commit b414aac902
3 changed files with 72 additions and 77 deletions

View file

@ -17,9 +17,11 @@ You should have received a copy of the GNU General Public License
along with SMS Gateway. If not, see <http://www.gnu.org/licenses/>. along with SMS Gateway. If not, see <http://www.gnu.org/licenses/>.
""" """
import base64
import getopt import getopt
from urllib.parse import urlencode import json
from urllib.request import urlopen import re
from urllib import error, parse, request
from typing import Any, Tuple from typing import Any, Tuple
@ -31,18 +33,23 @@ class Gateway:
""" """
options: dict[str, Any] = {} options: dict[str, Any] = {}
username: str
password: str
gateway_url: str = "https://api.cpsms.dk/v2/send"
@property @property
def default_options(self) -> dict[str, Any]: def default_options(self) -> dict[str, Any]:
"""Get some default options to use."""
return { return {
"recipients": [], "to": "",
"message": "", "message": "",
"callback_url": "", "from": "",
"timestamp": "", # For delaying messages. Format: YYYYMMDDHHMM. "timestamp": "", # For delaying messages. Format: Unix timestamp.
"utf8": True, "encoding": "UTF-8",
"dlr_url": "",
"flash": False, "flash": False,
"group": False, "reference": "",
"gateway_base_url": "https://www.cpsms.dk/sms/?", "format": "GSM",
} }
def __init__( def __init__(
@ -51,71 +58,72 @@ class Gateway:
password: str, password: str,
sender_name: str, sender_name: str,
options: dict[str, Any] = None, options: dict[str, Any] = None,
gateway_url: str = None,
) -> None: ) -> None:
"""Initialize SMS gateway, with some options.""" """Initialize SMS gateway."""
self.username = username
self.password = password
self.options = self.default_options self.options = self.default_options
self.options["from"] = sender_name
if options is not None: if options is not None:
self.options.update(options) self.options.update(options)
self.options["username"] = username if gateway_url:
self.options["password"] = password self.gateway_url = gateway_url
self.options["from"] = sender_name
def add_recipient(self, number: str) -> None: def send(self, to: str = None, message: str = None) -> Tuple[bool, str]:
"""Add a number to the list of recipients."""
self.options["recipients"].append(number)
def send(self, message: str = None) -> Tuple[bool, str]:
""" """
Send the message specified in self.options to all recipients. Send a message to a recipient.
Optionally, override the message to be sent. Optionally, override the recpient and the message to be sent.
""" """
# Decide what to send. # Raise an error if the sender is not specified.
if message is None: if not self.options["from"]:
message = self.options["message"] raise ValueError("Sender name cannot be empty.")
# Update message if specified.
if message is not None:
self.options["message"] = message
# Raise error if message is empty. # Raise error if message is empty.
if message == "": if not self.options["message"]:
raise ValueError("Message empty.") raise ValueError("Message cannot be empty.")
# Raise error if message is too long. # Raise error if message is too long.
if len(message) > 459: if len(self.options["message"]) > 459:
raise ValueError( raise ValueError(
"Message not allowed to be more than 459 characters." "Message not allowed to be more than 459 characters."
"Current message is %i characters." % len(message) "Current message is %i characters."
% len(self.options["message"])
) )
# Update recipient if specified.
if to is not None:
self.options["to"] = to
# Raise error if recipients is empty. # Raise error if recipients is empty.
if len(self.options["recipients"]) == 0: if not self.options["to"]:
raise ValueError("No recipients.") raise ValueError("No recipient is set.")
# Construct gateway URL. # Raise error if recipient is not a number.
options = [ pattern = re.compile("^[1-9]+$")
("username", self.options["username"]), if pattern.match(self.options["to"]) is None:
("password", self.options["password"]), raise ValueError("Recipient number must be numbers only (no characters, spaces or +)")
("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"]: # Prepare the data to send along.
options.append(("recipient[]", r)) data = self.options
data["flash"] = int(data["flash"])
data = json.dumps(data).encode()
if self.options["timestamp"] != "": # Prepare authentication.
options.append(("timestamp", self.options["timestamp"])) auth_header = "{}:{}".format(self.username, self.password)
auth_header = base64.b64encode(auth_header.encode()).decode()
url = self.options["gateway_base_url"] + urlencode(options) http_request = request.Request(self.gateway_url, data=data)
print(url) http_request.add_header(
"Authorization", "Basic {}".format(auth_header)
)
response = request.urlopen(http_request)
# Send SMS. return json.loads(response.read().decode())
remote_call = urlopen(url)
result = remote_call.read().decode()
remote_call.close()
if result.find("<succes>") > -1:
return True, result
else:
return False, result

View file

@ -19,19 +19,14 @@ along with SMS Gateway. If not, see <http://www.gnu.org/licenses/>.
import cpsms import cpsms
# The easiest way to send a single message.
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. # This will send text messages to Bob and Carol respectively. On their
gateway2 = cpsms.Gateway( # devices, the sender will shown as "Alice".
"username",
"password", gateway = cpsms.Gateway("username", "password", "Alice")
"SMS Test", gateway.send("4512345678", "Hello Bob")
{ gateway.send("4587654321", "Hello Carol")
"recipients": ["+4512345678", "+4587654321"],
"message": "Another way of sending a message.", # The `.send()` method will return the response from the SMS gateway. Have a
}, # look at the CPSMS documentation to see what responses look like:
) # <https://api.cpsms.dk/documentation/index.html#send>
print(gateway2.send())

View file

@ -4,7 +4,7 @@ from distutils.core import setup
setup( setup(
name="cpsms", name="cpsms",
packages=["cpsms"], packages=["cpsms"],
version="0.2", version="2.0",
author="Mikkel Munch Mortensen", author="Mikkel Munch Mortensen",
author_email="3xm@detfalskested.dk", author_email="3xm@detfalskested.dk",
url="https://github.com/decibyte/cpsms", url="https://github.com/decibyte/cpsms",
@ -12,12 +12,4 @@ setup(
"A Python wrapper around the SMS gateway from " "A Python wrapper around the SMS gateway from "
"CPSMS <https://www.cpsms.dk/>." "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",
],
) )