smswire 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- smswire/__init__.py +17 -0
- smswire/_client.py +575 -0
- smswire/_http.py +141 -0
- smswire/_requests.py +166 -0
- smswire/_version.py +1 -0
- smswire/errors.py +24 -0
- smswire/py.typed +0 -0
- smswire/types.py +282 -0
- smswire/webhooks.py +61 -0
- smswire-0.2.0.dist-info/METADATA +206 -0
- smswire-0.2.0.dist-info/RECORD +13 -0
- smswire-0.2.0.dist-info/WHEEL +4 -0
- smswire-0.2.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: smswire
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Official Python SDK for SMSWire: send SMS and one-time codes through your own Android phones.
|
|
5
|
+
Project-URL: Homepage, https://smswire.app
|
|
6
|
+
Project-URL: Documentation, https://smswire.app/docs#sdks
|
|
7
|
+
Project-URL: Source, https://github.com/jolabs-io/smswire-python
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: android,gateway,otp,sms,smswire
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Typing :: Typed
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Requires-Dist: httpx<1,>=0.25
|
|
17
|
+
Provides-Extra: dev
|
|
18
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
19
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# SMSWire for Python
|
|
23
|
+
|
|
24
|
+
Send SMS and one-time codes from your app through your own Android phones with [SMSWire](https://smswire.app).
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
pip install smswire
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Python 3.10+. Sync and async clients, built on httpx.
|
|
31
|
+
|
|
32
|
+
## Set up
|
|
33
|
+
|
|
34
|
+
Each project on the dashboard has an API key and a slug (the part after `/p/` in its endpoint). Keep them in the environment:
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
SMSWIRE_KEY="smswire_live_…"
|
|
38
|
+
SMSWIRE_PROJECT="prj_…"
|
|
39
|
+
SMSWIRE_WEBHOOK_SECRET="whsec_…" # only if you receive webhooks
|
|
40
|
+
# SMSWIRE_URL="https://api.smswire.app" # your server, if you host SMSWire yourself
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from smswire import SMSWire
|
|
45
|
+
|
|
46
|
+
sms = SMSWire() # or SMSWire(api_key=..., project=..., base_url=...)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Send
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
message = sms.messages.send(
|
|
53
|
+
"+251911234567",
|
|
54
|
+
"Your order {{order}} shipped",
|
|
55
|
+
variables={"order": "A-1042"},
|
|
56
|
+
type="transactional", # otp | transactional | notification | marketing
|
|
57
|
+
tags=["orders"],
|
|
58
|
+
metadata={"user_id": 42}, # echoed back in webhooks
|
|
59
|
+
)
|
|
60
|
+
# → {"id": "msg_…", "status": "queued", "segments": 1, …}
|
|
61
|
+
|
|
62
|
+
# Your own idempotency key makes your retries safe too (the SDK adds a random one otherwise).
|
|
63
|
+
sms.messages.send(to, body, idempotency_key=f"order-{order.id}")
|
|
64
|
+
|
|
65
|
+
sms.messages.get(message["id"])
|
|
66
|
+
sms.messages.cancel(message["id"]) # before it reaches a phone
|
|
67
|
+
|
|
68
|
+
page = sms.messages.list(status="failed", limit=50)
|
|
69
|
+
for m in sms.messages.list_all(tag="orders"):
|
|
70
|
+
print(m["id"], m["status"])
|
|
71
|
+
|
|
72
|
+
batch = sms.messages.send_bulk(
|
|
73
|
+
[{"to": "+251911234567", "variables": {"name": "Abebe"}}, {"to": "+251922345678", "variables": {"name": "Sara"}}],
|
|
74
|
+
name="Weekend sale",
|
|
75
|
+
body="Hi {{name}}, 20% off this weekend",
|
|
76
|
+
rate=30, # per minute
|
|
77
|
+
)
|
|
78
|
+
# → {"batch_id": …, "accepted": 2, "rejected": [{"index", "to", "reason"}]}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Templates in more than one language
|
|
82
|
+
|
|
83
|
+
A template saved on the dashboard can hold a text per language. Send it by name and each recipient
|
|
84
|
+
gets the one they read — the `language` on their contact — while everyone else gets the default text.
|
|
85
|
+
A message says which language it went out in (`language`; `null` is the default text), and `pt-BR`
|
|
86
|
+
matches a text written in `pt`.
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
sms.messages.send_bulk(
|
|
90
|
+
[
|
|
91
|
+
{"to": "+251911234567", "variables": {"name": "Abebe"}}, # reads am → the Amharic text
|
|
92
|
+
{"to": "+14155550123", "variables": {"name": "Alex"}}, # no language → the default text
|
|
93
|
+
{"to": "+5511999990000", "language": "pt-BR", "variables": {"name": "Bruna"}},
|
|
94
|
+
],
|
|
95
|
+
template="order-shipped",
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
# One language for everybody, whatever they read:
|
|
99
|
+
sms.messages.send("+251911234567", template="order-shipped", language="am", variables={"name": "Abebe"})
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## One-time codes
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
otp = sms.otp.send("+251911234567", length=6, ttl=300)
|
|
106
|
+
# later, with what the user typed:
|
|
107
|
+
result = sms.otp.verify(otp["id"], code)
|
|
108
|
+
if not result["valid"]:
|
|
109
|
+
print(result["reason"]) # invalid | expired | too_many_attempts | already_used
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Recurring sends
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
schedule = sms.schedules.create(
|
|
116
|
+
"0 9 * * 1",
|
|
117
|
+
name="Weekly report",
|
|
118
|
+
timezone="Africa/Addis_Ababa",
|
|
119
|
+
to="+251911234567",
|
|
120
|
+
body="Your weekly report is ready",
|
|
121
|
+
)
|
|
122
|
+
sms.schedules.update(schedule["id"], enabled=False)
|
|
123
|
+
sms.schedules.list()
|
|
124
|
+
sms.schedules.delete(schedule["id"])
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Contact groups
|
|
128
|
+
|
|
129
|
+
Groups belong to the workspace, so every project's key sees the same ones. With `keywords`, someone who texts exactly one of them to a workspace phone joins the group (and gets `join_reply` once); your webhook hears `group.joined`.
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
group = sms.groups.create("Promo", keywords=["A2"], join_reply="You're in! Reply STOP to opt out.")
|
|
133
|
+
sms.groups.add_members(group["id"], [{"phone": "+251911234567", "name": "Abebe"}])
|
|
134
|
+
for member in sms.groups.list_all_members(group["id"]):
|
|
135
|
+
print(member["phone"], member["source"])
|
|
136
|
+
sms.messages.send_bulk(group_ids=[group["id"]], body="Hi {{name|there}}, 20% off today")
|
|
137
|
+
sms.groups.remove_member(group["id"], "+251911234567")
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Async
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
from smswire import AsyncSMSWire
|
|
144
|
+
|
|
145
|
+
async with AsyncSMSWire() as sms:
|
|
146
|
+
await sms.messages.send("+251911234567", "Hello")
|
|
147
|
+
async for m in sms.messages.list_all(status="failed"):
|
|
148
|
+
...
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Campaigns
|
|
152
|
+
|
|
153
|
+
A campaign is a send with a name: a message, who it goes to, who to leave out, and when. It is made as a
|
|
154
|
+
draft and nothing goes out until you start it, so you can check who it reaches first. Each run is an
|
|
155
|
+
ordinary batch. `marketing` needs `consent` -- that the recipients agreed to hear from you -- and
|
|
156
|
+
`stop_footer` ends every marketing message with your opt-out line, counted in its SMS parts.
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
campaign = sms.campaigns.create(
|
|
160
|
+
"Weekend sale",
|
|
161
|
+
body="Hi {{name|there}}, 20% off this weekend",
|
|
162
|
+
type="marketing",
|
|
163
|
+
consent=True,
|
|
164
|
+
audience={"group_ids": [group["id"]]},
|
|
165
|
+
exclude={"recent_days": 7},
|
|
166
|
+
stop_footer="Reply STOP to opt out",
|
|
167
|
+
)
|
|
168
|
+
print(sms.campaigns.preview(campaign["id"]))
|
|
169
|
+
sms.campaigns.send(campaign["id"])
|
|
170
|
+
results = sms.campaigns.results(campaign["id"])
|
|
171
|
+
print(results["delivered"], results["replies"], results["opt_outs"], results["parts"])
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Webhooks
|
|
175
|
+
|
|
176
|
+
Status updates (`message.status`) and replies (`message.received`) are POSTed to the project's webhook URL, signed with `X-SMSWire-Signature`. Verify the **raw** body:
|
|
177
|
+
|
|
178
|
+
```python
|
|
179
|
+
from smswire import SignatureError
|
|
180
|
+
|
|
181
|
+
# Flask
|
|
182
|
+
@app.post("/hooks/smswire")
|
|
183
|
+
def smswire_hook():
|
|
184
|
+
try:
|
|
185
|
+
event = sms.webhooks.verify(request.get_data(), request.headers.get("X-SMSWire-Signature"))
|
|
186
|
+
except SignatureError:
|
|
187
|
+
return "", 400
|
|
188
|
+
if event["event"] == "message.status":
|
|
189
|
+
mark_order(event["metadata"], event["status"])
|
|
190
|
+
elif event["event"] == "message.received":
|
|
191
|
+
handle_reply(event["from"], event["body"])
|
|
192
|
+
return "", 200
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Django: `request.body` and `request.headers["X-SMSWire-Signature"]`. FastAPI: `await request.body()`. Without a client: `verify_webhook(raw_body, signature, secret)`. The same event can arrive twice; make the handler idempotent.
|
|
196
|
+
|
|
197
|
+
## Errors and retries
|
|
198
|
+
|
|
199
|
+
Failed calls raise `SMSWireError` with `status` (None when no answer came), `code` (`invalid_request`, `invalid_api_key`, `rate_limited`, `daily_limit_reached`, `network_error`, `timeout`, …), `message` and `retry_after`.
|
|
200
|
+
|
|
201
|
+
The SDK retries (default `max_retries=2`, waiting `Retry-After` or backing off):
|
|
202
|
+
|
|
203
|
+
- reads, updates, deletes and `messages.send` (it carries an idempotency key): on 429, 5xx and network errors;
|
|
204
|
+
- `send_bulk`, `otp.send`, `otp.verify`, `schedules.create`: on 429 only, since a 5xx or a dropped connection may already have done the work.
|
|
205
|
+
|
|
206
|
+
A `Retry-After` longer than `max_retry_wait` (60 s, e.g. a daily limit) raises at once.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
smswire/__init__.py,sha256=vLrngB9TXMLU2GVWl8kUTs8CUlmoUqcWae1ZHImIzeo,457
|
|
2
|
+
smswire/_client.py,sha256=U9P741uiJtCBN4Z2oePw5SK6-17mHk8RFC-J7mSPxIc,25061
|
|
3
|
+
smswire/_http.py,sha256=9PQ88OUyJyYF_FhRlyNl6IzerLDzcb6IRwBGxXp7ZU0,4546
|
|
4
|
+
smswire/_requests.py,sha256=a1xPMmwS-1Y2T1QJz_-kjplTPMbVilWJCtJq_WkbkEc,5667
|
|
5
|
+
smswire/_version.py,sha256=Zn1KFblwuFHiDRdRAiRnDBRkbPttWh44jKa5zG2ov0E,22
|
|
6
|
+
smswire/errors.py,sha256=jPFESvX_KmZ9fR3270ZTZR4q3hfWK0-SFKGaeUYRCCA,973
|
|
7
|
+
smswire/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
smswire/types.py,sha256=ayB5k3ehksgyVH0gtQnISIB57G_pvLTdxuBnAUcChAc,5747
|
|
9
|
+
smswire/webhooks.py,sha256=T3WFhyEZp_LEN5naIJVlatlhrdkS63OLGt3Mk0FR1_I,2094
|
|
10
|
+
smswire-0.2.0.dist-info/METADATA,sha256=sgt0McER-1XSPzB4KRRRkvFiBE2IBCnf4O7RuPJyeZ4,7473
|
|
11
|
+
smswire-0.2.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
12
|
+
smswire-0.2.0.dist-info/licenses/LICENSE,sha256=ek5xrEWIB0QyW8ZnTo6i-juo5YEVl2aEtmdn5L0U4HY,1064
|
|
13
|
+
smswire-0.2.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 SMSWire
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|