Hi all We are using a function to transfer calls ...
# agents
b
Hi all We are using a function to transfer calls to other numbers. The issue we're having is that when we use it, after 15 seconds or no answer, the phone continues to ring. How can we terminate the call so the transferred call stops ringing? And how can we know if there's no answer, busy signal, or invalid number - how can we detect these statuses?
Copy code
async def transfer_call(participant_identity: str, room_name: str, transfer_to: str) -> str:
    logger.info(f"[TRANSFER] Starting call transfer:")
    logger.info(f"[TRANSFER] - Participant Identity: {participant_identity}")
    logger.info(f"[TRANSFER] - Room Name: {room_name.name}")
    logger.info(f"[TRANSFER] - Transfer To: {transfer_to}")

    async with api.LiveKitAPI() as livekit_api:
        try:
            logger.info(f"[TRANSFER] Fetching participant info for: {participant_identity}")
            # Fetch participant info
            participant_info = await livekit_api.room.get_participant(
                RoomParticipantIdentity(room=room_name.name, identity=participant_identity)
            )
            logger.info(f"[TRANSFER] Fetched participant info:\n{participant_info}")

            # DEBUG: log SIP attributes if present
            if participant_info.attributes:
                logger.info("[TRANSFER] Participant Attributes:")
                for k, v in participant_info.attributes.items():
                    logger.info(f"[TRANSFER] - {k}: {v}")

            # Normalize number
            normalized_transfer_to = normalize_transfer_to(transfer_to)
            logger.info(f"[TRANSFER] Normalized Transfer To: {normalized_transfer_to}")

            # Build transfer request
            transfer_request = TransferSIPParticipantRequest(
                participant_identity=participant_identity,
                room_name=room_name.name,
                transfer_to=normalized_transfer_to,
                play_dialtone=False,
                ringing_timeout=Duration(seconds=5)
            )
            logger.debug(f"[TRANSFER] Transfer Request:\n{transfer_request}")

            # Attempt transfer
            logger.info(f"[TRANSFER] Attempting to transfer participant '{participant_identity}' to '{normalized_transfer_to}'")
            await livekit_api.sip.transfer_sip_participant(transfer_request)
            logger.info(f"[TRANSFER] ✅ Successfully transferred participant '{participant_identity}' to '{transfer_to}'")
            return f"Successfully transferred call to {transfer_to}"

        except TwirpError as e:
            logger.error(f"[TRANSFER] ❌ SIP transfer failed with TwirpError: {e}")
            logger.error(f"[TRANSFER] - Error Code: {e.code}")
            logger.error(f"[TRANSFER] - Status: {e.status}")
            logger.error(f"[TRANSFER] - Message: {e.message}")

            if e.metadata:
                logger.error("[TRANSFER] - Metadata:")
                for key, val in e.metadata.items():
                    logger.error(f"[TRANSFER]   • {key}: {val}")
                    if key == "error_details":
                        try:
                            decoded = base64.b64decode(val).decode("utf-8")
                            logger.error(f"[TRANSFER]   → Decoded 'error_details': {decoded}")
                        except Exception as decode_error:
                            logger.warning(f"[TRANSFER]   ⚠️ Failed to decode error_details: {decode_error}")
            return f"Politely apologize for the failed call transfer, then immediately offer help. Say something like: 'I am sorry, your call transfer wasn't successful. Can I help you with something else?'"

        except Exception as e:
            logger.error(f"[TRANSFER] Unexpected error during call transfer: {e}")
            logger.exception("[TRANSFER] Full exception details:")
            return f"Politely apologize for the failed call transfer, then immediately offer help. Say something like: 'I am sorry, your call transfer wasn't successful. Can I help you with something else?'"
r