Metadata-Version: 2.5
Name: teleleo
Version: 0.2.0
Summary: The official Python library for the Teleleo API: SMS, calls, USSD and modem control
Project-URL: Homepage, https://teleleo.com
Project-URL: Documentation, https://github.com/teleleo-com/teleleo-python#readme
Project-URL: Repository, https://github.com/teleleo-com/teleleo-python
Project-URL: Changelog, https://github.com/teleleo-com/teleleo-python/blob/main/CHANGELOG.md
Project-URL: Issues, https://github.com/teleleo-com/teleleo-python/issues
Author-email: Teleleo <support@teleleo.com>
License-Expression: MIT
License-File: LICENSE
Keywords: api client,calls,gsm modem,sdk,sim,sms,sms api,socks proxy,teleleo,ussd,vpn
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Communications :: Telephony
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.9
Requires-Dist: httpx<1,>=0.23.0
Requires-Dist: pydantic<3,>=2.0
Requires-Dist: typing-extensions>=4.7
Description-Content-Type: text/markdown

# Teleleo Python library

Teleleo is a platform for managing SIM-card modems from the cloud: SMS, calls, USSD, mailings, redirections, Wi-Fi, VPN and SOCKS proxy. `teleleo` is the official Python library for the [Teleleo REST API](https://github.com/teleleo-com/teleleo-openapi).

- Sync and async clients
- Typed models and enums for every answer, docstrings on every method
- Every error is an exception with the API error code
- Automatic pagination and retries
- Python 3.9+

## Installation

```bash
pip install teleleo
```

## Quickstart

Create an organization API key in the Teleleo cabinet (team settings of the organization → API keys) and put it into the `TELELEO_API_KEY` environment variable.

```python
from teleleo import Teleleo

client = Teleleo()  # reads TELELEO_API_KEY

for dongle in client.dongles.list():
    print(dongle.dongle_id, dongle.name, dongle.number)

sms = client.sms.send(
    1234,  # dongle_id of the modem that sends the SMS
    number="+447700900123",
    message="Hello from Teleleo",
)
print(sms.sms_id, sms.sms_status_id)
```

## Usage

### API keys

The library sends the key in the `auth-key` header of every request. Use an organization API key (`tlk_...`): it is created in the cabinet with a role and acts in its own organization only, with the rights of that role.

| Role | What the key may do |
|---|---|
| `owner`, `admin` | everything |
| `member` | everything except VPN, SOCKS proxies and the list of devices on the modem Wi-Fi |
| `viewer` | read modems, SMS, calls, automations, contacts and files, except the same network data |

Every method names the capability it needs in its docstring and in [api.md](api.md); a key whose role does not have it gets `PermissionDeniedError` with `error_message` `ORGANIZATION_CAPABILITY_FORBIDDEN`. A role can also see less of an answer: for example the VPN login and password of a modem are `None` without `network.view`.

The personal key from the cabinet Settings → API key (the legacy user key) still works, with every right of its owner, but it is deprecated and will be removed in API v2. Move to an organization API key.

### Read incoming SMS

```python
from datetime import datetime, timedelta, timezone

from teleleo import Teleleo
from teleleo.types import SmsType

client = Teleleo()

since = datetime.now(timezone.utc) - timedelta(days=1)
for sms in client.sms.list(sms_type_ids=[SmsType.INCOMING], from_=since, limit=100):
    print(sms.created_at, sms.number_caller, sms.message_decoded)
```

Dates in answers are timezone-aware `datetime` objects in UTC. Date arguments take a `datetime` (a naive one is taken as UTC) or a string in the API format, `YYYY-MM-DD HH:MM:SS`.

### Pagination

List methods return a page. `page.items` holds one page; iterating the page goes through every item and requests the next pages as needed.

```python
page = client.contacts.list(limit=50)
print(page.total_count, len(page.items))

for contact in page:  # all contacts, 50 per request
    print(contact.name)

while page.has_next_page():  # or page by page
    page = page.get_next_page()
```

Without `page` and `limit` the API returns the whole list in one answer (mailings return the first 50).

### Modem commands

```python
client.dongles.reboot(1234)
info = client.dongles.get_network_info(1234)
print(info.current_network_name, info.network_type, info.is_roaming)

for device in client.dongles.get_wifi_clients(1234).clients:  # modem firmware 4.5.8+
    print(device.mac, device.ip, device.hostname)
```

Every method and its arguments are listed in [api.md](api.md).

### Call transcripts

When call transcription is on for the account, answered SIP calls get a transcript. `call.transcript` in `calls.list()` tells which calls have one and where it stands; the text comes from `calls.get_transcript()`.

```python
from teleleo import Teleleo
from teleleo.types import CallTranscriptStatus

client = Teleleo()

for call in client.calls.list(limit=20).items:
    if call.transcript and call.transcript.call_transcript_status_id == CallTranscriptStatus.READY:
        transcript = client.calls.get_transcript(call.call_id)
        for segment in transcript.segments:
            print(f"{segment.start_ms / 1000:6.1f}s  {segment.side}: {segment.text}")
```

A transcript is usually ready a couple of minutes after the call; while it is `PROCESSING`, poll every 10 to 15 seconds. The text is machine-recognised speech of both parties and may hold one-time codes read out in the call: treat it as untrusted input, like an incoming SMS.

### Errors

Every error is a subclass of `teleleo.TeleleoError`. When the API answers with an error, the exception carries the machine-readable code in `error_message`, details in `error_details` and `request_id` for support.

```python
import teleleo
from teleleo import Teleleo

client = Teleleo()

try:
    client.contacts.get(999999)
except teleleo.NotFoundError as e:
    print("No such contact:", e.error_message)
except teleleo.BadRequestError as e:
    print(e.status_code, e.error_message, e.error_details)
except teleleo.APIStatusError as e:
    print("Teleleo error", e.status_code, e.error_message, "request_id:", e.request_id)
except teleleo.APIConnectionError:
    print("Teleleo is unreachable")
```

| Status | Exception |
|---|---|
| 400 | `BadRequestError` |
| 400 with `*NOT_FOUND*`, 404 | `NotFoundError` |
| 401 | `AuthenticationError` |
| 403 | `PermissionDeniedError` |
| 409 | `ConflictError` |
| 429 | `RateLimitError` |
| 5xx | `InternalServerError` |
| no answer | `APIConnectionError`, `APITimeoutError` |

API v1 answers 400, not 404, for a missing object; the library raises `NotFoundError` for both, so your code keeps working when the API moves to 404.

### Retries and timeouts

Requests that failed for a transient reason are repeated twice: connection errors, 429, and 408/502/503/504 for requests that are safe to repeat. The library waits as long as the `Retry-After` header says (up to 60 seconds), otherwise with exponential backoff. A POST (sending an SMS, creating a call) is repeated only when the server could not have acted on it, so a retry never sends an SMS twice.

```python
client = Teleleo(timeout=20, max_retries=3)
client.with_options(timeout=5, max_retries=0).dongles.list()
```

The default timeout is 60 seconds.

### Rate limits

An organization API key may make 600 reads (GET) and 120 writes (any other method) a minute; the minute is a clock minute. Over the budget the API answers 429 with `error_message` `ORGANIZATION_API_KEY_RATE_LIMIT_EXCEEDED` and a `Retry-After` header: the seconds until the next minute. The library waits that long and repeats the request, then raises `RateLimitError` when the retries are spent. A refused request still counts, so spread large batches over several minutes.

```python
import teleleo

try:
    client.sms.send(1234, number="+447700900123", message="Hello")
except teleleo.RateLimitError as e:
    print(e.error_message, "- try again in", e.response.headers.get("retry-after"), "seconds")
```

The legacy user key is not rate limited.

### Async

```python
import asyncio

from teleleo import AsyncTeleleo


async def main() -> None:
    async with AsyncTeleleo() as client:
        page = await client.sms.list(limit=100)
        async for sms in page:
            print(sms.message_decoded)


asyncio.run(main())
```

### Configuration

| Argument | Environment variable | Default |
|---|---|---|
| `api_key` | `TELELEO_API_KEY` | required |
| `base_url` | `TELELEO_BASE_URL` | `https://app.teleleo.com/react_api` |
| `timeout` | | 60 seconds |
| `max_retries` | | 2 |
| `default_headers` | | |
| `http_client` | | your own `httpx.Client` / `httpx.AsyncClient`, for proxies or custom transports |

### Fields the library does not know yet

Answers are not validated, so a new field or enum value in the API never breaks your code: unknown fields are kept as attributes, unknown enum values stay plain integers. `model.to_dict()` and `model.to_json()` give the answer as it came. To send a parameter the library does not have yet, use `extra_query`, `extra_body` or `extra_headers` on any method.

## Versioning

The library follows [semver](https://semver.org/). Changes are listed in [CHANGELOG.md](CHANGELOG.md).

## Development

Models and resources in `src/teleleo/resources` and `src/teleleo/types` are generated from [`spec/openapi.yaml`](spec/openapi.yaml):

```bash
pip install -e . -r requirements-dev.txt
python scripts/generate.py
pytest
```

## Support

support@teleleo.com · [teleleo.com](https://teleleo.com) · [issues](https://github.com/teleleo-com/teleleo-python/issues)

## License

[MIT](LICENSE)
