feature: added event handlers in constructor and call func in each _handle_* func

This commit is contained in:
ssillerom
2026-01-28 10:54:26 +01:00
parent 105824a372
commit a4acafd3be

View File

@@ -188,6 +188,15 @@ class GenesysAudioHookSerializer(FrameSerializer):
self._input_variables: Optional[Dict[str, Any]] = None # Custom input from Genesys self._input_variables: Optional[Dict[str, Any]] = None # Custom input from Genesys
self._output_variables: Optional[Dict[str, Any]] = None # Custom output to Genesys self._output_variables: Optional[Dict[str, Any]] = None # Custom output to Genesys
# Event handlers
self._register_event_handler("on_open")
self._register_event_handler("on_close")
self._register_event_handler("on_ping")
self._register_event_handler("on_pause")
self._register_event_handler("on_update")
self._register_event_handler("on_error")
self._register_event_handler("on_dtmf")
@property @property
def session_id(self) -> str: def session_id(self) -> str:
@@ -786,6 +795,8 @@ class GenesysAudioHookSerializer(FrameSerializer):
f"conversation={self._conversation_id}, ani={ani}" f"conversation={self._conversation_id}, ani={ani}"
) )
await self._call_event_handler("on_open", message)
return OutputTransportMessageUrgentFrame(message=self.create_opened_response( return OutputTransportMessageUrgentFrame(message=self.create_opened_response(
start_paused=self._params.start_paused, start_paused=self._params.start_paused,
supported_languages=self._params.supported_languages, supported_languages=self._params.supported_languages,
@@ -815,6 +826,8 @@ class GenesysAudioHookSerializer(FrameSerializer):
logger.info(f"Sending closed response to Genesys...") logger.info(f"Sending closed response to Genesys...")
await self._call_event_handler("on_close", message)
# Return as urgent frame to be sent through pipeline immediately # Return as urgent frame to be sent through pipeline immediately
# Include any output variables that were set during the session # Include any output variables that were set during the session
return OutputTransportMessageUrgentFrame( return OutputTransportMessageUrgentFrame(
@@ -833,6 +846,9 @@ class GenesysAudioHookSerializer(FrameSerializer):
OutputTransportMessageUrgentFrame with pong response. OutputTransportMessageUrgentFrame with pong response.
""" """
logger.info(f"Sending pong response to Genesys...") logger.info(f"Sending pong response to Genesys...")
await self._call_event_handler("on_ping", message)
# Return as urgent frame to be sent through pipeline immediately # Return as urgent frame to be sent through pipeline immediately
return OutputTransportMessageUrgentFrame(message=self.create_pong_response()) return OutputTransportMessageUrgentFrame(message=self.create_pong_response())
@@ -855,6 +871,8 @@ class GenesysAudioHookSerializer(FrameSerializer):
self._is_paused = True self._is_paused = True
await self._call_event_handler("on_pause", message)
# Note: Application should call create_resumed_response() when ready # Note: Application should call create_resumed_response() when ready
return None return None
@@ -876,6 +894,8 @@ class GenesysAudioHookSerializer(FrameSerializer):
logger.debug(f"AudioHook update received: {params}") logger.debug(f"AudioHook update received: {params}")
await self._call_event_handler("on_update", message)
return None return None
async def _handle_error(self, message: Dict[str, Any]) -> Frame | None: async def _handle_error(self, message: Dict[str, Any]) -> Frame | None:
@@ -893,6 +913,8 @@ class GenesysAudioHookSerializer(FrameSerializer):
logger.error(f"AudioHook error from Genesys: {code} - {error_msg}") logger.error(f"AudioHook error from Genesys: {code} - {error_msg}")
await self._call_event_handler("on_error", message)
return None return None
async def _handle_dtmf(self, message: Dict[str, Any]) -> Frame | None: async def _handle_dtmf(self, message: Dict[str, Any]) -> Frame | None:
@@ -916,6 +938,8 @@ class GenesysAudioHookSerializer(FrameSerializer):
logger.info(f"DTMF received: {digit}") logger.info(f"DTMF received: {digit}")
await self._call_event_handler("on_dtmf", message)
try: try:
return InputDTMFFrame(KeypadEntry(digit)) return InputDTMFFrame(KeypadEntry(digit))
except ValueError: except ValueError: