Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
clemlesne committed Jan 13, 2024
1 parent 7151082 commit 5935842
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,6 @@ async def call_event_post(request: Request, call_id: UUID) -> None:
client=client,
text=TTSPrompt.WELCOME_BACK,
)
await handle_media(
call=call,
client=client,
file="acknowledge.mp3",
)
await intelligence(call, client)

elif event_type == "Microsoft.Communication.CallDisconnected": # Call hung up
Expand All @@ -301,12 +296,6 @@ async def call_event_post(request: Request, call_id: UUID) -> None:
speech_text = event.data["speechResult"]["speech"]
_logger.info(f"Recognition completed ({call.id}): {speech_text}")

await handle_media(
call=call,
client=client,
file="acknowledge.mp3",
)

if speech_text is not None and len(speech_text) > 0:
call.messages.append(
CallMessageModel(content=speech_text, persona=CallPersona.HUMAN)
Expand All @@ -319,12 +308,6 @@ async def call_event_post(request: Request, call_id: UUID) -> None:
result_information = event.data["resultInformation"]
error_code = result_information["subCode"]

await handle_media(
call=call,
client=client,
file="acknowledge.mp3",
)

# Error codes:
# 8510 = Action failed, initial silence timeout reached
# 8532 = Action failed, inter-digit silence timeout reached
Expand Down Expand Up @@ -405,9 +388,22 @@ async def call_event_post(request: Request, call_id: UUID) -> None:


async def intelligence(call: CallModel, client: CallConnectionClient) -> None:
# Start loading sound
await handle_media_loop(
call=call,
client=client,
file="loading.mp3",
)

chat_res = await gpt_chat(call)
_logger.info(f"Chat ({call.id}): {chat_res}")

try:
# Cancel loading sound
client.cancel_all_media_operations()
except ResourceNotFoundError:
_logger.debug(f"Call hung up before playing ({call.id})")

if chat_res.intent == IndentAction.TALK_TO_HUMAN:
await handle_play(
call=call,
Expand Down Expand Up @@ -851,14 +847,15 @@ async def handle_recognize_media(
_logger.debug(f"Call hung up before recognizing ({call.id})")


async def handle_media(
async def handle_media_loop(
client: CallConnectionClient,
call: CallModel,
file: str,
context: Optional[str] = None,
) -> None:
try:
client.play_media_to_all(
loop=True,
operation_context=context,
play_source=FileSource(f"{CONFIG.resources.public_url}/{file}"),
)
Expand Down
Binary file removed resources/acknowledge.mp3
Binary file not shown.
Binary file added resources/loading.mp3
Binary file not shown.

0 comments on commit 5935842

Please sign in to comment.