zapz 0.1.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.
- zapz/__init__.py +56 -0
- zapz/_core.py +99 -0
- zapz/_errors.py +88 -0
- zapz/_generated/__init__.py +2 -0
- zapz/_generated/resources.py +321 -0
- zapz/_generated/types.py +616 -0
- zapz/_version.py +1 -0
- zapz/models.py +39 -0
- zapz/py.typed +0 -0
- zapz/webhooks.py +51 -0
- zapz-0.1.0.dist-info/METADATA +44 -0
- zapz-0.1.0.dist-info/RECORD +14 -0
- zapz-0.1.0.dist-info/WHEEL +5 -0
- zapz-0.1.0.dist-info/top_level.txt +1 -0
zapz/__init__.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""Official Python SDK for the ZapZ WhatsApp API.
|
|
2
|
+
|
|
3
|
+
from zapz import ZapZ
|
|
4
|
+
zapz = ZapZ(api_key="sk_live_...")
|
|
5
|
+
zapz.messages.send_text("INSTANCE_ID", {"to": "5511999999999", "text": "Olá!"})
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from typing import Optional
|
|
10
|
+
|
|
11
|
+
from ._core import DEFAULT_BASE_URL, ZapZCore
|
|
12
|
+
from ._errors import (
|
|
13
|
+
APIConnectionError,
|
|
14
|
+
AuthenticationError,
|
|
15
|
+
ConflictError,
|
|
16
|
+
InternalServerError,
|
|
17
|
+
NotFoundError,
|
|
18
|
+
PermissionDeniedError,
|
|
19
|
+
RateLimitError,
|
|
20
|
+
ValidationError,
|
|
21
|
+
ZapZError,
|
|
22
|
+
)
|
|
23
|
+
from ._generated import (
|
|
24
|
+
ContactsResource,
|
|
25
|
+
GroupsResource,
|
|
26
|
+
InstancesResource,
|
|
27
|
+
MessagesResource,
|
|
28
|
+
SandboxResource,
|
|
29
|
+
WebhooksResource,
|
|
30
|
+
)
|
|
31
|
+
from ._version import VERSION
|
|
32
|
+
from .models import Message, WebhookEvent
|
|
33
|
+
from .webhooks import WebhookVerificationError, verify_webhook
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class ZapZ(ZapZCore):
|
|
37
|
+
"""ZapZ API client. ``api_key`` defaults to the ZAPZ_API_KEY environment variable."""
|
|
38
|
+
|
|
39
|
+
def __init__(self, api_key: Optional[str] = None, *, base_url: Optional[str] = None,
|
|
40
|
+
timeout: float = 30.0, max_retries: int = 2) -> None:
|
|
41
|
+
super().__init__(api_key, base_url=base_url, timeout=timeout, max_retries=max_retries)
|
|
42
|
+
self.instances = InstancesResource(self)
|
|
43
|
+
self.messages = MessagesResource(self)
|
|
44
|
+
self.contacts = ContactsResource(self)
|
|
45
|
+
self.groups = GroupsResource(self)
|
|
46
|
+
self.webhooks = WebhooksResource(self)
|
|
47
|
+
self.sandbox = SandboxResource(self)
|
|
48
|
+
|
|
49
|
+
verify_webhook = staticmethod(verify_webhook)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
__all__ = [
|
|
53
|
+
"ZapZ", "VERSION", "DEFAULT_BASE_URL", "Message", "WebhookEvent", "verify_webhook",
|
|
54
|
+
"ZapZError", "APIConnectionError", "ValidationError", "AuthenticationError", "PermissionDeniedError",
|
|
55
|
+
"NotFoundError", "ConflictError", "RateLimitError", "InternalServerError", "WebhookVerificationError",
|
|
56
|
+
]
|
zapz/_core.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import random
|
|
6
|
+
import socket
|
|
7
|
+
import time
|
|
8
|
+
import uuid
|
|
9
|
+
import urllib.error
|
|
10
|
+
import urllib.request
|
|
11
|
+
from typing import Any, Dict, Optional
|
|
12
|
+
from urllib.parse import urlencode
|
|
13
|
+
|
|
14
|
+
from ._errors import APIConnectionError, ZapZError, error_from_response
|
|
15
|
+
from ._version import VERSION
|
|
16
|
+
|
|
17
|
+
DEFAULT_BASE_URL = "https://api.zapz.dev"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _backoff(attempt: int) -> float:
|
|
21
|
+
return min(8.0, 0.5 * 2 ** attempt) * (0.75 + random.random() * 0.5)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class ZapZCore:
|
|
25
|
+
"""HTTP layer shared by every resource (stdlib only)."""
|
|
26
|
+
|
|
27
|
+
def __init__(self, api_key: Optional[str] = None, *, base_url: Optional[str] = None,
|
|
28
|
+
timeout: float = 30.0, max_retries: int = 2) -> None:
|
|
29
|
+
key = api_key or os.environ.get("ZAPZ_API_KEY")
|
|
30
|
+
if not key:
|
|
31
|
+
raise ZapZError("Missing API key: pass api_key=... or set ZAPZ_API_KEY.")
|
|
32
|
+
self._api_key = key
|
|
33
|
+
url = (base_url or os.environ.get("ZAPZ_BASE_URL") or DEFAULT_BASE_URL).rstrip("/")
|
|
34
|
+
self.base_url = url[:-3] if url.endswith("/v1") else url
|
|
35
|
+
self.timeout = timeout
|
|
36
|
+
self.max_retries = max_retries
|
|
37
|
+
|
|
38
|
+
@property
|
|
39
|
+
def is_sandbox(self) -> bool:
|
|
40
|
+
"""True for sk_test_ keys (sandbox: nothing reaches WhatsApp)."""
|
|
41
|
+
return self._api_key.startswith("sk_test_")
|
|
42
|
+
|
|
43
|
+
def request(self, method: str, path: str, *, body: Any = None, query: Optional[Dict[str, Any]] = None,
|
|
44
|
+
auto_client_message_id: bool = False, timeout: Optional[float] = None,
|
|
45
|
+
max_retries: Optional[int] = None) -> Any:
|
|
46
|
+
if auto_client_message_id and isinstance(body, dict) and not body.get("clientMessageId"):
|
|
47
|
+
body = {**body, "clientMessageId": str(uuid.uuid4())}
|
|
48
|
+
url = self.base_url + "/v1" + path
|
|
49
|
+
q = {k: ("true" if v is True else "false" if v is False else v) for k, v in (query or {}).items() if v is not None}
|
|
50
|
+
if q:
|
|
51
|
+
url += "?" + urlencode(q)
|
|
52
|
+
data = json.dumps(body).encode() if body is not None else None
|
|
53
|
+
headers = {
|
|
54
|
+
"Authorization": f"Bearer {self._api_key}",
|
|
55
|
+
"Accept": "application/json",
|
|
56
|
+
"User-Agent": f"zapz-python/{VERSION}",
|
|
57
|
+
}
|
|
58
|
+
if data is not None:
|
|
59
|
+
headers["Content-Type"] = "application/json"
|
|
60
|
+
# a retry is safe when the request is idempotent or carries an idempotency key
|
|
61
|
+
idempotent = method in ("GET", "DELETE") or bool(isinstance(body, dict) and body.get("clientMessageId"))
|
|
62
|
+
retries = self.max_retries if max_retries is None else max_retries
|
|
63
|
+
|
|
64
|
+
attempt = 0
|
|
65
|
+
while True:
|
|
66
|
+
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
|
67
|
+
try:
|
|
68
|
+
with urllib.request.urlopen(req, timeout=timeout or self.timeout) as res: # noqa: S310
|
|
69
|
+
return _parse(res.read())
|
|
70
|
+
except urllib.error.HTTPError as e:
|
|
71
|
+
status = e.code
|
|
72
|
+
payload = _parse(e.read())
|
|
73
|
+
err = error_from_response(status, payload, e.headers.get("Retry-After"))
|
|
74
|
+
refused = status in (429, 503) # nothing was processed
|
|
75
|
+
transient = status in (502, 504)
|
|
76
|
+
if attempt < retries and (refused or (transient and idempotent)):
|
|
77
|
+
wait = err.retry_after if err.retry_after else _backoff(attempt)
|
|
78
|
+
if wait <= 60:
|
|
79
|
+
time.sleep(wait)
|
|
80
|
+
attempt += 1
|
|
81
|
+
continue
|
|
82
|
+
raise err from None
|
|
83
|
+
except (urllib.error.URLError, socket.timeout, ConnectionError, TimeoutError) as e:
|
|
84
|
+
if idempotent and attempt < retries:
|
|
85
|
+
time.sleep(_backoff(attempt))
|
|
86
|
+
attempt += 1
|
|
87
|
+
continue
|
|
88
|
+
reason = getattr(e, "reason", e)
|
|
89
|
+
raise APIConnectionError(f"Connection error: {reason}") from None
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _parse(raw: bytes) -> Any:
|
|
93
|
+
if not raw:
|
|
94
|
+
return None
|
|
95
|
+
text = raw.decode("utf-8", errors="replace")
|
|
96
|
+
try:
|
|
97
|
+
return json.loads(text)
|
|
98
|
+
except ValueError:
|
|
99
|
+
return text
|
zapz/_errors.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any, Optional
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class ZapZError(Exception):
|
|
7
|
+
"""Base class for every error raised by the SDK."""
|
|
8
|
+
|
|
9
|
+
def __init__(self, message: str, *, status: Optional[int] = None, code: Optional[str] = None,
|
|
10
|
+
retry_after: Optional[float] = None, body: Any = None) -> None:
|
|
11
|
+
super().__init__(message)
|
|
12
|
+
self.message = message
|
|
13
|
+
#: HTTP status (None for network errors)
|
|
14
|
+
self.status = status
|
|
15
|
+
#: machine-readable code from the API, e.g. ``INSTANCE_NOT_CONNECTED``
|
|
16
|
+
self.code = code
|
|
17
|
+
#: seconds to wait before retrying, when the API says so
|
|
18
|
+
self.retry_after = retry_after
|
|
19
|
+
self.body = body
|
|
20
|
+
|
|
21
|
+
def __str__(self) -> str:
|
|
22
|
+
prefix = f"[{self.status}{' ' + self.code if self.code else ''}] " if self.status else ""
|
|
23
|
+
return prefix + self.message
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class APIConnectionError(ZapZError):
|
|
27
|
+
"""Could not reach the API (DNS, TLS, timeout, connection reset)."""
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ValidationError(ZapZError):
|
|
31
|
+
"""400 / 422: the request is invalid."""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class AuthenticationError(ZapZError):
|
|
35
|
+
"""401: missing, revoked or wrong-environment API key."""
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class PermissionDeniedError(ZapZError):
|
|
39
|
+
"""403: the key lacks a scope, the IP is not allowed or the account is suspended."""
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class NotFoundError(ZapZError):
|
|
43
|
+
"""404"""
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class ConflictError(ZapZError):
|
|
47
|
+
"""409: e.g. the instance is not connected."""
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class RateLimitError(ZapZError):
|
|
51
|
+
"""429: rate limit or anti-ban pacing. See ``retry_after``."""
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class InternalServerError(ZapZError):
|
|
55
|
+
"""5xx"""
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def error_from_response(status: int, body: Any, retry_after_header: Optional[str]) -> ZapZError:
|
|
59
|
+
data = body if isinstance(body, dict) else {}
|
|
60
|
+
msg = data.get("message")
|
|
61
|
+
if isinstance(msg, list):
|
|
62
|
+
msg = "; ".join(str(m) for m in msg)
|
|
63
|
+
message = str(msg) if msg else f"HTTP {status}"
|
|
64
|
+
retry_after: Optional[float] = None
|
|
65
|
+
for v in (data.get("retryAfter"), retry_after_header):
|
|
66
|
+
try:
|
|
67
|
+
if v is not None:
|
|
68
|
+
retry_after = float(v)
|
|
69
|
+
break
|
|
70
|
+
except (TypeError, ValueError):
|
|
71
|
+
pass
|
|
72
|
+
code = data.get("error") if isinstance(data.get("error"), str) else None
|
|
73
|
+
kw = dict(status=status, code=code, retry_after=retry_after, body=body)
|
|
74
|
+
if status in (400, 422):
|
|
75
|
+
return ValidationError(message, **kw)
|
|
76
|
+
if status == 401:
|
|
77
|
+
return AuthenticationError(message, **kw)
|
|
78
|
+
if status == 403:
|
|
79
|
+
return PermissionDeniedError(message, **kw)
|
|
80
|
+
if status == 404:
|
|
81
|
+
return NotFoundError(message, **kw)
|
|
82
|
+
if status == 409:
|
|
83
|
+
return ConflictError(message, **kw)
|
|
84
|
+
if status == 429:
|
|
85
|
+
return RateLimitError(message, **kw)
|
|
86
|
+
if status >= 500:
|
|
87
|
+
return InternalServerError(message, **kw)
|
|
88
|
+
return ZapZError(message, **kw)
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
# Generated by generator/generate.py from openapi.json. Do not edit.
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from typing import TYPE_CHECKING, Any, Dict, List, Optional
|
|
5
|
+
from urllib.parse import quote
|
|
6
|
+
|
|
7
|
+
from .types import * # noqa: F401,F403
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from .._core import ZapZCore
|
|
11
|
+
from ..models import Message
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _q(v: str) -> str:
|
|
15
|
+
return quote(str(v), safe="")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class InstancesResource:
|
|
19
|
+
"""Instances"""
|
|
20
|
+
|
|
21
|
+
def __init__(self, client: "ZapZCore") -> None:
|
|
22
|
+
self._client = client
|
|
23
|
+
|
|
24
|
+
def list(self, *, external_id: Optional[str] = None, status: Optional[Literal["connecting", "qr_ready", "connected", "disconnected", "banned"]] = None, timeout: Optional[float] = None) -> Any:
|
|
25
|
+
"""List instances (``GET /v1/instances``)."""
|
|
26
|
+
return self._client.request("GET", f"/instances", query={"externalId": external_id, "status": status}, timeout=timeout)
|
|
27
|
+
|
|
28
|
+
def create(self, params: "CreateInstanceParams", *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
29
|
+
"""Create an instance (``POST /v1/instances``)."""
|
|
30
|
+
return self._client.request("POST", f"/instances", body=params, timeout=timeout)
|
|
31
|
+
|
|
32
|
+
def get(self, instance_id: str, *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
33
|
+
"""Get an instance (``GET /v1/instances/{id}``)."""
|
|
34
|
+
return self._client.request("GET", f"/instances/{_q(instance_id)}", timeout=timeout)
|
|
35
|
+
|
|
36
|
+
def update(self, instance_id: str, params: Optional["UpdateInstanceParams"] = None, *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
37
|
+
"""Rename or update an instance (``PATCH /v1/instances/{id}``)."""
|
|
38
|
+
return self._client.request("PATCH", f"/instances/{_q(instance_id)}", body=params, timeout=timeout)
|
|
39
|
+
|
|
40
|
+
def delete(self, instance_id: str, *, timeout: Optional[float] = None) -> Any:
|
|
41
|
+
"""Delete an instance (``DELETE /v1/instances/{id}``)."""
|
|
42
|
+
return self._client.request("DELETE", f"/instances/{_q(instance_id)}", timeout=timeout)
|
|
43
|
+
|
|
44
|
+
def status(self, instance_id: str, *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
45
|
+
"""Connection status (``GET /v1/instances/{id}/status``)."""
|
|
46
|
+
return self._client.request("GET", f"/instances/{_q(instance_id)}/status", timeout=timeout)
|
|
47
|
+
|
|
48
|
+
def qr(self, instance_id: str, *, timeout: Optional[float] = None) -> Any:
|
|
49
|
+
"""QR code to pair the phone (``GET /v1/instances/{id}/qr``)."""
|
|
50
|
+
return self._client.request("GET", f"/instances/{_q(instance_id)}/qr", timeout=timeout)
|
|
51
|
+
|
|
52
|
+
def request_pairing_code(self, instance_id: str, params: "PairingCodeParams", *, timeout: Optional[float] = None) -> Any:
|
|
53
|
+
"""Pair with a code instead of QR (``POST /v1/instances/{id}/pairing-code``)."""
|
|
54
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/pairing-code", body=params, timeout=timeout)
|
|
55
|
+
|
|
56
|
+
def clear_session(self, instance_id: str, *, timeout: Optional[float] = None) -> Any:
|
|
57
|
+
"""Log out and clear the session (``POST /v1/instances/{id}/session/clear``)."""
|
|
58
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/session/clear", timeout=timeout)
|
|
59
|
+
|
|
60
|
+
def health(self, instance_id: str, *, timeout: Optional[float] = None) -> Any:
|
|
61
|
+
"""Sending health and anti-ban state (``GET /v1/instances/{id}/health``)."""
|
|
62
|
+
return self._client.request("GET", f"/instances/{_q(instance_id)}/health", timeout=timeout)
|
|
63
|
+
|
|
64
|
+
def resume(self, instance_id: str, *, timeout: Optional[float] = None) -> Any:
|
|
65
|
+
"""Reconnect the session (``POST /v1/instances/{id}/resume``)."""
|
|
66
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/resume", timeout=timeout)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class MessagesResource:
|
|
70
|
+
"""Messages"""
|
|
71
|
+
|
|
72
|
+
def __init__(self, client: "ZapZCore") -> None:
|
|
73
|
+
self._client = client
|
|
74
|
+
|
|
75
|
+
def send_text(self, instance_id: str, params: "SendTextParams", *, timeout: Optional[float] = None) -> "Message":
|
|
76
|
+
"""Send text (``POST /v1/instances/{id}/send/text``)."""
|
|
77
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/text", body=params, auto_client_message_id=True, timeout=timeout)
|
|
78
|
+
|
|
79
|
+
def send_media(self, instance_id: str, params: "SendMediaParams", *, timeout: Optional[float] = None) -> "Message":
|
|
80
|
+
"""Send image, video, audio or document (``POST /v1/instances/{id}/send/media``)."""
|
|
81
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/media", body=params, auto_client_message_id=True, timeout=timeout)
|
|
82
|
+
|
|
83
|
+
def send_list(self, instance_id: str, params: "SendListParams", *, timeout: Optional[float] = None) -> "Message":
|
|
84
|
+
"""Send a list menu (``POST /v1/instances/{id}/send/list``)."""
|
|
85
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/list", body=params, auto_client_message_id=True, timeout=timeout)
|
|
86
|
+
|
|
87
|
+
def send_interactive(self, instance_id: str, params: "SendInteractiveParams", *, timeout: Optional[float] = None) -> "Message":
|
|
88
|
+
"""Send buttons (``POST /v1/instances/{id}/send/interactive``)."""
|
|
89
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/interactive", body=params, auto_client_message_id=True, timeout=timeout)
|
|
90
|
+
|
|
91
|
+
def send_sticker(self, instance_id: str, params: "SendStickerParams", *, timeout: Optional[float] = None) -> "Message":
|
|
92
|
+
"""Send a sticker (``POST /v1/instances/{id}/send/sticker``)."""
|
|
93
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/sticker", body=params, auto_client_message_id=True, timeout=timeout)
|
|
94
|
+
|
|
95
|
+
def send_location(self, instance_id: str, params: "SendLocationParams", *, timeout: Optional[float] = None) -> "Message":
|
|
96
|
+
"""Send a location (``POST /v1/instances/{id}/send/location``)."""
|
|
97
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/location", body=params, auto_client_message_id=True, timeout=timeout)
|
|
98
|
+
|
|
99
|
+
def send_contact(self, instance_id: str, params: "SendContactParams", *, timeout: Optional[float] = None) -> "Message":
|
|
100
|
+
"""Send a contact card (``POST /v1/instances/{id}/send/contact``)."""
|
|
101
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/contact", body=params, auto_client_message_id=True, timeout=timeout)
|
|
102
|
+
|
|
103
|
+
def send_reaction(self, instance_id: str, params: "SendReactionParams", *, timeout: Optional[float] = None) -> "Message":
|
|
104
|
+
"""React to a message (``POST /v1/instances/{id}/send/reaction``)."""
|
|
105
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/reaction", body=params, auto_client_message_id=True, timeout=timeout)
|
|
106
|
+
|
|
107
|
+
def send_poll(self, instance_id: str, params: "SendPollParams", *, timeout: Optional[float] = None) -> "Message":
|
|
108
|
+
"""Send a poll (``POST /v1/instances/{id}/send/poll``)."""
|
|
109
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/poll", body=params, auto_client_message_id=True, timeout=timeout)
|
|
110
|
+
|
|
111
|
+
def send_pix(self, instance_id: str, params: "SendPixParams", *, timeout: Optional[float] = None) -> "Message":
|
|
112
|
+
"""Send a Pix charge (``POST /v1/instances/{id}/send/pix``)."""
|
|
113
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/pix", body=params, auto_client_message_id=True, timeout=timeout)
|
|
114
|
+
|
|
115
|
+
def send_payment(self, instance_id: str, params: "SendPaymentParams", *, timeout: Optional[float] = None) -> "Message":
|
|
116
|
+
"""Send a payment request (``POST /v1/instances/{id}/send/payment``)."""
|
|
117
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/payment", body=params, auto_client_message_id=True, timeout=timeout)
|
|
118
|
+
|
|
119
|
+
def send_carousel(self, instance_id: str, params: "SendCarouselParams", *, timeout: Optional[float] = None) -> "Message":
|
|
120
|
+
"""Send a carousel (``POST /v1/instances/{id}/send/carousel``)."""
|
|
121
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/carousel", body=params, auto_client_message_id=True, timeout=timeout)
|
|
122
|
+
|
|
123
|
+
def send_album(self, instance_id: str, params: "SendAlbumParams", *, timeout: Optional[float] = None) -> "Message":
|
|
124
|
+
"""Send an album (``POST /v1/instances/{id}/send/album``)."""
|
|
125
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/album", body=params, auto_client_message_id=True, timeout=timeout)
|
|
126
|
+
|
|
127
|
+
def send_event(self, instance_id: str, params: "SendEventParams", *, timeout: Optional[float] = None) -> "Message":
|
|
128
|
+
"""Send an event invite (``POST /v1/instances/{id}/send/event``)."""
|
|
129
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/event", body=params, auto_client_message_id=True, timeout=timeout)
|
|
130
|
+
|
|
131
|
+
def send_group_invite(self, instance_id: str, params: "SendGroupInviteParams", *, timeout: Optional[float] = None) -> "Message":
|
|
132
|
+
"""Send a group invite card (``POST /v1/instances/{id}/send/group-invite``)."""
|
|
133
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/group-invite", body=params, auto_client_message_id=True, timeout=timeout)
|
|
134
|
+
|
|
135
|
+
def send_request_phone(self, instance_id: str, params: "SendRequestPhoneParams", *, timeout: Optional[float] = None) -> "Message":
|
|
136
|
+
"""Ask for the phone number (``POST /v1/instances/{id}/send/request-phone``)."""
|
|
137
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/request-phone", body=params, auto_client_message_id=True, timeout=timeout)
|
|
138
|
+
|
|
139
|
+
def send_share_phone(self, instance_id: str, params: "SendSharePhoneParams", *, timeout: Optional[float] = None) -> "Message":
|
|
140
|
+
"""Share your phone number (``POST /v1/instances/{id}/send/share-phone``)."""
|
|
141
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/share-phone", body=params, auto_client_message_id=True, timeout=timeout)
|
|
142
|
+
|
|
143
|
+
def send_product(self, instance_id: str, params: "SendProductParams", *, timeout: Optional[float] = None) -> "Message":
|
|
144
|
+
"""Send a catalog product (``POST /v1/instances/{id}/send/product``)."""
|
|
145
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/product", body=params, auto_client_message_id=True, timeout=timeout)
|
|
146
|
+
|
|
147
|
+
def send_order(self, instance_id: str, params: "SendOrderParams", *, timeout: Optional[float] = None) -> "Message":
|
|
148
|
+
"""Send an order (``POST /v1/instances/{id}/send/order``)."""
|
|
149
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/order", body=params, auto_client_message_id=True, timeout=timeout)
|
|
150
|
+
|
|
151
|
+
def send_catalog(self, instance_id: str, params: "SendCatalogParams", *, timeout: Optional[float] = None) -> "Message":
|
|
152
|
+
"""Send the catalog link (``POST /v1/instances/{id}/send/catalog``)."""
|
|
153
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/catalog", body=params, auto_client_message_id=True, timeout=timeout)
|
|
154
|
+
|
|
155
|
+
def forward(self, instance_id: str, params: "SendForwardParams", *, timeout: Optional[float] = None) -> "Message":
|
|
156
|
+
"""Forward a message (``POST /v1/instances/{id}/send/forward``)."""
|
|
157
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/forward", body=params, auto_client_message_id=True, timeout=timeout)
|
|
158
|
+
|
|
159
|
+
def pin(self, instance_id: str, params: "SendPinParams", *, timeout: Optional[float] = None) -> "Message":
|
|
160
|
+
"""Pin or unpin a message (``POST /v1/instances/{id}/send/pin``)."""
|
|
161
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/pin", body=params, auto_client_message_id=True, timeout=timeout)
|
|
162
|
+
|
|
163
|
+
def send_status(self, instance_id: str, params: "SendStatusParams", *, timeout: Optional[float] = None) -> "Message":
|
|
164
|
+
"""Post a status (story) (``POST /v1/instances/{id}/send/status``)."""
|
|
165
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/send/status", body=params, auto_client_message_id=True, timeout=timeout)
|
|
166
|
+
|
|
167
|
+
def list(self, instance_id: str, *, limit: Optional[int] = None, timeout: Optional[float] = None) -> List["Message"]:
|
|
168
|
+
"""List sent messages (``GET /v1/instances/{id}/messages``)."""
|
|
169
|
+
return self._client.request("GET", f"/instances/{_q(instance_id)}/messages", query={"limit": limit}, timeout=timeout)
|
|
170
|
+
|
|
171
|
+
def list_received(self, instance_id: str, *, limit: Optional[int] = None, before: Optional[str] = None, since: Optional[str] = None, chat: Optional[str] = None, q: Optional[str] = None, timeout: Optional[float] = None) -> Any:
|
|
172
|
+
"""List received messages (newest first) (``GET /v1/instances/{id}/messages/received``)."""
|
|
173
|
+
return self._client.request("GET", f"/instances/{_q(instance_id)}/messages/received", query={"limit": limit, "before": before, "since": since, "chat": chat, "q": q}, timeout=timeout)
|
|
174
|
+
|
|
175
|
+
def get(self, instance_id: str, message_id: str, *, timeout: Optional[float] = None) -> "Message":
|
|
176
|
+
"""Get a message and its status (``GET /v1/instances/{id}/messages/{messageId}``)."""
|
|
177
|
+
return self._client.request("GET", f"/instances/{_q(instance_id)}/messages/{_q(message_id)}", timeout=timeout)
|
|
178
|
+
|
|
179
|
+
def edit(self, instance_id: str, message_id: str, params: "EditMessageParams", *, timeout: Optional[float] = None) -> "Message":
|
|
180
|
+
"""Edit a sent message (``PATCH /v1/instances/{id}/messages/{messageId}``)."""
|
|
181
|
+
return self._client.request("PATCH", f"/instances/{_q(instance_id)}/messages/{_q(message_id)}", body=params, timeout=timeout)
|
|
182
|
+
|
|
183
|
+
def delete(self, instance_id: str, message_id: str, *, timeout: Optional[float] = None) -> "Message":
|
|
184
|
+
"""Delete a message for everyone (``DELETE /v1/instances/{id}/messages/{messageId}``)."""
|
|
185
|
+
return self._client.request("DELETE", f"/instances/{_q(instance_id)}/messages/{_q(message_id)}", timeout=timeout)
|
|
186
|
+
|
|
187
|
+
def mark_read(self, instance_id: str, params: "MarkReadParams", *, timeout: Optional[float] = None) -> Any:
|
|
188
|
+
"""Mark messages as read (``POST /v1/instances/{id}/messages/read``)."""
|
|
189
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/messages/read", body=params, timeout=timeout)
|
|
190
|
+
|
|
191
|
+
def send_presence(self, instance_id: str, params: "PresenceParams", *, timeout: Optional[float] = None) -> Any:
|
|
192
|
+
"""Typing / recording / online (``POST /v1/instances/{id}/presence``)."""
|
|
193
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/presence", body=params, timeout=timeout)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
class ContactsResource:
|
|
197
|
+
"""Contacts"""
|
|
198
|
+
|
|
199
|
+
def __init__(self, client: "ZapZCore") -> None:
|
|
200
|
+
self._client = client
|
|
201
|
+
|
|
202
|
+
def list(self, instance_id: str, *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
203
|
+
"""List contacts (``GET /v1/instances/{id}/contacts``)."""
|
|
204
|
+
return self._client.request("GET", f"/instances/{_q(instance_id)}/contacts", timeout=timeout)
|
|
205
|
+
|
|
206
|
+
def update(self, instance_id: str, jid: str, params: Optional[Dict[str, Any]] = None, *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
207
|
+
"""Update a contact (``PATCH /v1/instances/{id}/contacts/{jid}``)."""
|
|
208
|
+
return self._client.request("PATCH", f"/instances/{_q(instance_id)}/contacts/{_q(jid)}", body=params, timeout=timeout)
|
|
209
|
+
|
|
210
|
+
def delete(self, instance_id: str, jid: str, *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
211
|
+
"""Delete a contact (``DELETE /v1/instances/{id}/contacts/{jid}``)."""
|
|
212
|
+
return self._client.request("DELETE", f"/instances/{_q(instance_id)}/contacts/{_q(jid)}", timeout=timeout)
|
|
213
|
+
|
|
214
|
+
def check_phones(self, instance_id: str, params: Dict[str, Any], *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
215
|
+
"""Check which numbers have WhatsApp (``POST /v1/instances/{id}/contacts/check-phones``)."""
|
|
216
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/contacts/check-phones", body=params, timeout=timeout)
|
|
217
|
+
|
|
218
|
+
def profile_picture(self, instance_id: str, jid: str, *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
219
|
+
"""Profile picture URL (``GET /v1/instances/{id}/contacts/{jid}/profile-picture``)."""
|
|
220
|
+
return self._client.request("GET", f"/instances/{_q(instance_id)}/contacts/{_q(jid)}/profile-picture", timeout=timeout)
|
|
221
|
+
|
|
222
|
+
def about(self, instance_id: str, jid: str, *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
223
|
+
"""Contact "about" text (``GET /v1/instances/{id}/contacts/{jid}/status``)."""
|
|
224
|
+
return self._client.request("GET", f"/instances/{_q(instance_id)}/contacts/{_q(jid)}/status", timeout=timeout)
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
class GroupsResource:
|
|
228
|
+
"""Groups"""
|
|
229
|
+
|
|
230
|
+
def __init__(self, client: "ZapZCore") -> None:
|
|
231
|
+
self._client = client
|
|
232
|
+
|
|
233
|
+
def list(self, instance_id: str, *, participants: Optional[bool] = None, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
234
|
+
"""List groups (``GET /v1/instances/{id}/groups``)."""
|
|
235
|
+
return self._client.request("GET", f"/instances/{_q(instance_id)}/groups", query={"participants": participants}, timeout=timeout)
|
|
236
|
+
|
|
237
|
+
def create(self, instance_id: str, params: "CreateGroupParams", *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
238
|
+
"""Create a group (``POST /v1/instances/{id}/groups``)."""
|
|
239
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/groups", body=params, timeout=timeout)
|
|
240
|
+
|
|
241
|
+
def invite_info(self, instance_id: str, *, code: Optional[str] = None, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
242
|
+
"""Preview a group from an invite code (``GET /v1/instances/{id}/groups/invite-info``)."""
|
|
243
|
+
return self._client.request("GET", f"/instances/{_q(instance_id)}/groups/invite-info", query={"code": code}, timeout=timeout)
|
|
244
|
+
|
|
245
|
+
def join(self, instance_id: str, params: "JoinGroupParams", *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
246
|
+
"""Join with an invite code (``POST /v1/instances/{id}/groups/join``)."""
|
|
247
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/groups/join", body=params, timeout=timeout)
|
|
248
|
+
|
|
249
|
+
def get(self, instance_id: str, group_id: str, *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
250
|
+
"""Get a group (``GET /v1/instances/{id}/groups/{groupId}``)."""
|
|
251
|
+
return self._client.request("GET", f"/instances/{_q(instance_id)}/groups/{_q(group_id)}", timeout=timeout)
|
|
252
|
+
|
|
253
|
+
def update(self, instance_id: str, group_id: str, params: Optional["UpdateGroupParams"] = None, *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
254
|
+
"""Update subject, description, picture or settings (``PATCH /v1/instances/{id}/groups/{groupId}``)."""
|
|
255
|
+
return self._client.request("PATCH", f"/instances/{_q(instance_id)}/groups/{_q(group_id)}", body=params, timeout=timeout)
|
|
256
|
+
|
|
257
|
+
def update_participants(self, instance_id: str, group_id: str, params: "GroupParticipantsParams", *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
258
|
+
"""Add, remove, promote or demote (``POST /v1/instances/{id}/groups/{groupId}/participants``)."""
|
|
259
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/groups/{_q(group_id)}/participants", body=params, timeout=timeout)
|
|
260
|
+
|
|
261
|
+
def invite_link(self, instance_id: str, group_id: str, *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
262
|
+
"""Invite link (``GET /v1/instances/{id}/groups/{groupId}/invite``)."""
|
|
263
|
+
return self._client.request("GET", f"/instances/{_q(instance_id)}/groups/{_q(group_id)}/invite", timeout=timeout)
|
|
264
|
+
|
|
265
|
+
def revoke_invite(self, instance_id: str, group_id: str, *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
266
|
+
"""Revoke the invite link (``POST /v1/instances/{id}/groups/{groupId}/invite/revoke``)."""
|
|
267
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/groups/{_q(group_id)}/invite/revoke", timeout=timeout)
|
|
268
|
+
|
|
269
|
+
def leave(self, instance_id: str, group_id: str, *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
270
|
+
"""Leave the group (``POST /v1/instances/{id}/groups/{groupId}/leave``)."""
|
|
271
|
+
return self._client.request("POST", f"/instances/{_q(instance_id)}/groups/{_q(group_id)}/leave", timeout=timeout)
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
class WebhooksResource:
|
|
275
|
+
"""Webhooks"""
|
|
276
|
+
|
|
277
|
+
def __init__(self, client: "ZapZCore") -> None:
|
|
278
|
+
self._client = client
|
|
279
|
+
|
|
280
|
+
def list(self, *, timeout: Optional[float] = None) -> List[Dict[str, Any]]:
|
|
281
|
+
"""List webhooks (``GET /v1/webhooks``)."""
|
|
282
|
+
return self._client.request("GET", f"/webhooks", timeout=timeout)
|
|
283
|
+
|
|
284
|
+
def create(self, params: "CreateWebhookParams", *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
285
|
+
"""Create a webhook (``POST /v1/webhooks``)."""
|
|
286
|
+
return self._client.request("POST", f"/webhooks", body=params, timeout=timeout)
|
|
287
|
+
|
|
288
|
+
def update(self, webhook_id: str, params: Optional[Dict[str, Any]] = None, *, timeout: Optional[float] = None) -> Dict[str, Any]:
|
|
289
|
+
"""Update a webhook (``PATCH /v1/webhooks/{id}``)."""
|
|
290
|
+
return self._client.request("PATCH", f"/webhooks/{_q(webhook_id)}", body=params, timeout=timeout)
|
|
291
|
+
|
|
292
|
+
def delete(self, webhook_id: str, *, timeout: Optional[float] = None) -> Any:
|
|
293
|
+
"""Delete a webhook (``DELETE /v1/webhooks/{id}``)."""
|
|
294
|
+
return self._client.request("DELETE", f"/webhooks/{_q(webhook_id)}", timeout=timeout)
|
|
295
|
+
|
|
296
|
+
def deliveries(self, webhook_id: str, *, timeout: Optional[float] = None) -> List[Dict[str, Any]]:
|
|
297
|
+
"""Recent deliveries (``GET /v1/webhooks/{id}/logs``)."""
|
|
298
|
+
return self._client.request("GET", f"/webhooks/{_q(webhook_id)}/logs", timeout=timeout)
|
|
299
|
+
|
|
300
|
+
def test(self, webhook_id: str, params: Optional[Dict[str, Any]] = None, *, timeout: Optional[float] = None) -> Any:
|
|
301
|
+
"""Send a test event (``POST /v1/webhooks/{id}/test``)."""
|
|
302
|
+
return self._client.request("POST", f"/webhooks/{_q(webhook_id)}/test", body=params, timeout=timeout)
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
class SandboxResource:
|
|
306
|
+
"""Sandbox"""
|
|
307
|
+
|
|
308
|
+
def __init__(self, client: "ZapZCore") -> None:
|
|
309
|
+
self._client = client
|
|
310
|
+
|
|
311
|
+
def inbound(self, params: Optional[Dict[str, Any]] = None, *, timeout: Optional[float] = None) -> Any:
|
|
312
|
+
"""Simulate a received message (``POST /v1/sandbox/inbound``)."""
|
|
313
|
+
return self._client.request("POST", f"/sandbox/inbound", body=params, timeout=timeout)
|
|
314
|
+
|
|
315
|
+
def event(self, params: Optional[Dict[str, Any]] = None, *, timeout: Optional[float] = None) -> Any:
|
|
316
|
+
"""Fire any webhook event (``POST /v1/sandbox/events``)."""
|
|
317
|
+
return self._client.request("POST", f"/sandbox/events", body=params, timeout=timeout)
|
|
318
|
+
|
|
319
|
+
def set_message_status(self, message_id: str, params: Optional[Dict[str, Any]] = None, *, timeout: Optional[float] = None) -> Any:
|
|
320
|
+
"""Move a test message to a status (``POST /v1/sandbox/messages/{messageId}/status``)."""
|
|
321
|
+
return self._client.request("POST", f"/sandbox/messages/{_q(message_id)}/status", body=params, timeout=timeout)
|