Metadata-Version: 2.5
Name: voicemaildetector
Version: 0.1.0
Summary: Python client for the Voicemail Detector API: answering machine detection for live calls and recordings.
Project-URL: Homepage, https://voicemaildetector.com
Project-URL: Documentation, https://voicemaildetector.com/docs
Project-URL: Source, https://github.com/voicemaildetector/voicemaildetector-python
Author-email: Voicemail Detector <contact@voicemaildetector.com>
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: amd,answering machine detection,asterisk,dialer,telephony,telnyx,twilio,voice ai,voicemail,voicemail detection
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Communications :: Telephony
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: websockets>=14.1
Provides-Extra: test
Requires-Dist: pytest>=8; extra == 'test'
Description-Content-Type: text/markdown

# Voicemail Detector for Python

Answering machine detection for outbound calls and voice AI. Send the callee's audio and get back
`human`, `machine` or `unknown`, usually within 2.5 to 4.5 seconds of the answer. The call never
waits for the verdict: your agent or voice AI talks from the first second, and you hang up on
machines when the verdict arrives.

- Live calls over a WebSocket, recordings over REST, with a regular and an asyncio client.
- 8 kHz mono G.711 µ-law, A-law or 16-bit PCM, the way calls cross real trunks.
- Typed errors with a `retryable` flag. One dependency (`websockets`). Python 3.10 or newer.

Get an API key at [voicemaildetector.com](https://voicemaildetector.com/dashboard/key): 500 calls
a month are free.

## Install

```sh
pip install voicemaildetector
```

Until the first release reaches PyPI, install it from GitHub:
`pip install git+https://github.com/voicemaildetector/voicemaildetector-python`.

## A live call

Open one stream per call when it is answered, send the callee's audio as it arrives and end the
stream when the call is over. The connection opens in the background; frames sent before it does
are held and sent once it opens.

```python
from voicemaildetector import VoicemailDetector

vd = VoicemailDetector()  # reads VD_API_KEY

def on_result(result):
    if result.verdict == "machine":
        hang_up()

with vd.stream(encoding="mulaw", on_result=on_result) as call:
    for frame in callee_audio():  # 20 ms frames
        call.send(frame)
# Leaving the block ends the stream and waits for the last results.
```

`call.verdict()` returns the first decision, or `None` if the stream ended without one;
`call.wait()` returns once the server is done with the call; iterating over `call` gives every
result as it comes. All three take an optional `timeout` in seconds.

On asyncio, `send` and `end` never block, so they fit inside a media pipeline:

```python
from voicemaildetector import AsyncVoicemailDetector

vd = AsyncVoicemailDetector()

async with vd.stream(encoding="pcm16") as call:
    async for frame in callee_audio():
        call.send(frame)
    verdict = await call.verdict()
```

A call can get two results: the `decision`, and later an `after_human` machine result when a
machine shows up after a human verdict, for example a greeting that starts after a pause. Keep
sending audio for `watch_ms` after a human verdict to get it.

## A recording

```python
result = vd.detect("call-0193.wav")
print(result.verdict)  # machine
```

`detect` takes a file path, bytes or a binary file object, up to 8 MiB. A WAV file carries its own
format; raw audio needs `encoding`.

## Options

```python
VoicemailDetector(
    api_key="vd_…",        # default: VD_API_KEY
    base_url="…",          # default: VD_API_URL, then https://api.voicemaildetector.com
    timeout=60.0,          # for detect, in seconds
    user_agent="my-dialer/2.3",
)

vd.stream(
    encoding="mulaw",          # "mulaw", "alaw" or "pcm16"; required for streams
    decision_timeout_ms=6000,  # the most audio heard before the first verdict, 1000 to 30000
    watch_ms=30000,            # how long to keep listening after a human verdict, up to 60000
    on_result=print,           # every result; a function, or a coroutine function on asyncio
    on_error=print,            # once, if the stream fails
)
```

`detect` takes the same `encoding`, `decision_timeout_ms` and `watch_ms`.

## Results

`Result(verdict="machine", stage="decision", at_ms=4500)`

| field | values |
|---|---|
| `verdict` | `human`, `machine` or `unknown` (the line stayed silent, or the audio ended first) |
| `stage` | `decision` for the first verdict, `after_human` when a later result overturns `human` |
| `at_ms` | milliseconds of audio heard, from the answer, when the result was given |

## Errors

Every failure is a `VoicemailDetectorError` with a `code`, the HTTP `status` when there was one,
and `retryable`. On a stream it is raised by `verdict()`, `wait()` and the iteration, and passed to
`on_error`. A dialer that must never depend on the detector carries on as if the verdict were
`unknown` when `on_error` is called.

| code | when | retryable |
|---|---|---|
| `unauthorized` | the API key is missing, revoked or malformed | no |
| `payment_required` | the account has no calls left | no |
| `too_many_calls` | the account is at its limit of calls at once | no |
| `busy` | the API is at capacity; the next call will likely go through | yes |
| `not_ready` | the API is starting | yes |
| `bad_request`, `bad_audio`, `too_large` | the request or its audio was refused | no |
| `connection`, `timeout` | the network failed or the API did not answer in time | yes |
| `internal` | the API failed | yes |

A refused call is refused at once and never queued, so your dialer can fall back right away.

## Examples

- [detect_file.py](examples/detect_file.py): a recording.
- [stream_wav.py](examples/stream_wav.py): a WAV played as a live call.
- [twilio_media_stream.py](examples/twilio_media_stream.py): a relay for Twilio Media Streams
  that hangs up on machines.
- [telnyx_media_stream.py](examples/telnyx_media_stream.py): the same for Telnyx.

On Asterisk or Vicidial, the [connector](https://github.com/voicemaildetector/connector) replaces
`AMD()` with one line of dialplan.

## More

- API reference: https://voicemaildetector.com/docs
- Node.js: https://github.com/voicemaildetector/voicemaildetector-node
- Security: [SECURITY.md](SECURITY.md)

Apache-2.0.
