Skip to content

Commit f553855

Browse files
committed
feat: Gather all reports by phone number in a new web page
See URL "/report/[phone_number]".
1 parent bad118e commit f553855

File tree

6 files changed

+240
-174
lines changed

6 files changed

+240
-174
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Extract of the data stored during the call:
6363

6464
### User report after the call
6565

66-
A report is available at `https://[your_domain]/call/report/[call_id]`. It shows the conversation history, claim data and reminders.
66+
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.
6767

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

main.py

+35-6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from persistence.cosmos import CosmosStore
3131
from persistence.sqlite import SqliteStore
3232
from tenacity import retry, stop_after_attempt, wait_random_exponential
33+
from urllib.parse import quote_plus
3334
import asyncio
3435
import html
3536
import re
@@ -50,11 +51,15 @@
5051
_logger = build_logger(__name__)
5152
_logger.info(f"claim-ai v{CONFIG.version}")
5253

54+
# Jinja templates
5355
jinja = Environment(
5456
autoescape=select_autoescape(),
5557
enable_async=True,
5658
loader=FileSystemLoader("public_website"),
5759
)
60+
jinja.filters["quote_plus"] = lambda x: quote_plus(str(x)) if x else ""
61+
62+
# Azure OpenAI
5863
_logger.info(f"Using OpenAI GPT model {CONFIG.openai.gpt_model}")
5964
oai_gpt = AsyncAzureOpenAI(
6065
api_version="2023-12-01-preview",
@@ -70,6 +75,8 @@
7075
else None
7176
),
7277
)
78+
79+
# Azure Communication Services
7380
source_caller = PhoneNumberIdentifier(CONFIG.communication_service.phone_number)
7481
_logger.info(f"Using phone number {str(CONFIG.communication_service.phone_number)}")
7582
# Cannot place calls with RBAC, need to use access key (see: https://learn.microsoft.com/en-us/azure/communication-services/concepts/authentication#authentication-options)
@@ -82,12 +89,16 @@
8289
sms_client = SmsClient(
8390
credential=DefaultAzureCredential(), endpoint=CONFIG.communication_service.endpoint
8491
)
92+
93+
# Persistence
8594
db = (
8695
SqliteStore(CONFIG.database.sqlite)
8796
if CONFIG.database.mode == DatabaseMode.SQLITE
8897
else CosmosStore(CONFIG.database.cosmos_db)
8998
)
9099
search = AiSearchSearch(CONFIG.ai_search)
100+
101+
# FastAPI
91102
_logger.info(f'Using root path "{CONFIG.api.root_path}"')
92103
api = FastAPI(
93104
contact={
@@ -124,23 +135,41 @@ async def health_liveness_get() -> None:
124135

125136

126137
@api.get(
127-
"/call/report/{call_id}",
138+
"/report/{phone_number}",
139+
description="Display the history of calls in a web page.",
140+
)
141+
async def report_history_get(phone_number: str) -> HTMLResponse:
142+
calls = await db.call_asearch_all(phone_number) or []
143+
144+
template = jinja.get_template("history.html.jinja")
145+
render = await template.render_async(
146+
bot_company=CONFIG.workflow.bot_company,
147+
bot_name=CONFIG.workflow.bot_name,
148+
calls=calls,
149+
phone_number=phone_number,
150+
version=CONFIG.version,
151+
)
152+
return HTMLResponse(content=render)
153+
154+
155+
@api.get(
156+
"/report/{phone_number}/{call_id}",
128157
description="Display the call report in a web page.",
129158
)
130-
async def call_report_get(call_id: UUID) -> HTMLResponse:
159+
async def report_call_get(phone_number: str, call_id: UUID) -> HTMLResponse:
131160
call = await db.call_aget(call_id)
132-
if not call:
161+
if not call or call.phone_number != phone_number:
133162
raise HTTPException(
134163
status_code=status.HTTP_404_NOT_FOUND,
135-
detail=f"Call {call_id} not found",
164+
detail=f"Call {call_id} for phone number {phone_number} not found",
136165
)
137166

138-
template = jinja.get_template("report.html")
167+
template = jinja.get_template("report.html.jinja")
139168
render = await template.render_async(
140169
bot_company=CONFIG.workflow.bot_company,
141170
bot_name=CONFIG.workflow.bot_name,
142-
version=CONFIG.version,
143171
call=call,
172+
version=CONFIG.version,
144173
)
145174
return HTMLResponse(content=render)
146175

public_website/history.html.jinja

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{% extends "templates/base.html.jinja" %}
2+
3+
{% block title %}
4+
Historique des appels pour {{ phone_number }}
5+
{% endblock %}
6+
7+
{% block content %}
8+
<!-- History of calls -->
9+
<div class="col-span-full space-y-4">
10+
<div class="p-4 space-y-4 rounded-md lg:ring-1 ring-neutral-200/60 dark:ring-neutral-700/60">
11+
<h2 class="text-lg">📞 Historique des appels pour {{ phone_number }}</h2>
12+
{% for call in calls %}
13+
<div>
14+
<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>
15+
</div>
16+
{% endfor %}
17+
</div>
18+
</div>
19+
{% endblock %}

public_website/report.html

-167
This file was deleted.

0 commit comments

Comments
 (0)