python-sage-sms


Namepython-sage-sms JSON
Version 0.1.1 PyPI version JSON
download
home_pageNone
SummaryA Python package for sending SMS with different providers
upload_time2024-09-30 17:44:40
maintainerNone
docs_urlNone
authorSepehr Akbarzadeh
requires_python<4.0,>=3.9
licenseMIT
keywords python sms integration
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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:

```bash
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:

```python
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:

1. **Implement the ISmsProvider Interface**: Create a class that implements the methods defined in the `ISmsProvider` interface.
2. **Add Backend Module**: Add the new backend module to the appropriate package directory.
3. **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.

```python
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](https://python-sage-sms.readthedocs.io/en/latest/) section in the documentation.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "python-sage-sms",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": null,
    "keywords": "python, sms, integration",
    "author": "Sepehr Akbarzadeh",
    "author_email": "sepehr@sageteam.org",
    "download_url": "https://files.pythonhosted.org/packages/cf/6a/a632afb6a54432557b6979492243c3372e175303fc0213d0ad6d1d45806a/python_sage_sms-0.1.1.tar.gz",
    "platform": null,
    "description": "# python-sage-sms\n\nThe 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.\n\n## Key Features\n\n- **Modular Design**: The package is organized into modules for different functionalities, including backend management, phone number validation, and exception handling.\n- **Backend Discovery and Loading**: SMS backends are dynamically discovered and loaded based on the provided configuration.\n- **Phone Number Validation**: Phone numbers are validated and formatted using the `phonenumbers` library to ensure compliance with international standards.\n- **Exception Handling**: Custom exceptions are defined for various error scenarios, providing clear and specific error messages.\n\n## Getting Started\n\n### Installation\n\nTo install the Sage SMS package, use pip:\n\n```bash\npip install python-sage-sms\n```\n\n### Usage\n\nTo use an SMS backend, import the necessary modules and configure the settings for the desired SMS provider. Here is a quick example:\n\n```python\nfrom sage_sms.factory import SMSBackendFactory\n\n# Define settings for the SMS provider\nsettings = {\n    \"debug\": False,\n    \"provider\": {\n        \"NAME\": \"provider_name\",\n        \"API_KEY\": \"your_api_key\"\n    }\n}\n\n# Initialize the factory with settings and the base package path for the backends\n# Replace \"your_project.backends\" with the actual path where your backend modules are located\nfactory = SMSBackendFactory(settings, \"your_project.backends\")\n\n# Get the SMS provider class and instantiate it\nsms_provider_class = factory.get_backend()\nsms_provider = sms_provider_class(settings)\n\n# Send a test SMS message\nsms_provider.send_one_message(\"+1234567890\", \"Hello, World!\")\n```\n\n## Creating a Backend\n\nTo create a new SMS backend, follow these steps:\n\n1. **Implement the ISmsProvider Interface**: Create a class that implements the methods defined in the `ISmsProvider` interface.\n2. **Add Backend Module**: Add the new backend module to the appropriate package directory.\n3. **Update Configuration**: Update the configuration settings to include the new backend provider.\n\n### Example: Twilio Backend\n\nHere is an example of how to implement a backend for the Twilio service.\n\n```python\nimport logging\nlogger = logging.getLogger(__name__)\n\ntry:\n    from twilio.rest import Client as TwilioClient\nexcept ImportError:\n    TwilioClient = None\n    logger.error(\"Failed to import TwilioClient. Ensure 'twilio' package is installed.\")\n\nfrom sage_sms.design.interfaces.provider import ISmsProvider\nfrom sage_sms.validators import PhoneNumberValidator\n\nclass Twilio(ISmsProvider):\n    def __init__(self, settings):\n        if TwilioClient is None:\n            logger.critical(\n                \"TwilioClient is None. Install `twilio` package. Run `pip install twilio`.\"\n            )\n            raise ImportError(\n                \"Install `twilio` package. Run `pip install twilio`.\"\n            )\n\n        self.phone_number_validator = PhoneNumberValidator()\n        self._api_key = settings[\"provider\"][\"API_KEY\"]\n        self._auth_token = settings[\"provider\"][\"AUTH_TOKEN\"]\n        self._line_number = settings[\"provider\"].get(\"LINE_NUMBER\")\n        self.twilio_client = TwilioClient(self._api_key, self._auth_token)\n\n    def send_one_message(self, phone_number: str, message: str, linenumber=None) -> None:\n        try:\n            cast_phone_number = self.phone_number_validator.validate_and_format(phone_number, region=\"US\")\n            self.twilio_client.messages.create(\n                from_=self._line_number,\n                body=message,\n                to=cast_phone_number\n            )\n        except Exception as e:\n            logger.error(f\"Failed to send message to {phone_number}: {e}\")\n\n    def send_bulk_messages(self, phone_numbers: list[str], message: str, linenumber=None) -> None:\n        raise NotImplementedError\n\n    def send_verify_message(self, phone_number: str, value: str):\n        raise NotImplementedError\n```\n\n## Conclusion\n\nThe 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.\n\nFor detailed instructions on creating backends, read the [Creating a Backend](https://python-sage-sms.readthedocs.io/en/latest/) section in the documentation.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A Python package for sending SMS with different providers",
    "version": "0.1.1",
    "project_urls": {
        "Issues": "https://github.com/sageteamorg/python-sage-sms/issues",
        "Source Code": "https://github.com/sageteamorg/python-sage-sms"
    },
    "split_keywords": [
        "python",
        " sms",
        " integration"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6eca392f9c60e4fe7f01581d51af2943c16ab1bebadfb1bde3d30badf47f045d",
                "md5": "7d98e798e46f15dd0f55fc552666a4d4",
                "sha256": "063b4b83da707f7fc8fb6d66c8a71fd632e209d5a576c02932cf456d81e4cc53"
            },
            "downloads": -1,
            "filename": "python_sage_sms-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7d98e798e46f15dd0f55fc552666a4d4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 9814,
            "upload_time": "2024-09-30T17:44:38",
            "upload_time_iso_8601": "2024-09-30T17:44:38.859802Z",
            "url": "https://files.pythonhosted.org/packages/6e/ca/392f9c60e4fe7f01581d51af2943c16ab1bebadfb1bde3d30badf47f045d/python_sage_sms-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cf6aa632afb6a54432557b6979492243c3372e175303fc0213d0ad6d1d45806a",
                "md5": "72048938fb1cebdb1ba940be47e003b0",
                "sha256": "d5c7758c7ef6ead9ab1765bdea91f707a849df928d31661448f434b5bd7b411d"
            },
            "downloads": -1,
            "filename": "python_sage_sms-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "72048938fb1cebdb1ba940be47e003b0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 10041,
            "upload_time": "2024-09-30T17:44:40",
            "upload_time_iso_8601": "2024-09-30T17:44:40.375529Z",
            "url": "https://files.pythonhosted.org/packages/cf/6a/a632afb6a54432557b6979492243c3372e175303fc0213d0ad6d1d45806a/python_sage_sms-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-30 17:44:40",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "sageteamorg",
    "github_project": "python-sage-sms",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "python-sage-sms"
}
        
Elapsed time: 1.05882s