def handledirecttransfer(self, ivr): Define a handler for the hangup signal def…
- User
def handle_direct_transfer(self, ivr): # Define a handler for the hangup signal def hangup_handler(signum, frame): raise Exception("Hangup detected")
# Register the hangup signal handler signal.signal(signal.SIGHUP, hangup_handler) try: destination_number = ivr.number self.custom_log(f"Transferring call to {destination_number}") retry_count = 0 max_retries = 3 call_start_time = None call_end_time = None recording_filename = None # Retry loop while retry_count < max_retries: self.custom_log(f"Attempt {retry_count + 1} to call {destination_number}") # Record the call with MixMonitor timestamp = int(time.time()) recording_filename = f"/var/lib/asterisk/sounds/recordings/{self.caller_number}-{timestamp}.wav" self.agi.exec_command('MixMonitor', recording_filename) # Start the timer for the call call_start_time = time.time() try: # Dial the number with the 'g' option (continue after hangup) self.agi.exec_command('Dial', f'SIP/{destination_number}@{self.dialed_number},30,m,g') self.custom_log("AFTER CALL Line") # Get Dial status and Hangup cause dialstatus = self.agi.get_variable('DIALSTATUS') hangupcause = int(self.agi.get_variable('HANGUPCAUSE') or 0) self.custom_log(f"DIALSTATUS: {dialstatus}, HANGUPCAUSE: {hangupcause}") except Exception as e: self.custom_log(f"Exception occurred during or after Dial: {e}") # Assume call was hung up if call_start_time: call_end_time = time.time() call_duration = int(call_end_time - call_start_time) # Duration in seconds # Log the call details to the server after call finishes try: self.send_call_log(call_duration=call_duration, recording_filename=recording_filename) except Exception as log_exception: self.custom_log(f"Error logging call details: {log_exception}") return None # Stop the timer and calculate call duration if the call was answered if dialstatus == 'ANSWER': call_end_time = time.time() call_duration = int(call_end_time - call_start_time) # Duration in seconds # Log the call details to the server after call finishes self.send_call_log(call_duration=call_duration, recording_filename=recording_filename) self.custom_log("Call was answered.") return None # End the call flow after call completes elif hangupcause == 21: # Call Rejected self.custom_log("Call was rejected by the callee.") self.agi.appexec('Playback', 'call-rejected') self.agi.hangup() return None elif dialstatus == 'BUSY': retry_count += 1 if retry_count < max_retries: self.custom_log(f"Line is busy. Retrying ({retry_count}/{max_retries})...") self.agi.appexec('Wait', '1') else: self.custom_log("Line is busy after 3 attempts.") self.agi.appexec('Playback', 'line-busy') self.agi.hangup() return None elif dialstatus in ['NOANSWER', 'CHANUNAVAIL', 'CONGESTION']: retry_count += 1 if retry_count < max_retries: self.custom_log( f"Call not answered or channel unavailable. Retrying ({retry_count}/{max_retries})...") self.agi.appexec('Wait', '1') else: self.custom_log("Call could not be completed after 3 attempts.") self.agi.appexec('Playback', 'cannot-complete') self.agi.hangup() return None else: self.custom_log(f"Unhandled DIALSTATUS: {dialstatus}") self.agi.hangup() return None return None except Exception as e: self.custom_log(f"Error transferring call: {e}", level=logging.ERROR) # If hangup detected, calculate call duration and log details if str(e) == "Hangup detected": if call_start_time: call_end_time = time.time() call_duration = int(call_end_time - call_start_time) # Log the call details to the server after call finishes try: self.send_call_log(call_duration=call_duration, recording_filename=recording_filename) except Exception as log_exception: self.custom_log(f"Error logging call details: {log_exception}") else: # Handle other exceptions as needed pass raise - Assistant
Yes, it is possible to redirect the call and present the
caller_numberas the caller ID to the called party. In your Asterisk AGI script, you can achieve this by setting theCALLERID(num)variable before executing theDialcommand. This way, the called party will see the original caller's number as the incoming caller ID.