assistants.php
3 years ago
chatbot-chatgpt.css
3 years ago
chatbot-chatgpt.scss
3 years ago
chatbot.php
3 years ago
chatbot_chats.php
3 years ago
chatbot_logs.php
3 years ago
chatbot_chats.php
94 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Modules_Chatbot_Chats { |
| 4 | private $wpdb = null; |
| 5 | private $table_chats = null; |
| 6 | private $db_check = false; |
| 7 | |
| 8 | public function __construct() { |
| 9 | global $wpdb; |
| 10 | $this->wpdb = $wpdb; |
| 11 | $this->table_chats = $wpdb->prefix . 'mwai_chats'; |
| 12 | add_filter( 'mwai_chatbot_reply', [ $this, 'chatbot_reply' ], 10, 3 ); |
| 13 | } |
| 14 | |
| 15 | function chatbot_reply( $rawText, $query, $params ) { |
| 16 | global $mwai_core; |
| 17 | $userIp = $mwai_core->get_ip_address(); |
| 18 | $userId = $mwai_core->get_user_id(); |
| 19 | $chatClientId = isset( $params['clientId'] ) ? $params['clientId'] : $query->session; |
| 20 | $ssChatId = hash( 'sha256', $userIp . $userId . $chatClientId ); |
| 21 | $this->check_db(); |
| 22 | $chat = $this->wpdb->get_row( $this->wpdb->prepare( "SELECT * FROM $this->table_chats WHERE chatId = %s", $ssChatId ) ); |
| 23 | if ( $chat ) { |
| 24 | $chat->messages = json_decode( $chat->messages ); |
| 25 | $chat->messages[] = [ |
| 26 | 'type' => 'user', |
| 27 | 'text' => $params['rawInput'] |
| 28 | ]; |
| 29 | $chat->messages[] = [ |
| 30 | 'type' => 'ai', |
| 31 | 'text' => $rawText |
| 32 | ]; |
| 33 | $chat->messages = json_encode( $chat->messages ); |
| 34 | $chat->updated = wp_date( 'Y-m-d H:i:s' ); |
| 35 | $this->wpdb->update( $this->table_chats, (array) $chat, [ 'id' => $chat->id ] ); |
| 36 | } |
| 37 | else { |
| 38 | $chat = [ |
| 39 | 'chatId' => $ssChatId, |
| 40 | 'messages' => json_encode( [ |
| 41 | [ |
| 42 | 'type' => 'user', |
| 43 | 'text' => $params['rawInput'] |
| 44 | ], |
| 45 | [ |
| 46 | 'type' => 'ai', |
| 47 | 'text' => $rawText |
| 48 | ] |
| 49 | ] ), |
| 50 | 'extra' => json_encode( [ |
| 51 | 'ip' => $userIp, |
| 52 | 'userId' => $userId, |
| 53 | 'session' => $query->session, |
| 54 | 'model' => $query->model, |
| 55 | 'temperature' => $query->temperature |
| 56 | ] ) |
| 57 | ]; |
| 58 | $this->wpdb->insert( $this->table_chats, $chat ); |
| 59 | } |
| 60 | return $rawText; |
| 61 | } |
| 62 | |
| 63 | function check_db() { |
| 64 | if ( $this->db_check ) { |
| 65 | return true; |
| 66 | } |
| 67 | $this->db_check = !( strtolower( |
| 68 | $this->wpdb->get_var( "SHOW TABLES LIKE '$this->table_chats'" ) ) != strtolower( $this->table_chats ) |
| 69 | ); |
| 70 | if ( !$this->db_check ) { |
| 71 | $this->create_db(); |
| 72 | $this->db_check = !( strtolower( |
| 73 | $this->wpdb->get_var( "SHOW TABLES LIKE '$this->table_chats'" ) ) != strtolower( $this->table_chats ) |
| 74 | ); |
| 75 | } |
| 76 | return $this->db_check; |
| 77 | } |
| 78 | |
| 79 | function create_db() { |
| 80 | $charset_collate = $this->wpdb->get_charset_collate(); |
| 81 | $sqlLogs = "CREATE TABLE $this->table_chats ( |
| 82 | id BIGINT(20) NOT NULL AUTO_INCREMENT, |
| 83 | chatId VARCHAR(64) NOT NULL NULL, |
| 84 | messages TEXT NOT NULL NULL, |
| 85 | extra TEXT NOT NULL NULL, |
| 86 | created DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 87 | updated DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, |
| 88 | PRIMARY KEY (id) |
| 89 | ) $charset_collate;"; |
| 90 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 91 | dbDelta( $sqlLogs ); |
| 92 | } |
| 93 | |
| 94 | } |