Metadata-Version: 2.4
Name: appsocom
Version: 0.10.0
Summary: Python server SDK for Appsocom Live video calls: join tokens, rooms, moderation and webhook verification
Author: Appsotech Limited
License-Expression: MIT
Project-URL: Homepage, https://live.appsocom.com
Project-URL: Documentation, https://docs.appsocom.dev/server-sdks
Keywords: appsocom,video,calls,webrtc,sfu,cloudflare
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Communications :: Conferencing
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# appsocom (Python)

Python server SDK for [Appsocom Live](https://live.appsocom.com): mint join tokens, manage rooms and participants, moderate and verify webhooks. Python 3.9+, standard library only.

```bash
pip install appsocom
```

```python
import os
from appsocom import SfuKit, SfuKitError, verify_webhook

kit = SfuKit("https://api.appsocom.dev", os.environ["APPSOCOM_API_KEY"])  # project API key from the console

tok = kit.create_token("class-1", "u_7", name="Ada", role="student", metadata={"seat": 3})
tok["token"], tok["expiresAt"]

kit.set_role("class-1", "u_9", "student")             # promote an observer
kit.mute("class-1", "*", ["mic"], lock=True)          # mute everyone, keep them muted
kit.set_room_metadata("class-1", {"topic": "Fractions"})
kit.set_track_permissions("class-1", "u_7", {"allowAll": False, "allow": [{"userId": "u_1"}]})
kit.remove_participant("class-1", "u_8", ban=True)
```

Errors raise `SfuKitError` with `.status` and `.body`.

Webhooks (Flask):

```python
@app.post("/appsocom-webhooks")
def hook():
    event = verify_webhook(os.environ["APPSOCOM_WEBHOOK_SECRET"], request.get_data(), request.headers.get("X-SfuKit-Signature"))
    if event is None:
        return "", 401
    print(event["event"], event["roomId"])
    return "", 204
```

## AI assistant

Tell the assistant about your app once, mint a token for each signed-in person, and answer its tool calls at your `toolsUrl`:

```python
from appsocom import AssistantToolError, assistant_tools

kit.set_assistant_config({
    "name": "Ada",
    "toolsUrl": "https://app.example/assistant-tools",
    "tools": [{"name": "list_students", "description": "Students in a class", "parameters": {"type": "object", "properties": {"cls": {"type": "string"}}}}],
    "appMap": [{"label": "Fees", "path": "/fees", "aliases": ["bursary"]}],
})
tok = kit.create_assistant_token(user.id, name=user.name, role=user.role, metadata={"school": user.school_id})
# give tok["token"] to the browser (@appsocom/react <AssistantPanel>)

def list_students(args, call):  # runs as the person who asked
    if call["role"] != "teacher":
        raise AssistantToolError("Only teachers can list students.")  # the model sees this message
    return db.students(call["metadata"]["school"], args["cls"])

tools = assistant_tools(os.environ["APPSOCOM_WEBHOOK_SECRET"], {"list_students": list_students})

@app.post("/assistant-tools")
def assistant_tools_route():
    status, body = tools.handle(request.get_data(), request.headers.get("X-SfuKit-Signature"))
    return body, status, {"Content-Type": "application/json"}
```

`handle()` refuses bad or stale signatures with 401, answers `{"result": ...}` (`"done"` when a tool returns `None`) or `{"error": ...}`, and hides any exception other than `AssistantToolError` as "The tool failed." (pass `on_error=` to see it). Also: `get_assistant_config`, `delete_assistant_conversation(user_id, conversation_id)`, `verify_assistant_tool_call`.

## AI Tasks, Speech and Videos

```python
# AI Tasks: one Claude call from your server (a ready-made task, or your own prompt)
marked = kit.ai_task("mark", {"question": q, "answer": a, "rubric": rubric, "maxScore": 10}, user=student_id)
marked["json"]["score"], marked["usage"]["credits"]
draft = kit.ai_complete("Write a two-line welcome for new parents.", tier="standard")
data = kit.ai_complete("List the dates in this letter: ...", json={"type": "object", "properties": {"dates": {"type": "array"}}})
with kit.ai_stream("Tell a short story.") as story:
    for piece in story:
        print(piece, end="")
    story.usage  # model, tokens, cost, credits

# Speech
audio = kit.text_to_speech("Well done!", language="en")                  # mp3 bytes
link = kit.text_to_speech_url("Ẹ káàárọ̀", language="yo", expires_in=3600)  # link["url"] plays without your key
heard = kit.speech_to_text(recording_bytes, content_type="audio/webm", language="ha")  # heard["text"]

# Videos: an upload link for a browser, or send a file from your server
upload = kit.create_video_upload(3600, name="Lesson 1", upload_length=size)
kit.upload_video_file(upload, open("lesson.mp4", "rb"))
links = kit.video_playback(upload["videoId"])  # links["hls"], links["iframe"], links["thumbnail"]
```

`ai_complete` and `ai_task` return the API's answer as a dict (`text`, `json`, `usage`); a call the kit refuses (daily limit, no credit) raises `SfuKitError` with the status, and `ai_stream` raises it at once. Settings and reports: `get_ai_settings`, `set_ai_settings`, `get_ai_usage(days)`, `get_ai_audit(day, user)`, `get_speech_usage`, `set_speech_daily_budget`, `get_video_usage`. Videos: `copy_video`, `list_videos`, `get_video`, `update_video`, `delete_video`. See [AI Tasks](https://docs.appsocom.dev/ai-tasks/), [Speech](https://docs.appsocom.dev/speech/) and [Videos](https://docs.appsocom.dev/videos/).

All methods: `create_token`, `get_room`, `list_participants`, `update_room`, `set_room_metadata`, `lock_room`, `close_room`, `update_participant`, `set_permissions`, `set_role`, `set_participant_metadata`, `set_track_permissions`, `remove_participant`, `unban`, `mute`, `admit`, `post_chat`, `lower_hands`, `get_attendance`, `get_attendance_csv`, `clear_attendance`, `start_breakouts`, `end_breakouts`, `move_to_group`, `set_assistant_config`, `get_assistant_config`, `create_assistant_token`, `delete_assistant_conversation`, `ai_complete`, `ai_task`, `ai_stream`, `get_ai_settings`, `set_ai_settings`, `get_ai_usage`, `get_ai_audit`, `text_to_speech`, `text_to_speech_url`, `speech_to_text`, `get_speech_usage`, `set_speech_daily_budget`, `create_video_upload`, `upload_video_file`, `copy_video`, `list_videos`, `get_video`, `update_video`, `delete_video`, `video_playback`, `get_video_usage`, `health`, `request()`. Standalone: `sign_token`, `verify_webhook`, `sign_webhook`, `assistant_tools`, `verify_assistant_tool_call`.

Self-hosting your own deployment instead? Point `url` at it and pass its admin key; the optional `jwt_secret` signs tokens locally without an HTTP call.

Docs: [docs.appsocom.dev](https://docs.appsocom.dev) · License: MIT
