aicaller — Python SDK
SDK Python cho hệ thống AI Caller (gọi điện telesales bằng AI). Cho phép đội CRM
tích hợp: cấu hình kịch bản/tiêu chí, gửi SĐT cho AI gọi, đọc kết quả chấm điểm,
tải ghi âm, lấy lịch sử cuộc gọi — qua REST API /api/v1 bằng API key.
Thiết kế đầy đủ (endpoint, scope, webhook, điều kiện tiên quyết) xem
docs/phases/phase-7-public-api-sdk.md.
Cài đặt
pip install aicaller-sdk # từ PyPI / private index (khi đã publish)
# hoặc cài từ mã nguồn trong repo (dev):
pip install -e sdk/python
Yêu cầu Python ≥ 3.9. Phụ thuộc: httpx, pydantic v2.
Lấy API key
Tạo trên Portal → Cài đặt → API keys → Tạo key. Full key (sk_live_...) chỉ hiện
1 lần; lưu vào biến môi trường, đừng hard-code.
Quickstart
import os
from aicaller import AICallerClient
client = AICallerClient(
api_key=os.environ["AICALLER_API_KEY"], # "sk_live_..."
base_url="https://your-app.com", # mặc định http://localhost:3000
)
# 1) Cấu hình kịch bản + tiêu chí
sc = client.scenarios.create(name="Bán bảo hiểm", content_md="# Xin chào ...")
cr = client.criteria.create(name="Chấm telesales", content_md="## Nhóm điểm ...")
# 2) Gửi SĐT cho AI gọi (vào campaign đã tạo sẵn trên Portal)
client.campaigns.add_customers(
campaign_id=7,
customers=[
{"phone": "0901234567", "name": "Nguyễn Văn A", "metadata": {"crm_id": "C-123"}},
{"phone": "+84902345678", "name": "Trần B"},
],
)
client.campaigns.start(campaign_id=7)
# 3) Lịch sử + kết quả chấm điểm (tự lật hết các trang)
for call in client.calls.iter(campaign_id=7, outcome="potential"):
detail = client.calls.get(call.id) # score, outcome, summary, group_scores, transcript
print(detail.id, detail.score, detail.summary)
# 4) Tải ghi âm (signed URL từ R2, hết hạn sau ít phút)
link = client.calls.recording_url(call_id=123)
print(link.url)
Nhận kết quả qua Webhook (khỏi phải poll)
Khai báo webhook_url khi tạo API key. Khi mỗi cuộc gọi chấm điểm xong, server đẩy
POST {webhook_url} với event call.scored, ký HMAC-SHA256 ở header X-Signature.
# Trong endpoint CRM của bạn (ví dụ Flask):
from aicaller import webhooks
@app.post("/aicaller/webhook")
def hook():
raw = request.get_data() # RAW body, KHÔNG json.loads trước
sig = request.headers.get("X-Signature", "")
try:
event = webhooks.verify(raw, sig, secret=os.environ["AICALLER_WEBHOOK_SECRET"])
except webhooks.WebhookVerificationError:
return "", 400
# event.call_id, event.score, event.outcome, event.customer["crm_id"] ...
return "", 200
Xử lý lỗi
Mọi lỗi kế thừa AICallerError:
| Lớp | Khi nào |
|---|---|
AuthenticationError |
401 — key sai / thu hồi |
PermissionError_ |
403 — thiếu scope |
NotFoundError |
404 — không thấy (hoặc không thuộc key) |
ValidationError_ |
400/422 — payload sai |
RateLimitError |
429 — vượt rate limit/quota (có .retry_after) |
ServerError |
5xx |
APIConnectionError |
không kết nối được |
SDK tự retry (backoff) cho lỗi mạng + 429/5xx; không retry lỗi logic 4xx.
from aicaller import RateLimitError
try:
client.campaigns.start(7)
except RateLimitError as e:
print("Chờ", e.retry_after, "giây")
Tài nguyên & method
| Resource | Method |
|---|---|
client.scenarios |
list, get, create, update |
client.criteria |
list, get, create, update |
client.campaigns |
list, get, add_customers, list_customers, start, pause |
client.calls |
list, iter, get, recording_url |
client.webhooks* |
verify, compute_signature (module aicaller.webhooks) |
* Webhook là module tiện ích, không cần API key.
Chạy test (dev)
pip install -e "sdk/python[dev]"
pytest sdk/python
Test dùng httpx.MockTransport — không cần server thật, không đụng mạng.
Tài liệu API đầy đủ
- Trang tương tác (Redoc):
https://your-app.com/api-docs.html - Spec máy đọc (OpenAPI 3.0):
https://your-app.com/openapi.yaml— dùng để sinh client cho ngôn ngữ khác.
Versioning
SDK pin /api/v1. Server đổi phá vỡ → lên /api/v2 + SDK bump major; v1 giữ nguyên
để tích hợp cũ không vỡ.
Metadata
Release files for aicaller-sdk 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| aicaller_sdk-0.1.0.tar.gz | 10.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| aicaller_sdk-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.8 kB
Release files / aicaller_sdk-0.1.0.tar.gz
| Download URL | aicaller_sdk-0.1.0.tar.gz |
|---|---|
| Size | 10.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7eb2c71ef3011ef3b3ad3935565923c5ff44e12334716dd1571133bec88465eb
|
|
BLAKE2b-256 checksum How to use checksums |
3fbc1f7765828bec0616efbf943c4569b4127fbcb8e5debbc5fdb2614c83dca4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / aicaller_sdk-0.1.0-py3-none-any.whl
| Download URL | aicaller_sdk-0.1.0-py3-none-any.whl |
|---|---|
| Size | 14.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6daf5b5630f3168ab3c4d2e9875d5d22b6b986cb9e98a5ab2005676492500732
|
|
BLAKE2b-256 checksum How to use checksums |
51f9b5192f86a3652d2b5465a163f16b252e50dd13f6d1e601e57f7477867f6a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for aicaller-sdk 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.