Skip to main content

API wrapper for https://semaphore.co/

Project description

semaphore-sms

CI codecov PyPI Python 3.7+

API wrapper for Semaphore SMS Gateway. The documentation for their API can be found here.

Installation

This library supports Python versions >= 3.7.

Install from PyPi using pip via

pip install semaphore-sms

Don't have pip installed? Refer to this documentation

Usage

‼️ For all these calls, you can see sample responses in tests/test_semaphore_client.py ‼️

Initializing the client

from semaphore_sms import SemaphoreClient

client = SemaphoreClient(
    api_key='your_api_key',
    sender_name='sender_name',
)

Note: SemaphoreClient will try to pull api_key from os.environ if not supplied. If it still can't a value then, a SemaphoreException will be raised

Sending SMS

For sending a basic single message,

client.send(
    message='Your text here',
    recipients=['09980101010', ]
)

To send one message to several numbers in bulk, simply provide multiple numbers under recipients

client.send(
    message='Your text here',
    recipients=['09980101010', '09980202020', ]
)

Sending out priority messages and OTP messages will look very similar

client.priority(
    message='Your text here',
    recipients=['09980101010', '09980202020', ]
)

client.otp(
    message='Your OTP is: {otp}',  # refer to Semaphore's docs for details
    recipients=['09980101010', ],
    code=123456,  # refer to Semaphore's docs for details
)

Fetching messages

https://semaphore.co/docs#retrieving_messages

client.messages(
    limit=100,  # optional
    page=1,  # optional
    start_date='2023-11-06',  # optional
    end_date='2023-12-25',  # optional
    network='smart',  # optional
    status='succcess'  # optional
)

Retrieving account information

https://semaphore.co/docs#retrieving_account

account_information = client.account
transactions = client.transactions(
    limit=100,  # optional
    page=1  # optional
)
sender_names = client.sender_names(
    limit=100,  # optional
    page=1  # optional
)
users = client.users(
    limit=100,  # optional
    page=1  # optional
)

Again, you can see sample responses in tests/test_semaphore_client.py

Error Handling

The library provides a comprehensive exception hierarchy for handling different error scenarios:

SemaphoreException (base)
├── AuthenticationError     # Missing or invalid API credentials
├── ValidationError         # Client-side validation failures
│   ├── InvalidPhoneNumberError  # Invalid Philippine phone format
│   └── EmptyMessageError        # Blank message content
└── APIError               # Server returned an error response
    ├── AuthorizationError # 401/403 responses
    ├── RateLimitError     # 429 responses (includes retry_after)
    ├── ServerError        # 5xx responses
    └── NetworkError       # Connection/timeout failures

Example Usage

from semaphore_sms import SemaphoreClient
from semaphore_sms.exceptions import (
    AuthenticationError,
    InvalidPhoneNumberError,
    RateLimitError,
    SemaphoreException,
)

try:
    client = SemaphoreClient(api_key='your_api_key')
    client.send(message='Hello', recipients=['09991234567'])
except AuthenticationError:
    print("Invalid API key")
except InvalidPhoneNumberError as e:
    print(f"Invalid phone numbers: {e.recipients}")
except RateLimitError as e:
    if e.retry_after:
        print(f"Rate limited. Retry after {e.retry_after} seconds")
except SemaphoreException as e:
    print(f"Semaphore error: {e}")

All exceptions inherit from SemaphoreException, so you can catch all library errors with a single except block for backward compatibility.

Tests

Clone the repo and run

PYTHONPATH=src python -m unittest

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/sbenemerito/semaphore-sms/

Note: Yes, I'm currently pushing directly to main - I know, I know. When contributors come around, I'll enforce proper branch protection and PR workflows 🙇‍♂️

License

The library is available as open source under the terms of the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

semaphore_sms-1.0.1.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

semaphore_sms-1.0.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file semaphore_sms-1.0.1.tar.gz.

File metadata

  • Download URL: semaphore_sms-1.0.1.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for semaphore_sms-1.0.1.tar.gz
Algorithm Hash digest
SHA256 3e7c73d9ba248f0ebf6ddf2fe50c982c456fe43b3fa53a54c4502fcfb74d67eb
MD5 6e56589e230c3c8d49fab2df9a835b69
BLAKE2b-256 c035c06554f86d4095a0c3aaaf0c415de2820af7198a1cf52b8798b315a11f41

See more details on using hashes here.

Provenance

The following attestation bundles were made for semaphore_sms-1.0.1.tar.gz:

Publisher: publish.yml on sbenemerito/semaphore-sms

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file semaphore_sms-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: semaphore_sms-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for semaphore_sms-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 248f794a04321914f1ffe342669a647f2a2ce4e08f602af9125bd1caadf84e0d
MD5 27ca29deb74debac790825153d229ca2
BLAKE2b-256 37c769d5aafd2aad22c478dae79d42852e9b8658dc0f51cfe99a2fd46066076f

See more details on using hashes here.

Provenance

The following attestation bundles were made for semaphore_sms-1.0.1-py3-none-any.whl:

Publisher: publish.yml on sbenemerito/semaphore-sms

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page