Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sms77 backend #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
All notable changes in **django-sms** are documented below.

## [Unreleased]
### Added
- The **sms.backends.sms77.SmsBackend** to send text messages using [sms77](https://sms77.io/).
### Changed
- Add support for Django 3.2.

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [Dummy backend](#dummy-backend)
- [MessageBird backend](#messagebird-backend)
- [Twilio backend](#twilio-backend)
- [sms77 backend](#sms77-backend)
- [Defining a custom SMS backend](#defining-a-custom-sms-backend)
- [Signals](#signals)
- [sms.signals.post_send](#sms.signals.post_send)
Expand Down Expand Up @@ -214,6 +215,15 @@ Make sure the Twilio Python SDK is installed by running the following command:
pip install "django-sms[twilio]"
```


#### sms77 backend
The [sms77](https://sms77.io/) backend sends text messages using the [sms77 SMS API](https://www.sms77.io/en/docs/gateway/http-api/sms-dispatch/). To specify this backend, put the following in your settings:

```python
SMS_BACKEND = 'sms.backends.sms77.SmsBackend'
SMS77_API_KEY = 'live_redacted-sms77-api-key'
```

### Defining a custom SMS backend
If you need to change how text messages are sent you can write your own SMS backend. The **SMS_BACKEND** setting in your settings file is then the Python import path for you backend class.

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ def long_description() -> str:
extras_require={
'messagebird': ['messagebird'],
'twilio': ['twilio'],
'sms77api': ['sms77api'],
}
)
59 changes: 59 additions & 0 deletions sms/backends/sms77.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""
SMS backend for sending text messages using sms77.
"""
from typing import List, Optional

from django.conf import settings # type: ignore
from django.core.exceptions import ImproperlyConfigured # type: ignore

from sms.backends.base import BaseSmsBackend
from sms.message import Message

try:
from sms77api.Sms77api import Sms77api # type: ignore
HAS_SMS77 = True
except ImportError:
HAS_SMS77 = False


class SmsBackend(BaseSmsBackend):
def __init__(self, fail_silently: bool = False, **kwargs) -> None:
super().__init__(fail_silently=fail_silently, **kwargs)

if not HAS_SMS77 and not self.fail_silently:
raise ImproperlyConfigured(
"You're using the SMS backend "
"'sms.backends.sms77.SmsBackend' without having "
"'sms77' installed. Install 'sms77' or use "
"another SMS backend."
)

api_key: Optional[str] = getattr(settings, 'SMS77_API_KEY')
if not api_key and not self.fail_silently:
raise ImproperlyConfigured(
"You're using the SMS backend "
"'sms.backends.sms77.SmsBackend' without having the "
"setting 'SMS77_API_KEY' set."
)

self.client = None
if HAS_SMS77:
self.client = Sms77api(api_key, 'Django-SMS')

def send_messages(self, messages: List[Message]) -> int:
if not self.client:
return 0

msg_count: int = 0
for message in messages:
try:
self.client.sms(
message.recipients,
message.body,
{'from': message.originator}
)
except Exception as exc:
if not self.fail_silently:
raise exc
msg_count += 1
return msg_count
33 changes: 33 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,38 @@ def test_send_messages(self) -> None:
)


class Sms77BackendTests(BaseSmsBackendTests, SimpleTestCase):
sms_backend = 'sms.backends.sms77.SmsBackend'

def setUp(self) -> None:
super().setUp()
self._settings_override = override_settings(
SMS77_API_KEY='face_api_key'
)
self._settings_override.enable()

def tearDown(self) -> None:
self._settings_override.disable()
super().tearDown()

def test_send_messages(self) -> None:
"""Test send_messages with the MessageBird backend."""
message = Message(
'Here is the message',
'+12065550100',
['+441134960000']
)

connection = sms.get_connection()
connection.client.message_create = MagicMock() # type: ignore
connection.send_messages([message]) # type: ignore
connection.client.message_create.assert_called_with( # type: ignore
'+12065550100',
['+441134960000'],
'Here is the message'
)


class SignalTests(SimpleTestCase):

def flush_mailbox(self) -> None:
Expand All @@ -270,3 +302,4 @@ def f(instance, **kwargs):

self.assertTrue(self.state)
self.assertEqual(body, self.body)

1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ setenv=
deps=
messagebird
twilio
sms77
coverage
mypy
django22: Django==2.2.*
Expand Down