airboxhq 0.1.0__tar.gz
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.
- airboxhq-0.1.0/.gitignore +4 -0
- airboxhq-0.1.0/LICENSE +21 -0
- airboxhq-0.1.0/PKG-INFO +91 -0
- airboxhq-0.1.0/README.md +77 -0
- airboxhq-0.1.0/pyproject.toml +21 -0
- airboxhq-0.1.0/src/airboxhq/__init__.py +6 -0
- airboxhq-0.1.0/src/airboxhq/_client.py +138 -0
airboxhq-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Fintelligence
|
|
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.
|
airboxhq-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: airboxhq
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python SDK for the Airbox API: WhatsApp and Instagram messages through one REST API.
|
|
5
|
+
Project-URL: Source, https://github.com/airboxhq/airbox-python
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Keywords: airbox,api,instagram,messaging,sdk,whatsapp
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Topic :: Communications :: Chat
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# Airbox Python SDK
|
|
16
|
+
|
|
17
|
+
WhatsApp and Instagram messages through one REST API, from Python.
|
|
18
|
+
|
|
19
|
+
> Early release (0.1.0): sending and reading messages. Media, templates, consent and channels come next.
|
|
20
|
+
|
|
21
|
+
## Install
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pip install airboxhq
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Python 3.9 or newer. No dependencies.
|
|
28
|
+
|
|
29
|
+
## Send a message
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
import os
|
|
33
|
+
|
|
34
|
+
from airboxhq import Airbox
|
|
35
|
+
|
|
36
|
+
client = Airbox(api_key=os.environ["WHATSAPP_API_KEY"])
|
|
37
|
+
|
|
38
|
+
data = client.messages.send(
|
|
39
|
+
to="+77011234567",
|
|
40
|
+
channel="whatsapp",
|
|
41
|
+
type="text",
|
|
42
|
+
text={"body": "Your order is ready"},
|
|
43
|
+
idempotency_key="order-1042",
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
print(data)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
A test key sends to the simulator, so you can try every call before connecting a number.
|
|
50
|
+
|
|
51
|
+
## Read a message back
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
message = client.messages.get(data["id"])
|
|
55
|
+
print(message["status"])
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## With asyncio
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
import asyncio
|
|
62
|
+
import os
|
|
63
|
+
|
|
64
|
+
from airboxhq import AsyncAirbox
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
async def main():
|
|
68
|
+
async with AsyncAirbox(api_key=os.environ["WHATSAPP_API_KEY"]) as client:
|
|
69
|
+
data = await client.messages.send(to="+77011234567", channel="whatsapp", type="text", text={"body": "Hi"})
|
|
70
|
+
print(data)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
asyncio.run(main())
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Errors
|
|
77
|
+
|
|
78
|
+
A refused request raises `airboxhq.AirboxError` with the API's own code and the request id:
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
from airboxhq import AirboxError
|
|
82
|
+
|
|
83
|
+
try:
|
|
84
|
+
client.messages.send(to="+77011234567", channel="whatsapp", type="text", text={"body": "Hi"})
|
|
85
|
+
except AirboxError as error:
|
|
86
|
+
print(error.code, error.request_id)
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT
|
airboxhq-0.1.0/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Airbox Python SDK
|
|
2
|
+
|
|
3
|
+
WhatsApp and Instagram messages through one REST API, from Python.
|
|
4
|
+
|
|
5
|
+
> Early release (0.1.0): sending and reading messages. Media, templates, consent and channels come next.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install airboxhq
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Python 3.9 or newer. No dependencies.
|
|
14
|
+
|
|
15
|
+
## Send a message
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
import os
|
|
19
|
+
|
|
20
|
+
from airboxhq import Airbox
|
|
21
|
+
|
|
22
|
+
client = Airbox(api_key=os.environ["WHATSAPP_API_KEY"])
|
|
23
|
+
|
|
24
|
+
data = client.messages.send(
|
|
25
|
+
to="+77011234567",
|
|
26
|
+
channel="whatsapp",
|
|
27
|
+
type="text",
|
|
28
|
+
text={"body": "Your order is ready"},
|
|
29
|
+
idempotency_key="order-1042",
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
print(data)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
A test key sends to the simulator, so you can try every call before connecting a number.
|
|
36
|
+
|
|
37
|
+
## Read a message back
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
message = client.messages.get(data["id"])
|
|
41
|
+
print(message["status"])
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## With asyncio
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
import asyncio
|
|
48
|
+
import os
|
|
49
|
+
|
|
50
|
+
from airboxhq import AsyncAirbox
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
async def main():
|
|
54
|
+
async with AsyncAirbox(api_key=os.environ["WHATSAPP_API_KEY"]) as client:
|
|
55
|
+
data = await client.messages.send(to="+77011234567", channel="whatsapp", type="text", text={"body": "Hi"})
|
|
56
|
+
print(data)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
asyncio.run(main())
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Errors
|
|
63
|
+
|
|
64
|
+
A refused request raises `airboxhq.AirboxError` with the API's own code and the request id:
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from airboxhq import AirboxError
|
|
68
|
+
|
|
69
|
+
try:
|
|
70
|
+
client.messages.send(to="+77011234567", channel="whatsapp", type="text", text={"body": "Hi"})
|
|
71
|
+
except AirboxError as error:
|
|
72
|
+
print(error.code, error.request_id)
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## License
|
|
76
|
+
|
|
77
|
+
MIT
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "airboxhq"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Python SDK for the Airbox API: WhatsApp and Instagram messages through one REST API."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.9"
|
|
9
|
+
keywords = ["airbox", "whatsapp", "instagram", "messaging", "api", "sdk"]
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Programming Language :: Python :: 3",
|
|
12
|
+
"Operating System :: OS Independent",
|
|
13
|
+
"Topic :: Communications :: Chat",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
[project.urls]
|
|
17
|
+
Source = "https://github.com/airboxhq/airbox-python"
|
|
18
|
+
|
|
19
|
+
[build-system]
|
|
20
|
+
requires = ["hatchling"]
|
|
21
|
+
build-backend = "hatchling.build"
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
import urllib.error
|
|
7
|
+
import urllib.parse
|
|
8
|
+
import urllib.request
|
|
9
|
+
from typing import Any, Dict, Optional
|
|
10
|
+
|
|
11
|
+
DEFAULT_BASE_URL = "https://crm.fintelligence.kz/api/v1"
|
|
12
|
+
VERSION = "0.1.0"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class AirboxError(Exception):
|
|
16
|
+
"""A request the API refused, or one that never reached it.
|
|
17
|
+
|
|
18
|
+
``code`` is the API's own code, such as ``outside_window``, and ``request_id`` is what to quote to support.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
def __init__(self, code: str, message: str, status: int = 0, request_id: Optional[str] = None) -> None:
|
|
22
|
+
super().__init__(message)
|
|
23
|
+
self.code = code
|
|
24
|
+
self.status = status
|
|
25
|
+
self.request_id = request_id
|
|
26
|
+
|
|
27
|
+
def __str__(self) -> str:
|
|
28
|
+
suffix = f" ({self.request_id})" if self.request_id else ""
|
|
29
|
+
return f"{self.code}: {super().__str__()}{suffix}"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class Messages:
|
|
33
|
+
"""Send a message to a customer on WhatsApp or Instagram, and read one back with its status."""
|
|
34
|
+
|
|
35
|
+
def __init__(self, client: "Airbox") -> None:
|
|
36
|
+
self._client = client
|
|
37
|
+
|
|
38
|
+
def send(self, *, idempotency_key: Optional[str] = None, **params: Any) -> Dict[str, Any]:
|
|
39
|
+
"""Send a text, a template, an image or a voice message.
|
|
40
|
+
|
|
41
|
+
Pass the body of ``POST /v1/messages`` as keyword arguments: ``to``, ``channel``, ``type`` and its content.
|
|
42
|
+
The same ``idempotency_key`` never sends twice.
|
|
43
|
+
"""
|
|
44
|
+
headers = {"Idempotency-Key": idempotency_key} if idempotency_key else {}
|
|
45
|
+
return self._client.request("POST", "/messages", params, headers)
|
|
46
|
+
|
|
47
|
+
def get(self, id: str) -> Dict[str, Any]:
|
|
48
|
+
"""A message by its id, with its current status."""
|
|
49
|
+
return self._client.request("GET", "/messages/" + urllib.parse.quote(id, safe=""))
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class Airbox:
|
|
53
|
+
"""The Airbox API client. Make it with your API key, then call its resources: ``client.messages.send(...)``."""
|
|
54
|
+
|
|
55
|
+
def __init__(self, api_key: Optional[str] = None, *, base_url: Optional[str] = None, timeout: float = 30) -> None:
|
|
56
|
+
key = api_key or os.environ.get("AIRBOX_API_KEY")
|
|
57
|
+
if not key:
|
|
58
|
+
raise ValueError("An API key is required.")
|
|
59
|
+
self._api_key = key
|
|
60
|
+
self._base_url = (base_url or os.environ.get("AIRBOX_BASE_URL") or DEFAULT_BASE_URL).rstrip("/")
|
|
61
|
+
self._timeout = timeout
|
|
62
|
+
self.messages = Messages(self)
|
|
63
|
+
|
|
64
|
+
def request(
|
|
65
|
+
self,
|
|
66
|
+
method: str,
|
|
67
|
+
path: str,
|
|
68
|
+
body: Optional[Dict[str, Any]] = None,
|
|
69
|
+
headers: Optional[Dict[str, str]] = None,
|
|
70
|
+
) -> Dict[str, Any]:
|
|
71
|
+
"""Send one request and return the decoded answer. Raises ``AirboxError`` when the API refuses it."""
|
|
72
|
+
data = json.dumps(body, ensure_ascii=False).encode() if body is not None else None
|
|
73
|
+
request = urllib.request.Request(self._base_url + path, data=data, method=method)
|
|
74
|
+
request.add_header("Authorization", "Bearer " + self._api_key)
|
|
75
|
+
request.add_header("Accept", "application/json")
|
|
76
|
+
request.add_header("User-Agent", "airbox-python/" + VERSION)
|
|
77
|
+
if data is not None:
|
|
78
|
+
request.add_header("Content-Type", "application/json")
|
|
79
|
+
for name, value in (headers or {}).items():
|
|
80
|
+
request.add_header(name, value)
|
|
81
|
+
|
|
82
|
+
try:
|
|
83
|
+
with urllib.request.urlopen(request, timeout=self._timeout) as response:
|
|
84
|
+
return _decode(response.read(), response.status)
|
|
85
|
+
except urllib.error.HTTPError as refused:
|
|
86
|
+
answer = _decode(refused.read(), refused.code, refused=True)
|
|
87
|
+
error = answer.get("error") if isinstance(answer.get("error"), dict) else {}
|
|
88
|
+
raise AirboxError(
|
|
89
|
+
str(error.get("code") or f"http_{refused.code}"),
|
|
90
|
+
str(error.get("message") or "The request failed."),
|
|
91
|
+
refused.code,
|
|
92
|
+
error.get("request_id"),
|
|
93
|
+
) from None
|
|
94
|
+
except urllib.error.URLError as unreachable:
|
|
95
|
+
raise AirboxError("network_error", str(unreachable.reason)) from None
|
|
96
|
+
|
|
97
|
+
def __enter__(self) -> "Airbox":
|
|
98
|
+
return self
|
|
99
|
+
|
|
100
|
+
def __exit__(self, *exc: object) -> None:
|
|
101
|
+
return None
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class _AsyncMessages:
|
|
105
|
+
def __init__(self, messages: Messages) -> None:
|
|
106
|
+
self._messages = messages
|
|
107
|
+
|
|
108
|
+
async def send(self, *, idempotency_key: Optional[str] = None, **params: Any) -> Dict[str, Any]:
|
|
109
|
+
return await asyncio.to_thread(self._messages.send, idempotency_key=idempotency_key, **params)
|
|
110
|
+
|
|
111
|
+
async def get(self, id: str) -> Dict[str, Any]:
|
|
112
|
+
return await asyncio.to_thread(self._messages.get, id)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class AsyncAirbox:
|
|
116
|
+
"""The same client for asyncio: ``async with AsyncAirbox(api_key=...) as client``."""
|
|
117
|
+
|
|
118
|
+
def __init__(self, api_key: Optional[str] = None, *, base_url: Optional[str] = None, timeout: float = 30) -> None:
|
|
119
|
+
self._client = Airbox(api_key, base_url=base_url, timeout=timeout)
|
|
120
|
+
self.messages = _AsyncMessages(self._client.messages)
|
|
121
|
+
|
|
122
|
+
async def __aenter__(self) -> "AsyncAirbox":
|
|
123
|
+
return self
|
|
124
|
+
|
|
125
|
+
async def __aexit__(self, *exc: object) -> None:
|
|
126
|
+
return None
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def _decode(raw: bytes, status: int, refused: bool = False) -> Dict[str, Any]:
|
|
130
|
+
if not raw:
|
|
131
|
+
return {}
|
|
132
|
+
try:
|
|
133
|
+
decoded = json.loads(raw)
|
|
134
|
+
except ValueError:
|
|
135
|
+
if refused:
|
|
136
|
+
return {}
|
|
137
|
+
raise AirboxError("invalid_response", "The API did not answer with JSON.", status) from None
|
|
138
|
+
return decoded if isinstance(decoded, dict) else {"data": decoded}
|