Tips & Tricks: Chat Transcript Custom Action (PHP)

When the chat session ends, the chat transcript is saved to the database. During the process, your custom PHP code can also be processed to send the chat transcript to a ticketing system, save it to another table or other actions you have in mind. Upgrading your PHP Live! system will not affect your custom code because your code is located in a file that is not updated or overridden.
Here is what you will want to do.
  1. Create a new directory named custom_code inside your PHP Live! folder.
    example: phplive/custom_code/
  2. Create a new file named new_chat_transcript.php inside the custom_code/ directory.
    example: phplive/custom_code/new_chat_transcript.php
  3. Place your custom PHP code inside the new_chat_transcript.php file. When the chat session ends, it will also process the new_chat_transcript.php file.
Available variables:
  • $formatted = The complete transcript with formatted tags such as <span> and <div>. This contains extra data then just the messages (timestamp, line markers, tags, etc).
  • $message_trans = The chat transcript with all the extra data and tags removed. In most situations, use this variable as the chat transcript. The system also uses this variable when emailing chat transcripts to the visitor.
  • $created = The time the chat session was created, in unixtime format.
  • $ended = The time the chat session ended, in unixtime format.
  • $deptid = The ID of the department the chat was routed to.
  • $deptinfo = Array hash containing the department information. The MySQL table p_departments will show all the possible values.
    Important: Always check to see if this variable has content before using the variable, example: isset( $deptinfo["name"] ).
  • $opid = The ID of the operator that accepted the chat.
  • $vname = Visitor's name.
  • $vemail = Visitor's email.
  • $question = Visitor's question.
  • $custom = The custom variables, including custom fields (name/value pair). The name/value pair is in the format of: field1-_-value1-cus-field2-_-value2
When the chat session ends, process your custom PHP code.
  • If your custom PHP code produces errors, it may affect the software functions or break the system. Use the above variables and populate them with temporary data for testing. If everything is ok, then place the working code in the new_chat_transcript.php file.
    Example code: The following example will automatically send the chat transcript to a URL API when the chat session ends. Note: The URL API is just an example URL and is not a functioning URL. Keep in mind, this is just an example to send the chat transcript to an API URL. If your ticketing or CRM can create tickets via email address, PHP Live! already has a feature to send a chat transcript automatically to an email address, a feature that can be configured at Setup Admin > Departments > Email Transcript (department option) .
    <?php
    // always check if $deptinfo has content or it will produce error if key does not exist
    $department_name = isset( $deptinfo["name"] ) ? $deptinfo["name"] : "" ;
    
    $request = curl_init( "https://www.phplivesupport.com/api/create_ticket.php" ) ;
    curl_setopt( $request, CURLOPT_RETURNTRANSFER, true ) ;
    curl_setopt( $request, CURLOPT_CUSTOMREQUEST, "POST" ) ;
    curl_setopt( $request, CURLOPT_POSTFIELDS, array( "transcript"=>$message_trans, "visitor_name"=>$vname, "visitor_email"=>$vemail, "custom_variables"=>$custom, "department_name"=>$department_name ) ) ;
    
    /* set to true to use cert */
    if ( false )
    {
    	/* cert (optional) */
    	curl_setopt( $request, CURLOPT_SSL_VERIFYPEER, true ) ;
    	curl_setopt( $request, CURLOPT_CAINFO, "$CONF[DOCUMENT_ROOT]/mapp/API/cacert.pem" ) ;
    	/* end cert */
    }
    else { curl_setopt( $request, CURLOPT_SSL_VERIFYPEER, false ) ; }
    
    $response = curl_exec( $request ) ;
    $curl_errno = curl_errno( $request ) ;
    $status = curl_getinfo( $request, CURLINFO_HTTP_CODE ) ; 
    curl_close( $request ) ;
    ?>
    
Important: The ability to include custom PHP code when the chat session ends was introduced with PHP Live! v.4.7.9.9.9.3 (released May 2019). Make sure your PHP Live! is the latest version if wanting to process custom PHP code when the chat session ends. The latest version can be downloaded at the client area.
Tips & Tricks
  • Open Chat via CSS - Reference the open chat function using CSS class within the span, div or link.
  • Add Custom Variables - Pre-populate the name, email and question or send your custom variables to the chat request window.
  • JavaScript Callbacks - Process your custom JavaScript code when the chat icon has loaded, has been clicked and more.
  • Custom Operator Console - Inject custom code to the operator console to add your custom widgets, actions and more.