A Python package for sending SMS with different providers
Project description
python-sage-sms
The Sage SMS package is designed to facilitate the sending of SMS messages through various providers. This package provides a flexible and extensible framework for integrating multiple SMS backends, validating phone numbers, and handling SMS-related errors.
Key Features
- Modular Design: The package is organized into modules for different functionalities, including backend management, phone number validation, and exception handling.
- Backend Discovery and Loading: SMS backends are dynamically discovered and loaded based on the provided configuration.
- Phone Number Validation: Phone numbers are validated and formatted using the
phonenumbers
library to ensure compliance with international standards. - Exception Handling: Custom exceptions are defined for various error scenarios, providing clear and specific error messages.
Getting Started
Installation
To install the Sage SMS package, use pip:
pip install python-sage-sms
Usage
To use an SMS backend, import the necessary modules and configure the settings for the desired SMS provider. Here is a quick example:
from sage_sms.factory import SMSBackendFactory
# Define settings for the SMS provider
settings = {
"debug": False,
"provider": {
"NAME": "provider_name",
"API_KEY": "your_api_key"
}
}
# Initialize the factory with settings and the base package path for the backends
# Replace "your_project.backends" with the actual path where your backend modules are located
factory = SMSBackendFactory(settings, "your_project.backends")
# Get the SMS provider class and instantiate it
sms_provider_class = factory.get_backend()
sms_provider = sms_provider_class(settings)
# Send a test SMS message
sms_provider.send_one_message("+1234567890", "Hello, World!")
Creating a Backend
To create a new SMS backend, follow these steps:
- Implement the ISmsProvider Interface: Create a class that implements the methods defined in the
ISmsProvider
interface. - Add Backend Module: Add the new backend module to the appropriate package directory.
- Update Configuration: Update the configuration settings to include the new backend provider.
Example: Twilio Backend
Here is an example of how to implement a backend for the Twilio service.
import logging
logger = logging.getLogger(__name__)
try:
from twilio.rest import Client as TwilioClient
except ImportError:
TwilioClient = None
logger.error("Failed to import TwilioClient. Ensure 'twilio' package is installed.")
from sage_sms.design.interfaces.provider import ISmsProvider
from sage_sms.validators import PhoneNumberValidator
class Twilio(ISmsProvider):
def __init__(self, settings):
if TwilioClient is None:
logger.critical(
"TwilioClient is None. Install `twilio` package. Run `pip install twilio`."
)
raise ImportError(
"Install `twilio` package. Run `pip install twilio`."
)
self.phone_number_validator = PhoneNumberValidator()
self._api_key = settings["provider"]["API_KEY"]
self._auth_token = settings["provider"]["AUTH_TOKEN"]
self._line_number = settings["provider"].get("LINE_NUMBER")
self.twilio_client = TwilioClient(self._api_key, self._auth_token)
def send_one_message(self, phone_number: str, message: str, linenumber=None) -> None:
try:
cast_phone_number = self.phone_number_validator.validate_and_format(phone_number, region="US")
self.twilio_client.messages.create(
from_=self._line_number,
body=message,
to=cast_phone_number
)
except Exception as e:
logger.error(f"Failed to send message to {phone_number}: {e}")
def send_bulk_messages(self, phone_numbers: list[str], message: str, linenumber=None) -> None:
raise NotImplementedError
def send_verify_message(self, phone_number: str, value: str):
raise NotImplementedError
Conclusion
The Sage SMS package offers a robust and flexible solution for sending SMS messages through various providers. Its modular design, comprehensive validation, and detailed error handling make it a reliable choice for integrating SMS functionality into applications.
For detailed instructions on creating backends, read the Creating a Backend section in the documentation.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file python_sage_sms-0.1.1.tar.gz
.
File metadata
- Download URL: python_sage_sms-0.1.1.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.4 Windows/11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
d5c7758c7ef6ead9ab1765bdea91f707a849df928d31661448f434b5bd7b411d
|
|
MD5 |
72048938fb1cebdb1ba940be47e003b0
|
|
BLAKE2b-256 |
cf6aa632afb6a54432557b6979492243c3372e175303fc0213d0ad6d1d45806a
|
File details
Details for the file python_sage_sms-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: python_sage_sms-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.4 Windows/11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
063b4b83da707f7fc8fb6d66c8a71fd632e209d5a576c02932cf456d81e4cc53
|
|
MD5 |
7d98e798e46f15dd0f55fc552666a4d4
|
|
BLAKE2b-256 |
6eca392f9c60e4fe7f01581d51af2943c16ab1bebadfb1bde3d30badf47f045d
|