Skip to content

Commit

Permalink
feat: Gather all reports by phone number in a new web page
Browse files Browse the repository at this point in the history
See URL "/report/[phone_number]".
  • Loading branch information
clemlesne committed Jan 24, 2024
1 parent bad118e commit f553855
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 174 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Extract of the data stored during the call:

### User report after the call

A report is available at `https://[your_domain]/call/report/[call_id]`. It shows the conversation history, claim data and reminders.
A report is available at `https://[your_domain]/report/[phone_number]` (like `http://localhost:8080/report/%2B133658471534`). It shows the conversation history, claim data and reminders.

![User report](./docs/user_report.jpg)

Expand Down
41 changes: 35 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from persistence.cosmos import CosmosStore
from persistence.sqlite import SqliteStore
from tenacity import retry, stop_after_attempt, wait_random_exponential
from urllib.parse import quote_plus
import asyncio
import html
import re
Expand All @@ -50,11 +51,15 @@
_logger = build_logger(__name__)
_logger.info(f"claim-ai v{CONFIG.version}")

# Jinja templates
jinja = Environment(
autoescape=select_autoescape(),
enable_async=True,
loader=FileSystemLoader("public_website"),
)
jinja.filters["quote_plus"] = lambda x: quote_plus(str(x)) if x else ""

# Azure OpenAI
_logger.info(f"Using OpenAI GPT model {CONFIG.openai.gpt_model}")
oai_gpt = AsyncAzureOpenAI(
api_version="2023-12-01-preview",
Expand All @@ -70,6 +75,8 @@
else None
),
)

# Azure Communication Services
source_caller = PhoneNumberIdentifier(CONFIG.communication_service.phone_number)
_logger.info(f"Using phone number {str(CONFIG.communication_service.phone_number)}")
# Cannot place calls with RBAC, need to use access key (see: https://learn.microsoft.com/en-us/azure/communication-services/concepts/authentication#authentication-options)
Expand All @@ -82,12 +89,16 @@
sms_client = SmsClient(
credential=DefaultAzureCredential(), endpoint=CONFIG.communication_service.endpoint
)

# Persistence
db = (
SqliteStore(CONFIG.database.sqlite)
if CONFIG.database.mode == DatabaseMode.SQLITE
else CosmosStore(CONFIG.database.cosmos_db)
)
search = AiSearchSearch(CONFIG.ai_search)

# FastAPI
_logger.info(f'Using root path "{CONFIG.api.root_path}"')
api = FastAPI(
contact={
Expand Down Expand Up @@ -124,23 +135,41 @@ async def health_liveness_get() -> None:


@api.get(
"/call/report/{call_id}",
"/report/{phone_number}",
description="Display the history of calls in a web page.",
)
async def report_history_get(phone_number: str) -> HTMLResponse:
calls = await db.call_asearch_all(phone_number) or []

template = jinja.get_template("history.html.jinja")
render = await template.render_async(
bot_company=CONFIG.workflow.bot_company,
bot_name=CONFIG.workflow.bot_name,
calls=calls,
phone_number=phone_number,
version=CONFIG.version,
)
return HTMLResponse(content=render)


@api.get(
"/report/{phone_number}/{call_id}",
description="Display the call report in a web page.",
)
async def call_report_get(call_id: UUID) -> HTMLResponse:
async def report_call_get(phone_number: str, call_id: UUID) -> HTMLResponse:
call = await db.call_aget(call_id)
if not call:
if not call or call.phone_number != phone_number:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Call {call_id} not found",
detail=f"Call {call_id} for phone number {phone_number} not found",
)

template = jinja.get_template("report.html")
template = jinja.get_template("report.html.jinja")
render = await template.render_async(
bot_company=CONFIG.workflow.bot_company,
bot_name=CONFIG.workflow.bot_name,
version=CONFIG.version,
call=call,
version=CONFIG.version,
)
return HTMLResponse(content=render)

Expand Down
19 changes: 19 additions & 0 deletions public_website/history.html.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends "templates/base.html.jinja" %}

{% block title %}
Historique des appels pour {{ phone_number }}
{% endblock %}

{% block content %}
<!-- History of calls -->
<div class="col-span-full space-y-4">
<div class="p-4 space-y-4 rounded-md lg:ring-1 ring-neutral-200/60 dark:ring-neutral-700/60">
<h2 class="text-lg">📞 Historique des appels pour {{ phone_number }}</h2>
{% for call in calls %}
<div>
<a class="hover:underline" href="/report/{{ call.phone_number | quote_plus }}/{{ call.call_id | quote_plus }}"><span class="font-mono">#{{ call.claim.claim_id }}</span> ({{ call.created_at.strftime('%d %b %Y %H:%M') }})</a>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
167 changes: 0 additions & 167 deletions public_website/report.html

This file was deleted.

Loading

0 comments on commit f553855

Please sign in to comment.