title: Freeswitch: How to determine if call was answered when bridging an inbound and outbound call?
body: <p>In this implementation, I'm trying to do call patching between an inbound call and a outbound call via the <code>session.bridge()</code> command.</p>
  
  <p>I have already received the inbound call and my Python script is playing. In this script I'm trying to patch the running call with the numbers in the <code>number_list(list of numbers)</code> one by one until the list is exhausted or till the call is patched and answered.</p>
  
  <p>My Code:</p>
  
  <pre><code>for cp_num in connObj.cp_list:
      ivr_log.debug("Attempting to dial Call Patch Number:%s"%(cp_num['cp_no']))
      connObj.patch_uuid = uuid.uuid1()
      cmd_str = """{ignore_early_media=true,execute_on_answer=record_session %s/%s_%s.wav,origination_uuid=%s,originate_timeout=45,script_name=gaadi_test}freetdm/wp4/a/%s"""%(connObj.recording_path,connObj.uuid,connObj.caller_id,connObj.patch_uuid,cp_num['cp_no'])
      connObj.bridge(cmd_str) #BRIDGE
      connObj.hangup_cause = connObj.getVar("last_bridge_hangup_cause")
      if connObj.hangup_cause in ['NORMAL_CLEARING', '']:
          connObj.cp_status = "SUCCESS"
          return True
  connObj.cp_status = "FAILED"
  connObj.playback(connObj.path_sound + 'all_reps_busy.wav')
  return False
  </code></pre>
  
  <p>Multiple numbers to the bridge command for sequential dialing.
  From freeswitch documentation:
  Multiple endpoints sequential -- no limit to failover number</p>
  
  <pre><code>bridge &lt;target_endpoint&gt;[|&lt;target_endpoint&gt;]
  </code></pre>
  
  <p>My Implementation</p>
  
  <pre><code>cmd1 = """{ignore_early_media=true,originate_timeout=45}[origination_uuid=%s,script_name=gaadi_test]freetdm/wp4/a/%s"""%(connObj.patch_uuid1,connObj.cp_list[0]['cp_no'])
  cmd2 = """[origination_uuid=%s,script_name=gaadi_test]freetdm/wp4/a/%s"""%(connObj.patch_uuid2,connObj.cp_list[1]['cp_no'])
  cmd = "%s[|%s]"%(cmd3,cmd4)
  ivr_log.debug("CMD=%s"%(cmd))
  connObj.call_patch_start_time = int(time.time())
  connObj.patch_start_datetime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  connObj.bridge(cmd)
  </code></pre>
  
  <p>P.S.: ConnObj.bridge(cmd) is working perfect for only one target point.</p>
  
  <p>Sequential Dialing solution:
  just a little changes in cmd1 and cmd2. I have not confirmed if the origination uuid will be different for both the calls.</p>
  
  <pre><code>cmd1 = """{ignore_early_media=true,originate_timeout=45,script_name=gaadi_test}[origination_uuid=%s]freetdm/wp4/a/%s"""%(connObj.patch_uuid1,connObj.cp_list[0]['cp_no'])
  cmd2 = """[origination_uuid=%s]freetdm/wp4/a/%s"""%(connObj.patch_uuid2,connObj.cp_list[1]['cp_no'])
  cmd="%s|%s" #removed the brackets and its working now.
  connObj.bridge(cmd)
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>Finally I solved the problem with Stanislav's approach. I used api_on_answer. On answer I wrote a file on the disk and then checked whether the file was present or not. If the file was present then the call was answered.</p>