assistants.php
3 years ago
chatbot.php
3 years ago
chatbot_legacy.php
3 years ago
chatbot_logs.php
3 years ago
discussions.php
3 years ago
chatbot_logs.php
46 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Modules_Chatbot_Logs { |
| 4 | |
| 5 | public function __construct() { |
| 6 | add_filter( 'mwai_chatbot_reply', function ( $reply, $query, $params ) { |
| 7 | try { |
| 8 | global $mwai_core; |
| 9 | $newMessage = !empty( $params['newMessage'] ) ? $params['newMessage'] : ""; |
| 10 | |
| 11 | // We need to identify the user or session |
| 12 | $id = $mwai_core->get_user_id(); |
| 13 | if ( empty( $id ) ) { |
| 14 | $id = $mwai_core->get_ip_address(); |
| 15 | if ( empty( $id ) ) { |
| 16 | $id = 'RANDOM-' . uniqid(); |
| 17 | } |
| 18 | else { |
| 19 | $id = 'IP-' . $id; |
| 20 | } |
| 21 | } |
| 22 | else { |
| 23 | $id = 'USER-' . $id; |
| 24 | } |
| 25 | |
| 26 | // Create or Update Logs |
| 27 | $upload_dir = wp_upload_dir(); |
| 28 | $date = date( 'Y-m-d' ); |
| 29 | $chatbot_logs_dir = $upload_dir['basedir'] . '/chatbot'; |
| 30 | $file = $chatbot_logs_dir . '/' . $date . '-' . $id . '.txt'; |
| 31 | if ( !file_exists( $chatbot_logs_dir ) ) { |
| 32 | mkdir( $chatbot_logs_dir, 0777, true ); |
| 33 | } |
| 34 | $content = @file_get_contents( $file ); |
| 35 | $content .= "USER: " . $newMessage . "\n"; |
| 36 | $content .= "AI: " . $reply . "\n"; |
| 37 | file_put_contents( $file, $content ); |
| 38 | } |
| 39 | catch ( Exception $e ) { |
| 40 | error_log( $e->getMessage() ); |
| 41 | } |
| 42 | return $reply; |
| 43 | }, 10, 3 ); |
| 44 | } |
| 45 | } |
| 46 |